我已经在svm
中训练了smo
模型,并使用SerializationHelper将其写入文件。
当我尝试读取模型进行分类时,会出现以下错误:
发现问题:java.io.StreamCorruptedException:无效的流标题:47656E65
以下是给出错误的行:
classifier = (Classifier) SerializationHelper.read(strModel);
编辑:
Instances inst = Filter.useFilter(data, vectorizer);
inst.setClassIndex(0);
SMO svm = new SMO();
RBFKernel kernal = new RBFKernel();
svm.setKernel(kernal);
svm.buildClassifier(filteredDate);
SerializationHelper.write("SMO_samp.model", svm);
答案 0 :(得分:0)
对于错误,this可能会有所帮助。
这是一个完全有效的例子:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.functions.LibSVM;
import weka.classifiers.functions.SMO;
import weka.core.Instances;
import weka.core.SerializationHelper;
public class CodeTest {
public static void main(String[] args) throws Exception{
BufferedReader breader = null;
breader = new BufferedReader(new FileReader("iris.arff"));
Instances train_data = new Instances (breader);
breader.close();
train_data.setClassIndex(train_data.numAttributes() -1);
SMO smo = new SMO();
smo.buildClassifier(train_data);
SerializationHelper s = new SerializationHelper();
s.write("SMO_samp.model", smo);
Classifier c = new SMO();
c = (Classifier) s.read("SMO_Samp.model");
Evaluation evaluation = null;
evaluation = new Evaluation (train_data);
evaluation.crossValidateModel(c, train_data, 10, new Random(1));
System.out.println(evaluation.toSummaryString());
}
}