简体   繁体   English

是否有一种简单的方法来获得多类分类的混淆矩阵? (OneVsRest)

[英]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.

相关问题 用于多类分类的Tensorflow混淆矩阵 - Tensorflow confusion matrix for multiclass classification 如何计算Scikit中多类分类的混淆矩阵? - How compute confusion matrix for multiclass classification in Scikit? 在keras中生成混淆矩阵以进行多类分类 - generating confusion matrix in keras for multiclass classification 混淆矩阵() | ValueError:分类指标无法处理多类和多类多输出目标的混合 - confusion_matrix() | ValueError: Classification metrics can't handle a mix of multiclass and multiclass-multioutput targets 混淆矩阵:ValueError:分类指标无法处理多类和连续多输出目标的混合 - Confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets 如何从多类分类的混淆矩阵中提取假阳性,假阴性 - How to extract False Positive, False Negative from a confusion matrix of multiclass classification 接收分类指标无法处理多类混淆_matrix - Receiving Classification metrics can't handle a mix of multiclass confusion_matrix 混淆矩阵错误“分类指标无法处理多标签指标和多类目标的混合” - confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets" Keras混淆矩阵:ValueError:分类指标无法处理多类多输出和二进制目标的混合 - Keras confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass-multioutput and binary targets 混淆矩阵和分类报告 - Confusion matrix and classification report
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM