我想使用序列化分类器对新实例进行分类。我找到了这门课,但我不明白。
arg[2]
= 类属性名称和arg[3]
= 从原始数据集预测的实例的基于 1 的索引
这是这个类的代码:
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()));
}
}
提前致谢。