Search code examples
javamachine-learningprobabilitysvmlibsvm

How to compute the probability of a multi-class prediction using libsvm?


I'm using libsvm and the documentation leads me to believe that there's a way to output the believed probability of an output classification's accuracy. Is this so? And if so, can anyone provide a clear example of how to do it in code?

Currently, I'm using the Java libraries in the following manner

    SvmModel model = Svm.svm_train(problem, parameters);
    SvmNode x[] = getAnArrayOfSvmNodesForProblem();
    double predictedValue = Svm.svm_predict(model, x);

Solution

  • Given your code-snippet, I'm going to assume you want to use the Java API packaged with libSVM, rather than the more verbose one provided by jlibsvm.

    To enable prediction with probability estimates, train a model with the svm_parameter field probability set to 1. Then, just change your code so that it calls the svm method svm_predict_probability rather than svm_predict.

    Modifying your snippet, we have:

    parameters.probability = 1;
    svm_model model = svm.svm_train(problem, parameters);
    
    svm_node x[] = problem.x[0]; // let's try the first data pt in problem
    double[] prob_estimates = new double[NUM_LABEL_CLASSES]; 
    svm.svm_predict_probability(model, x, prob_estimates);
    

    It's worth knowing that training with multiclass probability estimates can change the predictions made by the classifier. For more on this, see the question Calculating Nearest Match to Mean/Stddev Pair With LibSVM.