【问题标题】:Each row is for which label in confusion matrix python每行是混淆矩阵python中的哪个标签
【发布时间】:2015-11-04 02:55:51
【问题描述】:

我在 sklearn 中使用混淆矩阵。

我的问题是,我无法理解每一行代表哪个标签!我的标签是[0, 1, 2, 3, 4, 5]

我想知道如果第一行是标签 0,第二行是标签 1 等等?

为了确保,我尝试了这段代码,我认为它是按标签顺序制作混淆矩阵的。但是我遇到了一个错误。

cfr = RandomForestClassifier(n_estimators = 80, n_jobs = 5)
cfr.fit(X1, y1)
predictedY2 = cfr.predict(X2)
shape = np.array([0, 1, 2, 3, 4, 5])
acc1 = cfr.score(X2, y2,shape)

错误是:

acc1 = cfr.score(X2, y2,shape)
TypeError: score() takes exactly 3 arguments (4 given)`

【问题讨论】:

  • crf.score 的文档是什么?
  • 4 个参数是 self 和 3 个显式参数。我会尝试在最后使用关键字。并且未通过该检查版本。关键字参数可能是最近添加的。我没有那个包裹,所以无法检查自己。虽然我可以探索 github 代码。

标签: python numpy import scikit-learn


【解决方案1】:

score 给出了分类器的准确率,即每个示例的正确预测数。您正在寻找的是 predict 函数,它产生为每个输入预测的类。看看这个例子:

import numpy as np
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.metrics import confusion_matrix
from sklearn.datasets import make_classification

# Add a random state to the various functions so we all have the same output.
rng = np.random.RandomState(1234)

# Make dataset
X,Y = make_classification( n_samples=1000, n_classes=6, n_features=20, n_informative=15, random_state=rng ) 
# take random 75% of data as training, leaving rest for test
train_inds = rng.rand(1000) < 0.75

# create and train the classifier
rfc = RFC(n_estimators=80, random_state=rng)
rfc.fit(X[train_inds], Y[train_inds])

# O is the predicted class for each input on the test data
O = rfc.predict(X[~train_inds])

print "Test accuracy: %.2f%%\n" % (rfc.score(X[~train_inds],Y[~train_inds])*100)

print "Confusion matrix:"
print confusion_matrix(Y[~train_inds], O)

打印出来:

Test accuracy: 57.92%

Confusion matrix:
[[24  4  3  1  1  6]
 [ 5 22  4  4  1  1]
 [ 5  2 18  5  3  2]
 [ 2  4  2 29  1  4]
 [ 3  1  3  2 28  3]
 [10  4  4  3  8 18]]

根据confusion_matrix 的文档,混淆矩阵的i,j 分量是已知属于i 类但归类为j 的对象的数量。所以在上面,正确分类的对象在对角线上,但是如果你看,比如说,第 3 行,第 0 列,看起来两个“3 类”对象被错误分类为“0 类”对象。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 2020-07-31
    • 2018-11-06
    • 2020-10-01
    • 2020-10-24
    • 2020-08-30
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多