我有以下代码,其中我在训练数据集上应用了分类器,我想将其保存(使用交叉验证)作为模型。并将其再次应用于测试数据集。
import weka.core.Instances;
import weka.classifiers.trees.J48;
import weka.classifiers.Evaluation;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader ;
public class EECS_738
{
public static void main(String[] args) throws Exception {
// training
BufferedReader reader = null;
reader=new BufferedReader(new FileReader("/Users/Sumiah/Desktop/ProjectTraining&TestingData/project/EECS738_Train_Project.arff"));
Instances train =new Instances (reader);
train.setClassIndex(0);
reader.close();
J48 j48 = new J48();
j48.buildClassifier(train);
Evaluation eval = new Evaluation(train);
eval.crossValidateModel(j48, train, 10 , new Random(1));
System.out.println(eval.toSummaryString("\n Results \n=====\n",true));
System.out.println(eval.fMeasure(1)+" "+eval.precision(1)+" "+eval.recall(1)+" ");
}
}
答案 0 :(得分:4)
对于大于3.5.5的版本,它很简单:
J48 j48 = new J48();
j48.buildClassifier(train);
weka.core.SerializationHelper.write("/some/where/j48.model", j48);
加载只是:
Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model");
有关详细信息以及如何在旧版本中执行此操作,请参阅here。