How to get predication value for an instance in weka?

一个人想着一个人 提交于 2019-11-27 20:53:51

The following code takes in a set of training instances, and outputs the predicted probability for a specific 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 method "distributionForInstance" only works for classifiers capable of outputting distribution predictions. You can read up on it here.

I think I found the solution.

The training set and the test set must be equal: same header, same name of attributes, same order. Only changes the numbers. And the question is: why do I have to put the class in the test set if I don’t know it, and precisely it is what I want to obtain? It seems that the method needs something on that, but when you have a look at classModel.distributionForInstance(dataModel.instance(0)) , it gives you the prediction on your classes with an array of double. So, it is necessary to put some values of the classes in the test set, and later the ‘distributionForInstance’ gives you the real result for your classes.

From WEKA GUI, Classify panel -> press the "More options..." -> Output predictions -> Choose "PlainText" option. Now, left-click on "PlainText" and turn the "outputDistribution" into "True".

Note that, this process can be performed in last WEKA versions, e.g., WEKA 3.8.0.

Regards,
Martin

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