【问题标题】:predict_proba or decision_function as estimator "confidence"predict_proba 或 decision_function 作为估计器“置信度”
【发布时间】:2012-10-29 09:55:36
【问题描述】:

我使用 LogisticRegression 作为模型来训练 scikit-learn 中的估计器。我使用的功能(大部分)是分类的;标签也是如此。因此,我分别使用 DictVectorizer 和 LabelEncoder 对值进行正确编码。

培训部分相当简单,但我在测试部分遇到了问题。简单的做法是使用训练好的模型的“预测”方法,得到预测的标签。但是,对于之后需要进行的处理,我需要每个特定实例的每个可能标签(类)的概率。我决定使用“predict_proba”方法。但是,对于同一个测试实例,我得到不同的结果,无论是在实例单独使用还是与其他实例一起使用时使用此方法。

接下来是重现问题的代码。

from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import LabelEncoder


X_real = [{'head': u'n\xe3o', 'dep_rel': u'ADVL'}, 
          {'head': u'v\xe3o', 'dep_rel': u'ACC'}, 
          {'head': u'empresa', 'dep_rel': u'SUBJ'}, 
          {'head': u'era', 'dep_rel': u'ACC'}, 
          {'head': u't\xeam', 'dep_rel': u'ACC'}, 
          {'head': u'import\xe2ncia', 'dep_rel': u'PIV'}, 
          {'head': u'balan\xe7o', 'dep_rel': u'SUBJ'}, 
          {'head': u'ocupam', 'dep_rel': u'ACC'}, 
          {'head': u'acesso', 'dep_rel': u'PRED'}, 
          {'head': u'elas', 'dep_rel': u'SUBJ'}, 
          {'head': u'assinaram', 'dep_rel': u'ACC'}, 
          {'head': u'agredido', 'dep_rel': u'SUBJ'}, 
          {'head': u'pol\xedcia', 'dep_rel': u'ADVL'}, 
          {'head': u'se', 'dep_rel': u'ACC'}] 
y_real = [u'AM-NEG', u'A1', u'A0', u'A1', u'A1', u'A1', u'A0', u'A1', u'AM-ADV', u'A0', u'A1', u'A0', u'A2', u'A1']

feat_encoder =  DictVectorizer()
feat_encoder.fit(X_real)

label_encoder = LabelEncoder()
label_encoder.fit(y_real)

model = LogisticRegression()
model.fit(feat_encoder.transform(X_real), label_encoder.transform(y_real))

print "Test 1..."
X_test1 = [{'head': u'governo', 'dep_rel': u'SUBJ'}]
X_test1_encoded = feat_encoder.transform(X_test1)
print "Features Encoded"
print X_test1_encoded
print "Shape"
print X_test1_encoded.shape
print "decision_function:"
print model.decision_function(X_test1_encoded)
print "predict_proba:"
print model.predict_proba(X_test1_encoded)

print "Test 2..."
X_test2 = [{'head': u'governo', 'dep_rel': u'SUBJ'}, 
           {'head': u'atrav\xe9s', 'dep_rel': u'ADVL'}, 
           {'head': u'configuram', 'dep_rel': u'ACC'}]

X_test2_encoded = feat_encoder.transform(X_test2)
print "Features Encoded"
print X_test2_encoded
print "Shape"
print X_test2_encoded.shape
print "decision_function:"
print model.decision_function(X_test2_encoded)
print "predict_proba:"
print model.predict_proba(X_test2_encoded)


print "Test 3..."
X_test3 = [{'head': u'governo', 'dep_rel': u'SUBJ'}, 
           {'head': u'atrav\xe9s', 'dep_rel': u'ADVL'}, 
           {'head': u'configuram', 'dep_rel': u'ACC'},
           {'head': u'configuram', 'dep_rel': u'ACC'},]

