Java Code Examples for weka.core.Instance#setClassValue()

The following examples show how to use weka.core.Instance#setClassValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instances mergeClassesOfInstances(final Instances data, final List<Set<String>> instancesCluster) {
	List<String> classes = new LinkedList<>();
	IntStream.range(0, instancesCluster.size()).forEach(x -> classes.add("C" + ((double) x)));

	Instances newData = WekaUtil.getEmptySetOfInstancesWithRefactoredClass(data, classes);

	for (Instance i : data) {
		Instance iNew = (Instance) i.copy();
		String className = i.classAttribute().value((int) Math.round(i.classValue()));
		for (Set<String> cluster : instancesCluster) {
			if (cluster.contains(className)) {
				iNew.setClassValue(instancesCluster.indexOf(cluster));
				iNew.setDataset(newData);
				newData.add(iNew);
			}
		}
	}
	return newData;
}
 
Example 2
Source File: RDG1.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tries to classify an example. 
 * 
 * @param example the example to classify
 * @return true if it could be classified
 * @throws Exception if something goes wrong
 */
private boolean classifyExample(Instance example) throws Exception {
  double classValue = -1.0;  

  for (Enumeration e = m_DecisionList.elements(); 
       e.hasMoreElements() && classValue < 0.0;) {
    RuleList rl = (RuleList) e.nextElement();
    classValue = rl.classifyInstance(example);   
  }
  if (classValue >= 0.0) {
    example.setClassValue(classValue);
    return true;
  } 
  else {
    return false;
  }
}
 
Example 3
Source File: RDG1.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Classify example with maximum vote the following way.
 * With every rule in the decisionlist, it is evaluated if
 * the given instance could be the class of the rule.
 * Finally the class value that receives the highest number of votes
 * is assigned to the example.
 * 
 * @param example example to be reclassified
 * @return instance with new class value
 * @throws Exception if classification fails
 */
private Instance votedReclassifyExample(Instance example) throws Exception {
  int classVotes[] = new int [getNumClasses()]; 
  for (int i = 0; i < classVotes.length; i++) classVotes[i] = 0; 

  for (Enumeration e = m_DecisionList.elements(); 
       e.hasMoreElements();) {
    RuleList rl = (RuleList) e.nextElement();
    int classValue = (int) rl.classifyInstance(example);
    if (classValue >= 0) classVotes[classValue]++;  
  }
  int maxVote = 0;
  int vote = -1;
  for (int i = 0; i < classVotes.length; i++) {
    if (classVotes[i] > maxVote) {
      maxVote = classVotes[i];
      vote = i; 
    }
  }
  if (vote >= 0)
    example.setClassValue((double) vote);
  else
    throw new Exception ("Error in instance classification.");

  return example;
}
 
Example 4
Source File: Decorate.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** 
    * Labels the artificially generated data.
    *
    * @param artData the artificially generated instances
    * @throws Exception if instances cannot be labeled successfully 
    */
   protected void labelData(Instances artData) throws Exception {
Instance curr;
double []probs;

for(int i=0; i<artData.numInstances(); i++){
    curr = artData.instance(i);
    //compute the class membership probs predicted by the current ensemble 
    probs = distributionForInstance(curr);
    //select class label inversely proportional to the ensemble predictions
    curr.setClassValue(inverseLabel(probs));
}	
   }
 
Example 5
Source File: RTUpdateable.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	int L = x.classIndex();

	for (int j = 0; j < L; j++) {
		if(x.value(j) > 0.0) {
			Instance x_j = convertInstance(x);
			x_j.setClassValue(j);
			((UpdateableClassifier)m_Classifier).updateClassifier(x_j);
		}
	}
}
 
Example 6
Source File: ZooModelTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
private Instances shrinkInstances(Instances data) {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < data.numAttributes(); i++) {
        atts.add(data.attribute(i));
    }
    Instances shrunkenData = new Instances("shrinked", atts, 10);
    shrunkenData.setClassIndex(1);
    for (int i = 0; i < 10; i++) {
        Instance inst = data.get(i);
        inst.setClassValue(i % 10);
        inst.setDataset(shrunkenData);
        shrunkenData.add(inst);
    }
    return shrunkenData;
}
 
