How to calculate the probability of multiclass prediction using libsvm?

I use libsvm , and the documentation makes me think that there is a way to deduce the estimated probability of classifying the results as accurate. This is true? And if so, can someone give a clear example of how to do this in code?

I am currently using Java libraries as follows

SvmModel model = Svm.svm_train(problem, parameters); SvmNode x[] = getAnArrayOfSvmNodesForProblem(); double predictedValue = Svm.svm_predict(model, x); 
+4
source share
2 answers

Given your piece of code, I'm going to suggest that you want to use the Java API packaged with libSVM , and not the more verbose one from jlibsvm .

To enable forecasting with probability estimates, prepare a model with the svm_parameter field probability set to 1 . Then just change your code so that it invokes the svm_predict_probability svm_predict_probability method , not svm_predict .

Changing your fragment, we have:

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

It is worth knowing that training with multiclass probability estimates can change the predictions made by the classifier. For more information about this, see the question Calculating the closest match to the middle / Stddev pairs with LibSVM .

+7
source

The accepted answer worked like a charm. Be sure to set probability = 1 during your workout.

If you are trying to drop the prediction when confidence does not fit the threshold, here is a sample code:

 double confidenceScores[] = new double[model.nr_class]; svm.svm_predict_probability(model, svmVector, confidenceScores); /*System.out.println("text="+ text); for (int i = 0; i < model.nr_class; i++) { System.out.println("i=" + i + ", labelNum:" + model.label[i] + ", name=" + classLoadMap.get(model.label[i]) + ", score="+confidenceScores[i]); }*/ //finding max confidence; int maxConfidenceIndex = 0; double maxConfidence = confidenceScores[maxConfidenceIndex]; for (int i = 1; i < confidenceScores.length; i++) { if(confidenceScores[i] > maxConfidence){ maxConfidenceIndex = i; maxConfidence = confidenceScores[i]; } } double threshold = 0.3; // set this based data & no. of classes int labelNum = model.label[maxConfidenceIndex]; // reverse map number to name String targetClassLabel = classLoadMap.get(labelNum); LOG.info("classNumber:{}, className:{}; confidence:{}; for text:{}", labelNum, targetClassLabel, (maxConfidence), text); if (maxConfidence < threshold ) { LOG.info("Not enough confidence; threshold={}", threshold); targetClassLabel = null; } return targetClassLabel; 
+1
source

Source: https://habr.com/ru/post/1308704/


All Articles