Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how classify a new instance using serialized classifier in java application

Tags:

java

weka

I want to classify a new instance using serialized classifier. I found this class but I don't understand it.

arg[2] = class attribute name and arg[3] = 1-based index of an instance to predict from original dataset

Here is the code of this class:

import weka.core.*;
import weka.classifiers.*;

import java.io.*;

/**
 * A little class for testing deserialization and prediction.
 * 
 * @author FracPete (fracpet at waikat dot ac dot nz)
 */
public class Blah {

   /**
    * Takes 4 arguments:
    * <ol>
    *   <li>serialized model</li>
    *   <li>ARFF file</li>
    *   <li>class attribute name</li>
    *   <li>1-based index of an instance to predict from original dataset</li>
    * </ol>
    */
   public static void main(String[] args) throws Exception {
      // read the arff training file
      BufferedReader reader = new BufferedReader(new FileReader(args[1]));
      Instances in = new Instances(reader);
      in.setClass(in.attribute(args[2]));

      // instance to classify
      int index = Integer.parseInt(args[3]) - 1;
      Instance toClassifyInstance = (Instance) in.instance(index).copy();
      toClassifyInstance.setClassValue(Instance.missingValue());

      // deserialize model
      Classifier cls = null;
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(args[0]));
      cls = (Classifier) ois.readObject();
      ois.close();

      // PREDICTION
      double clsLabel = cls.classifyInstance(toClassifyInstance);
      String classLabel = in.classAttribute().value((int) clsLabel);

      System.out.println(classLabel + " =?= " + in.instance(index).stringValue(in.classIndex()));
   }
}

Thanks in advance.

like image 694
WOW Avatar asked Nov 21 '25 07:11

WOW


1 Answers

The first parameter, args[0], is the pathname of a serialized classifier that is going to be used for classification. Next parameter is the path of data set which the Instances constructor expects to be in an arff file. This set must have features that are compatible with those in the training data used when creating the serialized classifier (so, the exact same features in the same order). args[2] is the name of the attribute which is the class attribute in the data set from the arff and args[3] is the the index plus one of the instance which will have a copy of itself classified after the value of the class label has been set to missing.

If you are trying to classify an "external" instance eg. on you have built in some code, the instance still has to have a link to some compatible data set before classifying. This can be done using the method setDataset(Instances) on an instance. There is no compatibility check done, so you might want to check with checkInstance(Instance) on an instances.

like image 54
storm Avatar answered Nov 23 '25 22:11

storm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!