Example 7
Source File: ZooModelTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
private Instances shrinkInstances(Instances data) {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < data.numAttributes(); i++) {
        atts.add(data.attribute(i));
    }
    Instances shrunkenData = new Instances("shrinked", atts, 10);
    shrunkenData.setClassIndex(1);
    for (int i = 0; i < 10; i++) {
        Instance inst = data.get(i);
        inst.setClassValue(i % 10);
        inst.setDataset(shrunkenData);
        shrunkenData.add(inst);
    }
    return shrunkenData;
}
 
Example 8
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Instances getRefactoredInstances(final Instances data, final Map<String, String> classMap) {

		List<String> targetClasses = new ArrayList<>(new HashSet<>(classMap.values()));
		Instances childData = WekaUtil.getEmptySetOfInstancesWithRefactoredClass(data, targetClasses);
		for (Instance i : data) {
			String className = i.classAttribute().value((int) Math.round(i.classValue()));
			if (classMap.containsKey(className)) {
				Instance iNew = WekaUtil.getRefactoredInstance(i, targetClasses);
				iNew.setClassValue(classMap.get(className));
				iNew.setDataset(childData);
				childData.add(iNew);
			}
		}
		return childData;
	}
 
Example 9
Source File: EvaluationUtilsTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public void cocoEvaluationTest() throws InterruptedException {

		String instString = "[0.9718934893608093, 0.001479289960116148, 0.002958579920232296, 0.0, 0.001479289960116148, 0.0, 0.0, 0.0, 0.002958579920232296, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.001479289960116148, 0.0, 0.001479289960116148, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.002958579920232296, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.004437869880348444, 0.0, 0.001479289960116148, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.001479289960116148, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.001479289960116148, 0.004437869880348444, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "
				+ "0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.489645004272461, 11.947349548339844, 0.2812563180923462, "
				+ "87.98896789550781, 4175163.5, 0.0, 0.0, 0.0, 0.0, 3.0]";

		double[] arr = Arrays.stream(instString.substring(1, instString.length() - 1).split(",")).map(String::trim)
				.mapToDouble(Double::parseDouble).toArray();

		ArrayList<Attribute> atts = new ArrayList<>();
		for (int i = 0; i < arr.length - 1; i++) {
			atts.add(new Attribute("att" + i));
		}
		atts.add(new Attribute("class", Arrays.asList("3.0")));
		final Instances batch = new Instances("Test", atts, 1);
		batch.setClassIndex(batch.numAttributes() - 1);

		final Instance inst = new DenseInstance(1, arr);
		inst.setDataset(batch);
		inst.setClassValue("" + arr[arr.length - 1]);
		batch.add(inst);

		Assert.assertEquals(0.3132617035483811, EvaluationUtils.calculateCOCOForBatch(batch), 0.00001);
	}
 
Example 10
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
static Instances matricesToInstances(final List<INDArray> matrices, final Instances refInstances) {
	if (matrices == null || matrices.isEmpty()) {
		throw new IllegalArgumentException("Parameter 'matrices' must not be null or empty!");
	}

	// Create attributes
	final ArrayList<Attribute> attributes = new ArrayList<>();
	for (int i = 0; i < matrices.get(0).length(); i++) {
		final Attribute newAtt = new Attribute("val" + i);
		attributes.add(newAtt);
	}

	final List<String> classValues = IntStream.range(0, refInstances.classAttribute().numValues()).asDoubleStream().mapToObj(String::valueOf).collect(Collectors.toList());
	final Attribute classAtt = new Attribute(CLASS_ATT_NAME, classValues);
	attributes.add(classAtt);

	final Instances result = new Instances(INSTANCES_DS_NAME, attributes, refInstances.size());
	result.setClassIndex(result.numAttributes() - 1);

	for (int i = 0; i < matrices.size(); i++) {

		// Initialize instance
		final Instance inst = new DenseInstance(1, ArrayUtils.addAll(Nd4j.toFlattened(matrices.get(i)).toDoubleVector(), 0));
		inst.setDataset(result);

		// Set class value
		inst.setClassValue(refInstances.get(i).classValue());

		result.add(inst);
	}

	return result;

}
 
