首页 文章

无法理解sklearn 's SVM'的predict_proba函数

提问于
浏览
3

我无法理解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

它做出了预测,“愤怒”(这是正确的分类btw),它在“中性”旁边分配了99.3%的置信水平 . 尽管预测完全不同,但最高置信水平(最高数字)被分配到中性 .

有时它也这样做:

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函数是如何工作的,并希望澄清它的工作原理以及我的代码发生了什么 . 我的代码中发生了什么?

1 回答

  • 1

    我不太了解SVC的工作原理,因此您可以考虑在评论中说什么来完成这个答案 .

    您必须考虑到predic_proba将按照它们出现在classes_属性中的字典顺序为您提供类别 . 你有这个在the doc .

    当您想要打印结果时,您必须考虑这一点 . 我们可以在你的例子中看到,愤怒是第一个索引,所以你的结果除了第一个之外都是好的 .

    试试这个 :

    print 'Neutral: ', prob_s[0,1]
    print 'Smiling: ', prob_s[0,3]
    print 'Shocked: ', prob_s[0,2]
    print 'Angry: ', prob_s[0,0]
    

相关问题