【发布时间】:2015-09-18 00:24:55
【问题描述】:
我在理解 sklearn 的函数时遇到了困难,希望得到澄清。一开始我以为sklearn的SVM的predict_proba函数给出了分类器预测的置信度,但是在用我的情绪识别程序玩过之后,我开始产生怀疑,感觉我误解了predict_proba函数的使用和如何工作。
例如,我的代码设置如下:
# Just finished training and now is splitting data (cross validation)
# and will give an accuracy after testing the accuracy of the test data
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(main, target, test_size = 0.4)
model = SVC(probability=True)
model.fit(features_train, labels_train)
pred = model.predict(features_test)
accuracy = accuracy_score(labels_test, pred)
print accuracy
# Code that records video of 17 frames and forms matrix know as
# sub_main with features that would be fed into SVM
# Few lines of code later. . .
model.predict(sub_main)
prob = model.predict_proba(sub_main)
prob_s = np.around(prob, decimals=5)
prob_s = prob_s* 100
pred = model.predict(sub_main)
print ''
print 'Prediction: '
print pred
print 'Probability: '
print 'Neutral: ', prob_s[0,0]
print 'Smiling: ', prob_s[0,1]
print 'Shocked: ', prob_s[0,2]
print 'Angry: ', prob_s[0,3]
print ''
当我测试它时,它给了我这样的东西:
Prediction:
['Neutral']
Probability:
Neutral: 66.084
Smiling: 17.875
Shocked: 11.883
Angry: 4.157
它成功地使正确分类为“中性”的置信度达到 66%。 66 在“中性”旁边,恰好是最高的数字。最高数字标有实际预测,我对此感到高兴。
但最终最终。 . .
Prediction:
['Angry']
Probability:
Neutral: 99.309
Smiling: 0.16
Shocked: 0.511
Angry: 0.02
它做出了预测,“愤怒”(顺便说一句,这是正确的分类),并在“中性”旁边分配了 99.3% 的置信度。尽管预测完全不同,但最高级别的置信度(最高数字)被分配给 Neutral。
有时它也会这样做:
Prediction:
['Smiling']
Probability:
Neutral: 0.0
Smiling: 0.011
Shocked: 0.098
Angry: 99.891
Prediction:
['Angry']
Probability:
Neutral: 99.982
Smiling: 0.0
Shocked: 0.016
Angry: 0.001
我不认为理解 SVM 的 predict_proba 函数是如何工作的,我想了解一下它是如何工作的以及我的代码发生了什么。我的代码发生了什么?
【问题讨论】:
-
来自 SVC 上的文档:“概率模型是使用交叉验证创建的,因此结果可能与通过预测获得的结果略有不同。此外,它会在非常小的数据集上产生毫无意义的结果。”你的训练集有多大?
-
大约 550 个示例。对于 predict_proba 函数来说,这是否被认为太小了?
-
@user3377126 样本大小看起来不错。你的训练集的准确率是多少?
-
交叉验证的范围为 80% 到 90%
-
您是否尝试在 SVC 中设置 class_weight='auto'?据我了解,predict_proba 使用 Platt 缩放,它使用交叉验证。如果你有一些类在数据集中的数量大大超过,如果它们在 predict_proba 中表现特别差,我不会感到惊讶。 class_weight='auto' 可能对此有所帮助。
标签: python machine-learning scikit-learn classification probability