Example 11
Source File: DatabaseConnectorImpl.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private Instance createInstance(final IKVStore rs, final List<AbstractFeature> features, final autofe.db.model.database.Attribute target, final Instances instances) {
	Instance instance = new DenseInstance(features.size() + 1);
	instance.setDataset(instances);
	for (int i = 0; i < features.size(); i++) {
		AbstractFeature feature = features.get(i);
		if (feature.getType() == AttributeType.TEXT) {
			if (!rs.isNull(feature.getName())) {
				instance.setValue(i, rs.getAsString(feature.getName()));
			}
		} else if (feature.getType() == AttributeType.NUMERIC) {
			if (!rs.isNull(feature.getName())) {
				instance.setValue(i, rs.getAsLong(feature.getName()));
			}
		} else {
			throw new UnsupportedAttributeTypeException("Unsupported attribute type " + feature.getType());
		}
	}

	// Add class value (last column in result set)
	if (target.getType() == AttributeType.TEXT) {
		instance.setClassValue(rs.getAsString(target.getName()));
	} else if (target.getType() == AttributeType.NUMERIC) {
		instance.setClassValue(rs.getAsDouble(target.getName()));
	} else {
		throw new UnsupportedAttributeTypeException("Unsupported attribute type for target " + target.getType());
	}

	return instance;
}
 
Example 12
Source File: ContractRotationForest.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** 
 * Transforms an instance for the i-th classifier.
 *
 * @param instance the instance to be transformed
 * @param i the base classifier number
 * @return the transformed instance
 * @throws Exception if the instance can't be converted successfully 
 */
protected Instance convertInstance( Instance instance, int i ) 
throws Exception {
  Instance newInstance = new DenseInstance( headers.get(i).numAttributes( ) );
  newInstance.setWeight(instance.weight());
  newInstance.setDataset(headers.get(i));
  int currentAttribute = 0;

  // Project the data for each group
  int[][] g=groups.get(i);
  for( int j = 0; j < g.length; j++ ) {
    Instance auxInstance = new DenseInstance(g[j].length + 1 );
    int k;
    for( k = 0; k < g[j].length; k++ ) {
      auxInstance.setValue( k, instance.value( g[j][k] ) );
    }
    auxInstance.setValue( k, instance.classValue( ) );
    auxInstance.setDataset(reducedHeaders.get(i)[ j ] );
    Filter[] projection=projectionFilters.get(i);
    projection[j].input( auxInstance );
    auxInstance = projection[j].output( );
    projection[j].batchFinished();
    for( int a = 0; a < auxInstance.numAttributes() - 1; a++ ) {
      newInstance.setValue( currentAttribute++, auxInstance.value( a ) );
    }
  }

  newInstance.setClassValue( instance.classValue() );
  return newInstance;
}
 
Example 13
Source File: ClassOrder.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Input an instance for filtering. Ordinarily the instance is processed
  * and made available for output immediately. Some filters require all
  * instances be read before producing output.
  *
  * @param instance the input instance
  * @return true if the filtered instance may now be
  * collected with output().
  * @throws IllegalStateException if no input format has been defined.
  */
 public boolean input(Instance instance) {

   if (getInputFormat() == null) {
     throw new IllegalStateException("No input instance format defined");
   }
   if (m_NewBatch) {
     resetQueue();
     m_NewBatch = false;     
   }	
   
   // In case some one use this routine in testing, 
   // although he/she should not do so
   if(m_Converter != null){
     Instance datum = (Instance)instance.copy();
     if (!datum.isMissing(m_ClassAttribute)){
datum.setClassValue((double)m_Converter[(int)datum.classValue()]);
     }
     push(datum);
     return true;
   }
   
   if (!instance.isMissing(m_ClassAttribute)) {
     m_ClassCounts[(int)instance.classValue()] += instance.weight();
   }

   bufferInput(instance);
   return false;
 }
 
Example 14
Source File: RegressionByDiscretization.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a predicted class for the test instance.
 *
 * @param instance the instance to be classified
 * @return predicted class value
 * @throws Exception if the prediction couldn't be made
 */
