Java Code Examples for weka.classifiers.Classifier#classifyInstance()

The following examples show how to use weka.classifiers.Classifier#classifyInstance() . 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: AdditiveRegression.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Replace the class values of the instances from the current iteration
  * with residuals ater predicting with the supplied classifier.
  *
  * @param data the instances to predict
  * @param c the classifier to use
  * @param useShrinkage whether shrinkage is to be applied to the model's output
  * @return a new set of instances with class values replaced by residuals
  * @throws Exception if something goes wrong
  */
 private Instances residualReplace(Instances data, Classifier c, 
			    boolean useShrinkage) throws Exception {
   double pred,residual;
   Instances newInst = new Instances(data);

   for (int i = 0; i < newInst.numInstances(); i++) {
     pred = c.classifyInstance(newInst.instance(i));
     if (useShrinkage) {
pred *= getShrinkage();
     }
     residual = newInst.instance(i).classValue() - pred;
     newInst.instance(i).setClassValue(residual);
   }
   //    System.err.print(newInst);
   return newInst;
 }
 
Example 2
Source File: PredictByDomainOS.java    From recon with GNU General Public License v2.0 6 votes vote down vote up
private static boolean predictOneFlow(String line, String domainOS) {
	if (!domainOSModel.containsKey(domainOS))
		return false;
	else {
		try {
			Classifier classifier = domainOSModel.get(domainOS);
			Map<String, Integer> fi = domainOSFeature.get(domainOS);
			Instances structure = domainOSStruct.get(domainOS);
			Instance current = getInstance(line, fi, fi.size());

			Instances is = new Instances(structure);
			is.setClassIndex(is.numAttributes() - 1);
			is.add(current);
			current = is.get(is.size() - 1);
			current.setClassMissing();
			double predicted = classifier.classifyInstance(current);
			if (predicted > 0) {
				return true;
			} else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	return false;
}
 
Example 3
Source File: DecisionTree.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Classifier j48 = new J48();
    Instances trainingData = GenerateTestVessels.getData();
    j48.buildClassifier(trainingData);
    System.out.println(j48);



    double[] vesselUnderTest = GenerateTestVessels.getBarco(5);

    DenseInstance inst = new DenseInstance(1.0,vesselUnderTest);
    inst.setDataset(trainingData);
    inst.setClassMissing();
    System.out.println(inst);

    double result = j48.classifyInstance(inst);
    System.out.println(GenerateTestVessels.types[(int)result]);

    SerializationHelper.write(new FileOutputStream("tmp"), j48);
    J48 j48Read = (J48)SerializationHelper.read(new FileInputStream("tmp"));
}
 
Example 4
Source File: TriangleFlipper.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
 * returns the (possibly) new class label
 * 
 * @param c           the Classifier to use for prediction
 * @param instances   the instances to use for flipping
 * @param from        the starting of flipping
 * @param count       the number of instances to flip
 * @param index       the index of the instance to flip
 * @param history     the flipping history
 * @return            the (possibly) new class label
 */
@Override
public double flipLabel( Classifier c, Instances instances, 
                         int from, int count, int index,
                         FlipHistory history ) {
  double          result;
  double[]        dist;
  double          prob;
  double          rand;
  double          threshold;
  
  try {
    result = c.classifyInstance(instances.instance(index));
    dist   = c.distributionForInstance(instances.instance(index));
  } 
  catch (Exception e) {
    e.printStackTrace();
    return instances.instance(index).classValue();
  }
  
  prob = dist[(int) result];
  
  // flip label
  rand      = m_Random.nextDouble();
  threshold = StrictMath.max(5.0 / count, 1 - 2*(prob - 0.5));
  if (rand < threshold) {
    if (Utils.eq(result, 0.0))
      result = 1;
    else
      result = 0;
  }

  // history
  history.add(instances.instance(index), dist);

  return result;
}
 
Example 5
Source File: CollectiveInstances.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
 * calculates the accuracy for original test and train set 
 * @param c		the classifier to use for determining the RMS
 * @param train	the training set
 * @param test	the original test set
 * @return		the accuracy array (contains AccTrain, AccTestOriginal)
 * @throws Exception 	if something goes wrong
 */
public static double[] calculateAccuracy( Classifier c, 
                                          Instances train, 
                                          Instances test )
  throws Exception {

  int         i;
  double      classValue;
  double[]    result;
  
  result = new double[2];
  
  // 1. train
  result[0] = 0;
  for (i = 0; i < train.numInstances(); i++) {
    classValue = c.classifyInstance(train.instance(i));
    if (Utils.eq(classValue, train.instance(i).classValue()))
      result[0] += 1.0;
  }

  // 2. test
  result[1] = 0;
  if (test != null) {
    for (i = 0; i < test.numInstances(); i++) {
      classValue = c.classifyInstance(test.instance(i));
      if (Utils.eq(classValue, test.instance(i).classValue()))
        result[1] += 1.0;
    }
  }
  
  // normalize
  result[0] = result[0] / train.numInstances();
  if (test != null)
    result[1] = result[1] / test.numInstances();

  return result;
}
 
Example 6
Source File: Tools.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
/**
 * get the list of classification errors for each instance in the dataset
 * @param c the classifier
 * @param i the dataset
 * @return the list of errors
 * @throws Exception
 */
public static double[] get_errors(Classifier c, Instances i) throws Exception{
	double[] computed = new double[i.numInstances()];
	for(int m = 0;m<computed.length;m++){
		double s = c.classifyInstance(i.instance(m));
		double r = i.instance(m).value(i.classAttribute());
		computed[m] = (r-s)*(r-s); 
	}
	return computed;
}
 
Example 7
Source File: Tools.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
/**
 * get the average error of the classifier over the given dataset
 * @param c the classifier
 * @param i the dataset
 * @return the average error
 * @throws Exception
 */
public static double get_avg_error(Classifier c, Instances i) throws Exception{
	double computed = 0;
	for(int m = 0;m<i.numInstances();m++){
		double s = c.classifyInstance(i.instance(m));
		double r = i.instance(m).value(i.classAttribute());
		computed += (r-s)*(r-s); 
	}
	return computed/i.numInstances();
}
 
Example 8
Source File: Tools.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
/**
 * add a new feature in the dataset containing the predicted values by the classifier
 * @param c the classifier
 * @param i the dataset
 * @throws Exception
 */
public static void add_predictions(Classifier c, Instances i) throws Exception{
	
	double[] computed = new double[i.numInstances()];
	for(int m = 0;m<computed.length;m++){
		computed[m] = c.classifyInstance(i.instance(m)); 
	}
	Attribute a = new Attribute("interpolate");
	int num = i.numAttributes();
	i.insertAttributeAt(a, num);
	for(int m = 0;m<computed.length;m++){
		i.instance(m).setValue(num, computed[m]);
	}
}
 
Example 9
Source File: SimulationExperiments.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** Runs a single fold experiment, saving all output. 
 * 
 * @param train
 * @param test
 * @param c
 * @param sample
 * @param preds
 * @return 
 */
    public static double singleSampleExperiment(Instances train, Instances test, Classifier c, int sample,String preds){
        double acc=0;
        OutFile p=new OutFile(preds+"/testFold"+sample+".csv");

// hack here to save internal CV for further ensembling   
        if(EnhancedAbstractClassifier.classifierAbleToEstimateOwnPerformance(c))
            ((EnhancedAbstractClassifier)c).setEstimateOwnPerformance(true);
        if(c instanceof SaveableEnsemble)
           ((SaveableEnsemble)c).saveResults(preds+"/internalCV_"+sample+".csv",preds+"/internalTestPreds_"+sample+".csv");
        try{              
            c.buildClassifier(train);
            if(EnhancedAbstractClassifier.classifierIsEstimatingOwnPerformance(c))
                ((EnhancedAbstractClassifier)c).getTrainResults().writeFullResultsToFile(preds+"/trainFold"+sample+".csv");
            
            int[][] predictions=new int[test.numInstances()][2];
            for(int j=0;j<test.numInstances();j++){
                predictions[j][0]=(int)test.instance(j).classValue();
                test.instance(j).setMissing(test.classIndex());//Just in case ....
            }
            for(int j=0;j<test.numInstances();j++)
            {
                predictions[j][1]=(int)c.classifyInstance(test.instance(j));
                if(predictions[j][0]==predictions[j][1])
                    acc++;
            }
            acc/=test.numInstances();
            String[] names=preds.split("/");
            p.writeLine(names[names.length-1]+","+c.getClass().getName()+",test");
            if(c instanceof EnhancedAbstractClassifier)
                p.writeLine(((EnhancedAbstractClassifier)c).getParameters());
            else if(c instanceof SaveableEnsemble)
                p.writeLine(((SaveableEnsemble)c).getParameters());
            else
                p.writeLine("NoParameterInfo");
            p.writeLine(acc+"");
            for(int j=0;j<test.numInstances();j++){
                p.writeString(predictions[j][0]+","+predictions[j][1]+",");
                double[] dist =c.distributionForInstance(test.instance(j));
                for(double d:dist)
                    p.writeString(","+d);
                p.writeString("\n");
            }
        }catch(Exception e)
        {
                System.out.println(" Error ="+e+" in method simpleExperiment"+e);
                e.printStackTrace();
                System.out.println(" TRAIN "+train.relationName()+" has "+train.numAttributes()+" attributes and "+train.numInstances()+" instances");
                System.out.println(" TEST "+test.relationName()+" has "+test.numAttributes()+" attributes and "+test.numInstances()+" instances");

                System.exit(0);
        }
         return acc;
    }
 
Example 10
Source File: SimulationExperiments.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public static double singleSampleExperiment(Instances train, Instances test, Classifier c, int sample,String preds){
        double acc=0;
        OutFile p=new OutFile(preds+"/testFold"+sample+".csv");

// hack here to save internal CV for further ensembling   
//        if(c instanceof TrainAccuracyEstimate)
//            ((TrainAccuracyEstimate)c).writeCVTrainToFile(preds+"/trainFold"+sample+".csv");
        if(c instanceof SaveableEnsemble)
           ((SaveableEnsemble)c).saveResults(preds+"/internalCV_"+sample+".csv",preds+"/internalTestPreds_"+sample+".csv");
        try{              
            c.buildClassifier(train);
            int[][] predictions=new int[test.numInstances()][2];
            for(int j=0;j<test.numInstances();j++){
                predictions[j][0]=(int)test.instance(j).classValue();
                test.instance(j).setMissing(test.classIndex());//Just in case ....
            }
            for(int j=0;j<test.numInstances();j++)
            {
                predictions[j][1]=(int)c.classifyInstance(test.instance(j));
                if(predictions[j][0]==predictions[j][1])
                    acc++;
            }
            acc/=test.numInstances();
            String[] names=preds.split("/");
            p.writeLine(names[names.length-1]+","+c.getClass().getName()+",test");
            if(c instanceof EnhancedAbstractClassifier)
                p.writeLine(((EnhancedAbstractClassifier)c).getParameters());
            else if(c instanceof SaveableEnsemble)
                p.writeLine(((SaveableEnsemble)c).getParameters());
            else
                p.writeLine("NoParameterInfo");
            p.writeLine(acc+"");
            for(int j=0;j<test.numInstances();j++){
                p.writeString(predictions[j][0]+","+predictions[j][1]+",");
                double[] dist =c.distributionForInstance(test.instance(j));
                for(double d:dist)
                    p.writeString(","+d);
                p.writeString("\n");
            }
        }catch(Exception e)
        {
                System.out.println(" Error ="+e+" in method simpleExperiment"+e);
                e.printStackTrace();
                System.out.println(" TRAIN "+train.relationName()+" has "+train.numAttributes()+" attributes and "+train.numInstances()+" instances");
                System.out.println(" TEST "+test.relationName()+" has "+test.numAttributes()+" attributes and "+test.numInstances()+" instances");

                System.exit(0);
        }
         return acc;
    }
 
Example 11
Source File: CollectiveForest.java    From collective-classification-weka-package with GNU General Public License v3.0 4 votes vote down vote up
/**
 * performs the actual building of the classifier
 * 
 * @throws Exception if building fails
 */
@Override
protected void buildClassifier() throws Exception {
  Classifier        tree;
  int               i;
  int               n;
  int               nextSeed;
  double[]          dist;
  Instances         bagData;
  boolean[]         inBag;
  double            outOfBagCount;
  double            errorSum;
  Instance          outOfBagInst;

  m_PureTrainNodes = 0;
  m_PureTestNodes  = 0;
  
  for (i = 0; i < getNumTrees(); i++) {
    // info
    if (getVerbose())
      System.out.print(".");

    // get next seed number
    nextSeed = m_Random.nextInt();

    // bagging?
    if (getUseBagging()) {
      // inBag-dataset/array
      inBag   = new boolean[m_TrainsetNew.numInstances()];
      bagData = resample(m_TrainsetNew, nextSeed, inBag);
      
      // build i.th tree
      tree = initClassifier(nextSeed);

      // determine and store distributions
      for (n = 0; n < m_TestsetNew.numInstances(); n++) {
        dist = tree.distributionForInstance(m_TestsetNew.instance(n));
        m_List.addDistribution(m_TestsetNew.instance(n), dist);
      }

      // determine out-of-bag-error
      outOfBagCount = 0;
      errorSum      = 0;

      for (n = 0; n < inBag.length; n++) {  
        if (!inBag[n]) {
          outOfBagInst = m_TrainsetNew.instance(n);
          outOfBagCount += outOfBagInst.weight();
          if (m_TrainsetNew.classAttribute().isNumeric()) {
            errorSum += outOfBagInst.weight() *
              StrictMath.abs(tree.classifyInstance(outOfBagInst)
                       - outOfBagInst.classValue());
          } 
          else {
            if (tree.classifyInstance(outOfBagInst) 
                  != outOfBagInst.classValue()) {
              errorSum += outOfBagInst.weight();
            }
          }
        }
      }

      m_OutOfBagError = errorSum / outOfBagCount;
    }
    else {
      // build i.th tree
      tree = initClassifier(nextSeed);

      // determine and store distributions
      for (n = 0; n < m_TestsetNew.numInstances(); n++) {
        dist = tree.distributionForInstance(m_TestsetNew.instance(n));
        m_List.addDistribution(m_TestsetNew.instance(n), dist);
      }
    }

    // get information about pure nodes
    try {
      if (tree instanceof AdditionalMeasureProducer) {
        m_PureTrainNodes += ((AdditionalMeasureProducer) tree).getMeasure(
                                "measurePureTrainNodes");
        m_PureTestNodes  += ((AdditionalMeasureProducer) tree).getMeasure(
                                "measurePureTestNodes");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    tree = null;
  }
    
  if (getVerbose())
    System.out.println();
}
 
Example 12
Source File: DecisionTreeEstimator.java    From jMetal with MIT License 4 votes vote down vote up
public double doPrediction(int index,S testSolution) {
  double result = 0.0d;

  try {
    int numberOfObjectives = solutionList.get(0).getNumberOfObjectives();
    //Attributes
    //numeric
    Attribute attr = new Attribute("my-numeric");

    //nominal
    ArrayList<String> myNomVals = new ArrayList<>();

    for (int i=0; i<numberOfObjectives; i++)
      myNomVals.add(VALUE_STRING+i);
    Attribute attr1 = new Attribute(NOMINAL_STRING, myNomVals);
    //System.out.println(attr1.isNominal());

    //string
    Attribute attr2 = new Attribute(MY_STRING, (List<String>)null);
    //System.out.println(attr2.isString());

    //2.create dataset
    ArrayList<Attribute> attrs = new ArrayList<>();
    attrs.add(attr);
    attrs.add(attr1);
    attrs.add(attr2);
    Instances dataset = new Instances("my_dataset", attrs, 0);

    //Add instances
    for (S solution : solutionList) {
      //instaces
      for (int i = 0; i <numberOfObjectives ; i++) {
        double[] attValues = new double[dataset.numAttributes()];
        attValues[0] = solution.getObjective(i);
        attValues[1] = dataset.attribute(NOMINAL_STRING).indexOfValue(VALUE_STRING+i);
        attValues[2] = dataset.attribute(MY_STRING).addStringValue(solution.toString()+i);
        dataset.add(new DenseInstance(1.0, attValues));
      }
    }


    //DataSet test
    Instances datasetTest = new Instances("my_dataset_test", attrs, 0);

    //Add instances
    for (int i = 0; i < numberOfObjectives; i++) {
      Instance test = new DenseInstance(3);
      test.setValue(attr, testSolution.getObjective(i));
      test.setValue(attr1, VALUE_STRING+i);
      test.setValue(attr2, testSolution.toString()+i);
      datasetTest.add(test);
    //  dataset.add(test);
    }


    //split to 70:30 learn and test set

    //Preprocess strings (almost no classifier supports them)
    StringToWordVector filter = new StringToWordVector();

    filter.setInputFormat(dataset);
    dataset = Filter.useFilter(dataset, filter);

    //Buid classifier
    dataset.setClassIndex(1);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);
    //resample if needed
    //dataset = dataset.resample(new Random(42));
    dataset.setClassIndex(1);
    datasetTest.setClassIndex(1);
    //do eval
    Evaluation eval = new Evaluation(datasetTest); //trainset
    eval.evaluateModel(classifier, datasetTest); //testset
    result = classifier.classifyInstance(datasetTest.get(index));
  } catch (Exception e) {
    result = testSolution.getObjective(index);
  }
  return result;
}
 
Example 13
Source File: DecisionTreeEstimator.java    From jMetal with MIT License 4 votes vote down vote up
public double doPredictionVariable(int index,S testSolution) {
  double result = 0.0d;

  try {
    int numberOfVariables = solutionList.get(0).getNumberOfVariables();
    //Attributes
    //numeric
    Attribute attr = new Attribute("my-numeric");

    //nominal
    ArrayList<String> myNomVals = new ArrayList<>();

    for (int i=0; i<numberOfVariables; i++)
      myNomVals.add(VALUE_STRING+i);
    Attribute attr1 = new Attribute(NOMINAL_STRING, myNomVals);

    //string
    Attribute attr2 = new Attribute(MY_STRING, (List<String>)null);

    //2.create dataset
    ArrayList<Attribute> attrs = new ArrayList<>();
    attrs.add(attr);
    attrs.add(attr1);
    attrs.add(attr2);
    Instances dataset = new Instances("my_dataset", attrs, 0);

    //Add instances
    for (S solution : solutionList) {
      //instaces
      for (int i = 0; i <numberOfVariables ; i++) {
        double[] attValues = new double[dataset.numAttributes()];
        attValues[0] = ((DoubleSolution)solution).getVariable(i);
        attValues[1] = dataset.attribute(NOMINAL_STRING).indexOfValue(VALUE_STRING+i);
        attValues[2] = dataset.attribute(MY_STRING).addStringValue(solution.toString()+i);
        dataset.add(new DenseInstance(1.0, attValues));
      }
    }


    //DataSet test
    Instances datasetTest = new Instances("my_dataset_test", attrs, 0);

    //Add instances
    for (int i = 0; i < numberOfVariables; i++) {
      Instance test = new DenseInstance(3);
      test.setValue(attr, ((DoubleSolution)testSolution).getVariable(i));
      test.setValue(attr1, VALUE_STRING+i);
      test.setValue(attr2, testSolution.toString()+i);
      datasetTest.add(test);
      //  dataset.add(test);
    }


    //split to 70:30 learn and test set

    //Preprocess strings (almost no classifier supports them)
    StringToWordVector filter = new StringToWordVector();

    filter.setInputFormat(dataset);
    dataset = Filter.useFilter(dataset, filter);

    //Buid classifier
    dataset.setClassIndex(1);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);
    //resample if needed
    //dataset = dataset.resample(new Random(42));
    dataset.setClassIndex(1);
    datasetTest.setClassIndex(1);
    //do eval
    Evaluation eval = new Evaluation(datasetTest); //trainset
    eval.evaluateModel(classifier, datasetTest); //testset
    result = classifier.classifyInstance(datasetTest.get(index));
  } catch (Exception e) {
    result = ((DoubleSolution)testSolution).getVariable(index);
  }
  return result;
}