10
$\begingroup$

I am developing a prediction model using Java Weka api. I can predict class for the new instance using the following code:

double predictClass = classifer.classifyInstance(instance)

However, I need class probability instead of class value. Thanks in advance for your support.

$\endgroup$

2 Answers 2

9
$\begingroup$

Welcome to the community! You can replace your code by the following code.

double[] prediction=classifier.distributionForInstance(instance);

    for (int k<prediction.length; k++){
    System.out.println("Probability of class "+
      trains.classAttribute().value(k)+
       " : "+Double.toString(prediction[k]));
      }

This loop prints all the four values.
Hope it will help you.

$\endgroup$
4
  • 1
    $\begingroup$ Thanks for your quick response. But, I do not understand what classifier.distributionForInstance(instance) return exactly $\endgroup$
    – Howa Begum
    Nov 6, 2018 at 0:57
  • 1
    $\begingroup$ @HowaBegum you can see the return value by printing content of variable prediction. It will return four values such as class o, prediction probability, class 1, prediction probability. Hope you understand $\endgroup$
    – Reja
    Nov 6, 2018 at 1:05
  • 1
    $\begingroup$ Thanks again for your help. It really help me a lot. $\endgroup$
    – Howa Begum
    Nov 6, 2018 at 1:16
  • 1
    $\begingroup$ I am glad it helped. $\endgroup$
    – Reja
    Dec 4, 2018 at 1:03
1
$\begingroup$

It is work good, thanks a lot.There are some correction:

    //Dont forget create new Instance for prediction.
    DenseInstance newinstance = new DenseInstance(2);
    double[] prediction=classifier.distributionForInstance(newinstance);
    //Some correction in for 
        for (int k =0; k<prediction.length; k++){
            System.out.println("Probability of class "+
                    newinstance.classAttribute().value(k)+
                    " : "+Double.toString(prediction[k]));
        }
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.