public double classifyInstance(Instance instance) throws Exception {  

  // Make sure structure of class attribute correct
  m_Discretizer.input(instance);
  m_Discretizer.batchFinished();
  Instance newInstance = m_Discretizer.output();//(Instance)instance.copy();
  if (m_OldIndexToNewIndex != null) {
    newInstance.setClassValue(m_OldIndexToNewIndex[(int)newInstance.classValue()]);
  }
  newInstance.setDataset(m_DiscretizedHeader);
  double [] probs = m_Classifier.distributionForInstance(newInstance);

  if (!m_MinimizeAbsoluteError) {

    // Compute actual prediction
    double prediction = 0, probSum = 0;
    for (int j = 0; j < probs.length; j++) {
      prediction += probs[j] * m_ClassMeans[j];
      probSum += probs[j];
    }
    
    return prediction /  probSum;
  } else {
  
    // Get density estimator
    UnivariateQuantileEstimator e = (UnivariateQuantileEstimator)getDensityEstimator(instance, true);
    
    // Return estimate
    return e.predictQuantile(0.5);
  }
}
 
Example 15
Source File: RotationForest.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** 
 * Transforms an instance for the i-th classifier.
 *
 * @param instance the instance to be transformed
 * @param i the base classifier number
 * @return the transformed instance
 * @throws Exception if the instance can't be converted successfully 
 */
protected Instance convertInstance( Instance instance, int i ) 
throws Exception {
  Instance newInstance = new DenseInstance( m_Headers[ i ].numAttributes( ) );
  newInstance.setWeight(instance.weight());
  newInstance.setDataset( m_Headers[ i ] );
  int currentAttribute = 0;

  // Project the data for each group
  for( int j = 0; j < m_Groups[i].length; j++ ) {
    Instance auxInstance = new DenseInstance( m_Groups[i][j].length + 1 );
    int k;
    for( k = 0; k < m_Groups[i][j].length; k++ ) {
      auxInstance.setValue( k, instance.value( m_Groups[i][j][k] ) );
    }
    auxInstance.setValue( k, instance.classValue( ) );
    auxInstance.setDataset( m_ReducedHeaders[ i ][ j ] );
    m_ProjectionFilters[i][j].input( auxInstance );
    auxInstance = m_ProjectionFilters[i][j].output( );
    m_ProjectionFilters[i][j].batchFinished();
    for( int a = 0; a < auxInstance.numAttributes() - 1; a++ ) {
      newInstance.setValue( currentAttribute++, auxInstance.value( a ) );
    }
  }

  newInstance.setClassValue( instance.classValue() );
  return newInstance;
}
 
Example 16
Source File: UnionFilter.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
static DataSet union(final DataSet coll1, final DataSet coll2) {
	if (coll1 == null || coll2 == null) {
		throw new IllegalArgumentException("Parameters 'coll1' and 'coll2' must not be null!");
	}

	if (coll1.getIntermediateInstances() == null || coll2.getIntermediateInstances() == null) {
		// Merge Weka instances
		Instances instances1 = coll1.getInstances();
		Instances instances2 = coll2.getInstances();

		if (instances1.numInstances() != instances2.numInstances()) {
			throw new IllegalArgumentException("Data sets to be united must have the same amount of instances!");
		}

		ArrayList<Attribute> attributes = new ArrayList<>(
				coll1.getInstances().numAttributes() + coll2.getInstances().numAttributes() - 1);
		for (int i = 0; i < instances1.numAttributes() - 1; i++) {
			attributes.add(instances1.attribute(i).copy(instances1.attribute(i).name() + "u1"));
		}
		for (int i = 0; i < instances2.numAttributes() - 1; i++) {
			attributes.add(instances2.attribute(i).copy(instances2.attribute(i).name() + "u2"));
		}

		// Add class attribute
		List<String> classValues = IntStream.range(0, instances1.classAttribute().numValues()).asDoubleStream()
				.mapToObj(String::valueOf).collect(Collectors.toList());
		Attribute classAtt = new Attribute("classAtt", classValues);
		attributes.add(classAtt);

		Instances unitedInstances = new Instances("UnitedInstances", attributes, instances1.numInstances());
		unitedInstances.setClassIndex(unitedInstances.numAttributes() - 1);

		for (int i = 0; i < instances1.numInstances(); i++) {
			Instance instance = new DenseInstance(attributes.size());
			instance.setDataset(unitedInstances);

			// Copy values
			int runningIndex = 0;
			for (int j = 0; j < instances1.numAttributes() - 1; j++) {
				instance.setValue(runningIndex++, instances1.get(i).value(j));
			}
			for (int j = 0; j < instances2.numAttributes() - 1; j++) {
				instance.setValue(runningIndex++, instances2.get(i).value(j));
			}
			instance.setClassValue(instances1.get(i).classValue());

			unitedInstances.add(instance);
		}

		return new DataSet(unitedInstances, null);
	} else {
		if (coll1.getIntermediateInstances().isEmpty() || coll2.getIntermediateInstances().isEmpty()) {
			throw new IllegalArgumentException("There must be intermediate instances if the collection is set.");
		}

		// Merge intermediate instances
		List<INDArray> intermediateInsts1 = coll1.getIntermediateInstances();
		List<INDArray> intermediateInsts2 = coll2.getIntermediateInstances();

		List<INDArray> unitedIntermediateInsts = new ArrayList<>(
				(int) (intermediateInsts1.get(0).length() + intermediateInsts2.get(0).length()));
		for (int i = 0; i < intermediateInsts1.size(); i++) {
			INDArray intermediateInst = Nd4j.hstack(intermediateInsts1.get(i).ravel(),
					intermediateInsts2.get(i).ravel());
			unitedIntermediateInsts.add(intermediateInst);
		}

		return new DataSet(coll1.getInstances(), unitedIntermediateInsts);
	}
}
 
