我想使用序列化分类器对新实例进行分类.我找到了这堂课,但我听不懂.
arg [2] =类属性名称,arg [3] =基于实例的索引(从原始数据集中进行预测)
这是此类的代码:
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:
*
*
serialized model*
ARFF file*
class attribute name*
1-based index of an instance to predict from original dataset*
*/
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()));
}
}
提前致谢.