Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on Weka and need to output the predication values (probabilities) of each labels for each test instance.

In GUI there is an option in classify tab as (classify -> options -> Output predicted value) which does this work by outputting the prediction probabilities for each label but how to do this in java code. I want to receive probability scores for each label after classifying it ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
136 views
Welcome To Ask or Share your Answers For Others

1 Answer

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&#60prediction.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...