Example 17
Source File: Ridor.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Builds a ripple-down manner rule learner.
 *
 * @param instances the training data
 * @throws Exception if classifier can't be built successfully
 */
public void buildClassifier(Instances instances) throws Exception {

  // can classifier handle the data?
  getCapabilities().testWithFail(instances);

  // remove instances with missing class
  Instances data = new Instances(instances);
  data.deleteWithMissingClass();
  
  int numCl = data.numClasses();
  m_Root = new Ridor_node();
  m_Class = instances.classAttribute();     // The original class label
	
  int index = data.classIndex();
  m_Cover = data.sumOfWeights();

  m_Random = new Random(m_Seed);
	
  /* Create a binary attribute */
  FastVector binary_values = new FastVector(2);
  binary_values.addElement("otherClasses");
  binary_values.addElement("defClass");
  Attribute attr = new Attribute ("newClass", binary_values);
  data.insertAttributeAt(attr, index);	
  data.setClassIndex(index);                 // The new class label

  /* Partition the data into bags according to their original class values */
  Instances[] dataByClass = new Instances[numCl];
  for(int i=0; i < numCl; i++)
    dataByClass[i] = new Instances(data, data.numInstances()); // Empty bags
  for(int i=0; i < data.numInstances(); i++){ // Partitioning
    Instance inst = data.instance(i);
    inst.setClassValue(0);           // Set new class vaue to be 0
    dataByClass[(int)inst.value(index+1)].add(inst); 
  }	
	
  for(int i=0; i < numCl; i++)    
    dataByClass[i].deleteAttributeAt(index+1);   // Delete original class
	
  m_Root.findRules(dataByClass, 0);
  
}
 
Example 18
Source File: RegressionByDiscretization.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get density estimator for given instance.
 * 
 * @param inst the instance
 * @return the univariate density estimator
 * @exception Exception if the estimator can't be computed
 */
protected UnivariateDensityEstimator getDensityEstimator(Instance instance, boolean print) throws Exception {

  // Initialize estimator
  UnivariateDensityEstimator e;
  
  if (m_estimatorType == ESTIMATOR_KERNEL) {
    e = new UnivariateKernelEstimator();
  } else if (m_estimatorType == ESTIMATOR_NORMAL) {
    e = new UnivariateNormalEstimator();
  } else {
    e = new UnivariateEqualFrequencyHistogramEstimator();

    // Set the number of bins appropriately
    ((UnivariateEqualFrequencyHistogramEstimator)e).setNumBins(getNumBins());

    // Initialize boundaries of equal frequency estimator
    for (int i = 0; i < m_OriginalTargetValues.length; i++) {
      e.addValue(m_OriginalTargetValues[i], 1.0);
    }
    
    // Construct estimator, then initialize statistics, so that only boundaries will be kept
    ((UnivariateEqualFrequencyHistogramEstimator)e).initializeStatistics();

    // Now that boundaries have been determined, we only need to update the bin weights
    ((UnivariateEqualFrequencyHistogramEstimator)e).setUpdateWeightsOnly(true);      
  }

  // Make sure structure of class attribute correct
  m_Discretizer.input(instance);
  m_Discretizer.batchFinished();
  Instance newInstance = m_Discretizer.output();//(Instance)instance.copy();
  if (m_OldIndexToNewIndex != null) {
    newInstance.setClassValue(m_OldIndexToNewIndex[(int)newInstance.classValue()]);
  }
  newInstance.setDataset(m_DiscretizedHeader);
  double [] probs = m_Classifier.distributionForInstance(newInstance);

  // Add values to estimator
  for (int i = 0; i < m_OriginalTargetValues.length; i++) {
    e.addValue(m_OriginalTargetValues[i], probs[m_NewTargetValues[i]] * 
               m_OriginalTargetValues.length / m_ClassCounts[m_NewTargetValues[i]]);
  }

  // Return estimator
  return e;
}
 
