Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a classifier's confidence score for a prediction in sklearn?

I would like to get a confidence score of each of the predictions that it makes, showing on how sure the classifier is on its prediction that it is correct.

I want something like this:

How sure is the classifier on its prediction?

Class 1: 81% that this is class 1
Class 2: 10%
Class 3: 6%
Class 4: 3%

Samples of my code:

features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(main, target, test_size = 0.4)  # Determine amount of time to train t0 = time() model = SVC() #model = SVC(kernel='poly') #model = GaussianNB()  model.fit(features_train, labels_train)  print 'training time: ', round(time()-t0, 3), 's'  # Determine amount of time to predict t1 = time() pred = model.predict(features_test)  print 'predicting time: ', round(time()-t1, 3), 's'  accuracy = accuracy_score(labels_test, pred)  print 'Confusion Matrix: ' print confusion_matrix(labels_test, pred)  # Accuracy in the 0.9333, 9.6667, 1.0 range print accuracy    model.predict(sub_main)  # Determine amount of time to predict t1 = time() pred = model.predict(sub_main)  print 'predicting time: ', round(time()-t1, 3), 's'  print '' print 'Prediction: ' print pred 

I suspect that I would use the score() function, but I seem to keep implementing it correctly. I don't know if that's the right function or not, but how would one get the confidence percentage of a classifier's prediction?

like image 367
user3377126 Avatar asked Jun 30 '15 04:06

user3377126


People also ask

How do you get a confidence from a classifier?

If you want confidence of classification result, you have two ways. First is using the classifier that will output probabilistic score, like logistic regression; the second approach is using calibration, like for svm or CART tree.

Is confidence score same as accuracy?

Confidence value can be calculated for single input as well giving the meaning as how much the algorithm is confident for that class. On the other hand, accuracy defines the skill of the learning algorithm to predict accurately. It defines the percentage of correct predictions made from all predictions.

What does predict () function of Sklearn do?

The Sklearn 'Predict' Method Predicts an OutputThat being the case, it provides a set of tools for doing things like training and evaluating machine learning models. What is this? And it also has tools to predict an output value, once the model is trained (for ML techniques that actually make predictions).


1 Answers

Per the SVC documentation, it looks like you need to change how you construct the SVC:

model = SVC(probability=True) 

and then use the predict_proba method:

class_probabilities = model.predict_proba(sub_main) 
like image 88
Justin Peel Avatar answered Sep 23 '22 15:09

Justin Peel