【问题标题】:sklearn : get prediction along with score in Random Forest classifiersklearn:在随机森林分类器中获得预测和分数
【发布时间】:2018-10-04 15:07:12
【问题描述】:

我能够获得Random Forest 分类器中每一行的单独预测值。

有没有什么方法可以得到每一行的预测分数?

我能够获得整个数据的预测准确度得分。但我需要为每一行单独提供它。

【问题讨论】:

  • 一行的预测分数到底是什么意思?所选班级的计算概率是多少?因为我认为行分数可以是真或假 - 您可以通过将测试数据与预测数据进行比较来检查您是否达到了所选行的目标。
  • 我需要手动操作吗?@Konrad Lyda
  • 请举例说明你想要什么。您将计算什么类型的分数。对于单行,模型的预测将与真实标签匹配或不匹配。没有别的
  • 如何判断是真是假? @Vivek Kumar
  • clf = RandomForestClassifier() clf.fit(trainArr, trainRes[:,0]) clf.score(trainArr, trainRes[:,0]) - 这里我得到准确度分数。结果 = clf.predict(testArr) 。这里的结果将给出预测标签。除此之外,我想知道预测值是真还是假。截至目前,我手动比较这些值并找到它。有没有办法自动完成

标签: machine-learning scikit-learn random-forest


【解决方案1】:

只需比较数组即可轻松完成。无需逐行进行。

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=100, n_features=4,
                           n_informative=2, n_redundant=0,
                           random_state=0, shuffle=False)
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)

predictions = clf.predict(X)
comparison = (pred==y)

print(comparison)
Output: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
   False,  True,  True,  True,  True,  True,  True, False,  True,
    True,  True, False,  True, False,  True, False,  True,  True,
    True,  True,  True,  True, False,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True, False,  True,  True,  True,  True,  True,  True,
    True, False,  True,  True,  True,  True,  True,  True,  True,
    True])

False 表示在这种情况下预测是错误的。

【讨论】:

猜你喜欢
  • 2018-06-11
  • 2015-07-26
  • 2019-03-14
  • 2021-02-11
  • 2019-11-16
  • 2019-07-22
  • 2018-02-18
  • 2013-12-31
  • 2021-08-13
相关资源
最近更新 更多