如何在sklearn中获得分类器的置信度分数以进行预测? [英] 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.
我想要这样的东西:
分类器的预测如何确定?
How sure is the classifier on its prediction?
第1类:这是第1类的81%
第2类:10%
第3类:6%
第4类:3%
Class 1: 81% that this is class 1
Class 2: 10%
Class 3: 6%
Class 4: 3%
我的代码示例:
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
我怀疑我会使用score()函数,但是我似乎一直在正确地实现它.我不知道这是否是正确的函数,但是如何获得分类器预测的置信度呢?
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?
推荐答案
每个 SVC文档,看来您需要更改构造SVC的方式:
Per the SVC documentation, it looks like you need to change how you construct the SVC:
model = SVC(probability=True)
,然后使用predict_proba方法:
and then use the predict_proba method:
class_probabilities = model.predict_proba(sub_main)
这篇关于如何在sklearn中获得分类器的置信度分数以进行预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!