python - predict_proba 或 decision_function 作为估计器 "confidence"

标签 python machine-learning scikit-learn

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

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

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

在此先感谢您能给我的任何帮助。

更新

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

最佳答案

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

关于python - predict_proba 或 decision_function 作为估计器 "confidence",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301986/

相关文章:

Python sklearn 所有记录的余弦相似度循环

python - 在django查询集中动态定义过滤器参数

python - 通过Python解析文件

python - 按 pandas 数据框唯一的第一个值分组 - 返回 numpy 数组

machine-learning - 如何考虑 Myrrix 中的权重

python - 不同阈值的特异性(与 sklearn.metrics. precision_recall_curve 相同)

python - Django:没有默认值的不可空字段

java - 我怎样才能在反向传播中获取softmax输出的导数

python - 在基础 Tensorflow 2.0 中运行简单回归

python - “float”类型错误 Python、pandas