10

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.

Howa Begum
  • 348
  • 1
  • 6

2 Answers2

9

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.

Reja
  • 898
  • 1
  • 9
  • 21
1

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]));
        }
Alex Titov
  • 11
  • 1
  • 2