[英]Is there an easy way to get confusion matrix for multiclass classification? (OneVsRest)
I was using OneVsRest classifier on three class classification problem, (three random forests). 我在三类分类问题上使用OneVsRest分类器(三个随机森林)。 Occurrence of each class is defined my dummy integer (1 for occurrence, 0 for otherwise).
每个类的出现都是我的虚拟整数(1表示发生,0表示否则)。 I was wondering if there is an easy alternative way to creating confusion matrix?
我想知道是否有一种简单的替代方法来创建混淆矩阵? As all approaches I came across, takes arguments in the form of y_pred, y_train = array, shape = [n_samples].
正如我遇到的所有方法一样,以y_pred,y_train = array,shape = [n_samples]的形式获取参数。 Ideally , I would like y_pred, y_train = array , shape = [n_samples, n_classes]
理想情况下,我想要y_pred,y_train = array,shape = [n_samples,n_classes]
SOME SAMPLE , SIMILAR TO THE STRUCTURE OF THE PROBLEM: 一些样本,类似于问题的结构:
y_train = np.array([(1,0,0), (1,0,0), (0,0,1), (1,0,0), (0,1,0)])
y_pred = np.array([(1,0,0), (0,1,0), (0,0,1), (0,1,0), (1,0,0)])
print(metrics.confusion_matrix(y_train, y_pred)
RETURNS: multilabel-indicator is not supported 退货:不支持多标签指标
I don't know what you have in mind since you didn't specify the output you're looking for, but here are two ways you could go about it: 我不知道你有什么想法,因为你没有指定你想要的输出,但是有两种方法可以解决它:
1.One confusion matrix per column 1.每列一个混淆矩阵
In [1]:
for i in range(y_train.shape[1]):
print("Col {}".format(i))
print(metrics.confusion_matrix(y_train[:,i], y_pred[:,i]))
print("")
Out[1]:
Col 0
[[1 1]
[2 1]]
Col 1
[[2 2]
[1 0]]
Col 2
[[4 0]
[0 1]]
2.One confusion matrix altogether 2.一个混淆矩阵
For this, we are going to flatten the arrays: 为此,我们将展平数组:
In [2]: print(metrics.confusion_matrix(y_train.flatten(), y_pred.flatten()))
Out[2]:
[[7 3]
[3 2]]
You can try like below to get all the details in one go. 您可以尝试以下方式一次性获取所有详细信息。
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
This will give you something like below: 这将为您提供如下内容:
array([[ 7, 0, 0, 0],
[ 0, 7, 0, 0],
[ 0, 1, 2, 4],
[ 0, 1, 0, 11]])
-This means all diagonals are correctly predicted. - 这意味着所有对角线都能正确预测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.