我想获取每个预测的置信度得分,显示分类器对其预测正确性的把握程度。
我想要的结果类似于这样:
分类器对其预测的把握程度如何?
类别1:81% 认为这是类别1
类别2:10%
类别3:6%
类别4:3%
我的代码样本:
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(main, target, test_size = 0.4)# 确定训练所需的时间t0 = time()model = SVC()#model = SVC(kernel='poly')#model = GaussianNB()model.fit(features_train, labels_train)print 'training time: ', round(time()-t0, 3), 's'# 确定预测所需的时间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)# 准确率在0.9333, 9.6667, 1.0范围内print accuracymodel.predict(sub_main)# 确定预测所需的时间t1 = time()pred = model.predict(sub_main)print 'predicting time: ', round(time()-t1, 3), 's'print ''print 'Prediction: 'print pred
我怀疑应该使用score()函数,但似乎一直无法正确实现。我不知道这是否是正确的函数,那么如何获取分类器预测的置信度百分比呢?
回答:
根据SVC文档,看起来你需要改变SVC的构造方式:
model = SVC(probability=True)
然后使用predict_proba方法:
class_probabilities = model.predict_proba(sub_main)