微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

混淆矩阵() | ValueError:分类指标无法处理多类和多类多输出目标的混合

如何解决混淆矩阵() | ValueError:分类指标无法处理多类和多类多输出目标的混合

之前肯定有人问过,但我没有成功地分析其他帖子的解决方案以解决我自己的这个问题。

我有很多分类模型要使用 confusion_matrix()

进行比较
matrix = confusion_matrix(y_test,y_pred) # ERROR
>>> y_pred
[[2 2 2 ... 2 2 2]
 [2 2 2 ... 2 2 2]
 [2 2 2 ... 2 2 2]
 ...
 [3 3 2 ... 3 2 3]
 [2 2 2 ... 2 2 2]
 [3 3 3 ... 3 3 3]]
>>> y_pred.shape
(500,256)
>>> y_test
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
>>> y_test.shape
(500,)

错误

ValueError: Classification metrics can't handle a mix of multiclass and multiclass-multIoUtput targets

当对 .flatten() 执行 y_pred 时 - 即一维数组 (500 * 256 = 128000):

ValueError: Found input variables with inconsistent numbers of samples: [500,128000]

解决方法

混淆矩阵的工作原理是将每个预测值与实际值进行比较。将 1[2,2,2....2,2]

进行比较是不可能的

在您的情况下,您的 y_pred 是 2d 但您的 y_test 是 1d,这就是实际错误出现的地方。我相信您必须在预测列表中选择最常见的数字。喜欢来自2

[2,2]

所以这是解决方案:

from scipy import stats 
import numpy as np

#taking the most frequent element from the predicted list
y_pred_list = [int(stats.mode(arr)[0]) for arr in y_pred.tolist()] #convert to list

y_pred_array = np.array(y_pred_list)  #convert to 1D with same shape of y_test

print(y_pred_array.shape)

print(y_pred_array)

matrix = confusion_matrix(y_test,y_pred_array)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。