开发者

how classify a new instance using serialized classifier in java application

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(a开发者_开发技巧rgs[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.


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.


Most classifiers require a set of training data before prediction. This training data is used to create a model which can then be used to make classifications. It looks like all that is happening here is that they are reading in the model that has been serialized and then making a prediction off of it. That is, it's likely the used a ObjectOutputStream (http://download.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html) after churning through some training data to create the classifier.

If this didn't answer what you're confused about, please clarify what you're looking for a little further.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