Example 19
Source File: NSR.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
public Instances convertInstances(Instances D, int L) throws Exception {

		//Gather combinations
		HashMap<String,Integer> distinctCombinations = MLUtils.classCombinationCounts(D);
		if(getDebug())
			System.out.println("Found "+distinctCombinations.size()+" unique combinations");

		//Prune combinations
		MLUtils.pruneCountHashMap(distinctCombinations,m_P);
		if(getDebug())
			System.out.println("Pruned to "+distinctCombinations.size()+" with P="+m_P);

		// Remove all class attributes
		Instances D_ = MLUtils.deleteAttributesAt(new Instances(D),MLUtils.gen_indices(L));
		// Add a new class attribute
		D_.insertAttributeAt(new Attribute("CLASS", new ArrayList(distinctCombinations.keySet())),0); // create the class attribute
		D_.setClassIndex(0);

		//Add class values
		for (int i = 0; i < D.numInstances(); i++) {
			String y = MLUtils.encodeValue(MLUtils.toIntArray(D.instance(i),L));
			// add it
			if(distinctCombinations.containsKey(y)) 	//if its class value exists
				D_.instance(i).setClassValue(y);
			// decomp
			else if(m_N > 0) { 
				String d_subsets[] = SuperLabelUtils.getTopNSubsets(y, distinctCombinations, m_N);
				for (String s : d_subsets) {
					int w = distinctCombinations.get(s);
					Instance copy = (Instance)(D_.instance(i)).copy();
					copy.setClassValue(s);
					copy.setWeight(1.0 / d_subsets.length);
					D_.add(copy);
				}
			}
		}

		// remove with missing class
		D_.deleteWithMissingClass();

		// keep the header of new dataset for classification
		m_InstancesTemplate = new Instances(D_, 0);

		if (getDebug())
			System.out.println(""+D_);

		return D_;
	}
 
Example 20
Source File: RT.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
  	testCapabilities(D);
  	
	int L = D.classIndex();

	//Create header
	Instances D_ = new Instances(D,0,0);

	//Delete the old class attributes
	for (int j = 0; j < L; j++)
		D_.deleteAttributeAt(0); 

	//Make the new class attribute
	FastVector classes = new FastVector(L);
	for (int j = 0; j < L; j++)
		classes.addElement("C"+j);

	//Add the new class attribute
	D_.insertAttributeAt(new Attribute("ClassY",classes),0);
	D_.setClassIndex(0);

	//Loop through D again
	for (int i = 0; i < D.numInstances(); i++) {
		for (int j = 0; j < L; j++) {
			if((int)D.instance(i).value(j) > 0) {
				// make a copy here ...
				Instance x_ = (Instance)D.instance(i).copy();
				x_.setDataset(null);
				// make it multi-class, and set the appropriate class value ...
				for (int k = 1; k < L; k++)
					x_.deleteAttributeAt(1); 
				x_.setDataset(D_);
				x_.setClassValue(j); // (*) this just ponts to the right index
				D_.add(x_);
			}
		}
	}

	//Save the template
	m_InstancesTemplate = new Instances(D_,0);

	//Build
	if(getDebug())  System.out.println("Building classifier "+m_Classifier.getClass().getName()+" on "+D_.numInstances()+" instances (originally "+D.numInstances()+")");
	m_Classifier.buildClassifier(D_);

}