X_test3_encoded = feat_encoder.transform(X_test3)
print "Features Encoded"
print X_test3_encoded
print "Shape"
print X_test3_encoded.shape
print "decision_function:"
print model.decision_function(X_test3_encoded)
print "predict_proba:"
print model.predict_proba(X_test3_encoded)

以下是得到的输出:

Test 1...
Features Encoded
  (0, 4)    1.0
Shape
(1, 19)
decision_function:
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347]]
predict_proba:
[[ 1.  1.  1.  1.  1.]]
Test 2...
Features Encoded
  (0, 4)    1.0
  (1, 1)    1.0
  (2, 0)    1.0
Shape
(3, 19)
decision_function:
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347]
 [-1.07370197 -0.69103629 -0.89306092 -1.51402163 -0.89306092]
 [-1.55921001  1.11775556 -1.92080112 -1.90133404 -1.92080112]]
predict_proba:
[[ 0.59710757  0.19486904  0.26065002  0.32612646  0.26065002]
 [ 0.23950111  0.24715931  0.51348452  0.3916478   0.51348452]
 [ 0.16339132  0.55797165  0.22586546  0.28222574  0.22586546]]
Test 3...
Features Encoded
  (0, 4)    1.0
  (1, 1)    1.0
  (2, 0)    1.0
  (3, 0)    1.0
Shape
(4, 19)
decision_function:
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347]
 [-1.07370197 -0.69103629 -0.89306092 -1.51402163 -0.89306092]
 [-1.55921001  1.11775556 -1.92080112 -1.90133404 -1.92080112]
 [-1.55921001  1.11775556 -1.92080112 -1.90133404 -1.92080112]]
predict_proba:
[[ 0.5132474   0.12507868  0.21262531  0.25434403  0.21262531]
 [ 0.20586462  0.15864173  0.4188751   0.30544372  0.4188751 ]
 [ 0.14044399  0.3581398   0.1842498   0.22010613  0.1842498 ]
 [ 0.14044399  0.3581398   0.1842498   0.22010613  0.1842498 ]]

可以看出,当同一个实例与 X_test2 中的其他实例一起使用时,使用“predict_proba”为“X_test1”中的实例获得的值会发生变化。此外,“X_test3”只是复制了“X_test2”并添加了一个实例(等于“X_test2”中的最后一个实例),但所有实例的概率值都发生了变化。为什么会这样? 另外,我觉得很奇怪“X_test1”的所有概率都是 1,所有的总和不应该是 1 吗?

现在,如果我不使用“predict_proba”,而使用“decision_function”,我会获得所需值的一致性。问题是我得到了负系数,甚至一些正系数大于1。

那么,我应该使用什么?为什么“predict_proba”的值会这样改变?我没有正确理解这些值的含义吗?

提前感谢您能给我的任何帮助。

更新

按照建议,我更改了代码,以便同时打印编码的“X_test1”、“X_test2”和“X_test3”,以及它们的形状。这似乎不是问题,因为测试集之间的相同实例的编码是一致的。

【问题讨论】:

  • 乍一看,您的所有观察结果似乎都是正确的。我也不明白发生了什么。您能否也提供每次通话的编码数据?它的形状?
  • 这是a bug in master 我认为从未进入任何版本。 @feralvam,您使用的是哪个版本的 scikit-learn?
  • @larsmans 我使用的是 0.13。我前一阵子下载了。我应该更新到哪个版本?
  • @feralvam:目前还没有 0.13 版本,所以您使用的是 GitHub 存储库的旧 snapshort。我建议你要么使用 0.12.1,要么使用新的快照,如果你真的需要前沿特性/想要贡献代码。
  • @larsmans 太棒了!谢谢!

标签: python machine-learning scikit-learn


【解决方案1】:

如问题的 cmets 所示,该错误是由我正在使用的 scikit-learn 版本的实现中的错误引起的。更新到最新的稳定版本 0.12.1 解决了问题

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 2021-02-14
    • 2011-12-21
    • 1970-01-01
    • 2021-02-20
    • 2016-07-03
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多