我有一个从weka
分类器获得的模型,我想在java代码中测试它,但是当我读取实例时,会出现错误:
Exception in thread "main" java.io.IOException: keyword @relation expected, read Token[Word], line 1
at weka.core.Instances.errms(Instances.java:1863)
at weka.core.Instances.readHeader(Instances.java:1740)
at weka.core.Instances.<init>(Instances.java:119)
at licenta1.LoadModelWeka.main(LoadModelWeka.java:18)
我的代码是:
package licenta1;
import weka.core.Instances;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.trees.J48;
import weka.classifiers.Evaluation;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader ;
public class LoadModelWeka
{
public static void main(String[] args) throws Exception {
// training
BufferedReader reader = null;
reader=new BufferedReader(new FileReader("D:\\aaaaaaaaaaaaaaaaaaaaaa\\Licenta\\BioArtLicTrainSetTask1.csv"));
Instances train =new Instances (reader);
train.setClassIndex(0);
reader.close();
NaiveBayes nb = new NaiveBayes();
nb.buildClassifier(train);
Evaluation eval = new Evaluation(train);
eval.crossValidateModel(nb, 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)+" ");
}
}
有人能帮助我吗?
Mt训练集采用.csv
格式
答案 0 :(得分:0)
您可以尝试
,而不是使用Buffered ReaderDataSource source = new DataSource("/some/where/data.arff");
有关详细信息,请访问此链接http://weka.wikispaces.com/Use+WEKA+in+your+Java+code
答案 1 :(得分:0)
此代码段可用于直接加载csv内容并将其转换为实例。通常.arff用于weka操作,并且此加载程序直接将csv文件在内部转换为arff,然后转换为Instances类。
CSVLoader loader = new CSVLoader();
loader.setSource(new File("filename.csv"));
Instances trainingDataSet = loader.getDataSet();