weka: how to get class name from testing single instance

走远了吗. 提交于 2019-12-07 15:22:09

问题


i want to make a single test for single instance

i use j48 in FilteredClassifier like this:

Remove rm = new Remove();
rm.setAttributeIndices("1"); // remove 1st attribute
// classifier
J48 j48 = new J48();
j48.setUnpruned(true); // using an unpruned J48

// meta-classifier
FilteredClassifier fc_J48 = new FilteredClassifier();
fc_J48.setFilter(rm);
fc_J48.setClassifier(j48);
tdta.dataSet.setClassIndex(tdta.dataSet.numAttributes() - 1);
fc_J48.buildClassifier(tdta.dataSet);

now, i try those options:

j48.classifyInstance(dataSet.instance(1))

or

eval.evaluateModelOnce(j48, dataSet.instance(1))

i think it's will be the same result.

my question is: when i get the double number, how can i translate it to the class name ?


回答1:


Try this:

System.out.println(dataSet.classAttribute().value((int) j48.classifyInstance(dataSet.instance(1)));

Take a look at: http://weka.8497.n7.nabble.com/Predicting-in-java-td27363.html




回答2:


for (int i = 0; i < test.numInstances(); i++) {
  double pred = fc.classifyInstance(test.instance(i));
  System.out.print("ID: " + test.instance(i).value(0));
  System.out.print(", actual: " + test.classAttribute().value((int)         test.instance(i).classValue()));
 System.out.println(", predicted: " + test.classAttribute().value((int) pred));

}

test is testInstances, so if you have a single instance you can replace test.instance(i) with your instance.



来源:https://stackoverflow.com/questions/16188490/weka-how-to-get-class-name-from-testing-single-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!