How to get prediction value for instance in weka?

I am working on Weka and should output predicate values โ€‹โ€‹(probabilities) for each label for each test instance.

There is an option in the tab as classification in the graphical interface (classify โ†’ options โ†’ Output, the predicted value) that does this, displaying the prediction probabilities for each label, but how to do it in Java code. I want to get probability estimates for each label after its classification?

+7
java weka
source share
3 answers

The following code takes a set of training instances and displays the predicted probability for a particular instance.

import weka.classifiers.trees.J48; import weka.core.Instances; public class Main { public static void main(String[] args) throws Exception { //load training instances Instances test=... //build a J48 decision tree J48 model=new J48(); model.buildClassifier(test); //decide which instance you want to predict int s1=2; //get the predicted probabilities double[] prediction=model.distributionForInstance(test.get(s1)); //output predictions for(int i=0; i<prediction.length; i=i+1) { System.out.println("Probability of class "+ test.classAttribute().value(i)+ " : "+Double.toString(prediction[i])); } } } 

The "distributionForInstance" method works only for classifiers that can output distribution predictions. You can read it here .

+12
source share

I think I found a solution.

The training set and the test set must be equal: the same title, the same attribute name, the same order. Changes only numbers. And the question arises: why do I need to put the class in a test suite if I do not know it, and that is exactly what I want to get? This method seems to need it, but when you look at classModel.distributionForInstance(dataModel.instance(0)) , it gives you a prediction on your classes with a double array. Thus, you need to put some class values โ€‹โ€‹in a test case, and then 'distributionForInstance' gives you the real result for your classes.

+1
source share

From the WEKA GUI, the "Classification" panel โ†’ click "More options ..." โ†’ Output forecasts โ†’ Select "PlainText". Now left-click on โ€œPlainTextโ€ and turn โ€œoutputDistributionโ€ to โ€œTrueโ€.

Please note that this process can be performed in the latest versions of WEKA, for example WEKA 3.8.0.

Yours faithfully,
Martin

-one
source share

All Articles