【问题标题】:Big difference between predict and predict_proba probabilitiespredict 和 predict_proba 概率之间的巨大差异
【发布时间】:2020-07-26 05:37:45
【问题描述】:

我正在尝试训练用于社交网络帖子识别的模型,但遇到了一件奇怪的事情。我收到用户帖子的文本,模型预测一个类别,但最高概率对应于另一个类别。我在下面给出了最简单的例子,但我在其他模型中也发现了同样的情况。可能是我不了解 predict_proba 方法。

型号:

    texts = np.array(get_train_texts()[0])
    labels = np.array(get_train_texts()[1])

    X_train, X_test, y_train, y_test = train_test_split(texts, labels, 
                                       test_size=0.25, random_state=True)

    gbc_model = Pipeline([
        ('tf_idf', TfidfVectorizer()),
        ('gbc', GradientBoostingClassifier(n_estimators=128,
                                            max_depth=16,
                                            criterion='friedman_mse'))])

    gbc_model.fit(X_train, y_train)  
    text_to_recognize = [get_post(id, offset, access_token)]    
    label = gbc_model.predict(text_to_recognize)                
    grades = gbc_model.predict_proba(text_to_recognize)        
    grades = [f'{classes[i]}: {round(grades[0][i] * 100, 4)} %' for i in range(len(classes))]

输出:

...
['science'] 
 ['science: 3.6298 %', 'cinema: 1.0597 %', 'IT: 1.5812 %', 'art: 2.1504 %', 'games: 91.5788 %']

所以,如果我从成绩中选择 argmax,我会得到不正确的类“游戏”而不是“科学”,这对应的是好:

grades = gbc_model.predict_proba(text_to_recognize) 
result = classes[np.argmax(grades)]
print(result)

输出:

['science'] 
 ['science: 3.6298 %', 'cinema: 1.0597 %', 'IT: 1.5812 %', 'art: 2.1504 %', 'games: 91.5788 %']
games

为什么会这样?

【问题讨论】:

  • 你能用argmax的用法更新问题吗?
  • 完成。但有趣的是:如果我离开等级 = [f'{classes[i]}:... argmax 给出正确的结果“科学”
  • science 不是正确答案。根据概率,games 的概率最高,因此是正确答案。也就是说,最后一条语句将grades 转换为字符串值列表。当您获得字符串列表的最大值时,您将根据字符串比较获得最高值。这就是你观察science的原因

标签: python machine-learning xgboost


【解决方案1】:

gbc 模型使用的类顺序与您使用的不同。你选择了classes[i],但不能保证GradientBoostingClassifier使用的类的索引是一样的。

事实上,分类器按字母顺序排序类,使科学成为最后一个类,也是您示例中概率最高的类。这就是为什么您应该使用内部gbc_model.classes_ 属性或LabelEncoder

换句话说,分类器一切正常。

【讨论】:

  • 事实证明这是完全正确的。我最初按照我的类进入“类”的顺序创建标签,并且不认为预测可以放在另一个中。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多