简体   繁体   English

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

[英]confusion_matrix() | ValueError: Classification metrics can't handle a mix of multiclass and multiclass-multioutput targets

Definitely has been asked before, but I've not been successful at analysing other posts' solutions for my own instance of this problem.以前肯定有人问过,但我没有成功分析其他帖子的解决方案,以解决我自己的这个问题的实例。

I have many classification models I want to compare using confusion_matrix()我有许多分类模型我想使用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, )

Error:错误:

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

When .flatten() is performed on y_pred - ie 1D array (500 * 256 = 128000):.flatten()y_pred上执行时 - 即一维数组(500 * 256 = 128000):

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

Confusion matrix works on the basis of comparision between each predicted value and actual value.混淆矩阵基于每个预测值与实际值之间的比较来工作。 It is impossible compare 1 with [2,2,2....2,2,2]不可能将1[2,2,2....2,2,2]进行比较

In your case, your y_pred is 2d but your y_test is 1d, thats where the actual error came.在您的情况下,您的 y_pred 是 2d 但您的 y_test 是 1d,这就是实际错误出现的地方。 I believe that you have to choose the most common number in your predicted list.我相信你必须在你的预测列表中选择最常见的数字。 Like 2 from [2,2,2....2,2][2,2,2....2,2] 2的 2

So here is the solution:所以这里是解决方案:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras混淆矩阵:ValueError:分类指标无法处理多类多输出和二进制目标的混合 - Keras confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass-multioutput and binary targets 混淆矩阵:ValueError:分类指标无法处理多类和连续多输出目标的混合 - Confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets Confusion_matrix ValueError:分类指标无法处理二进制和连续多输出目标的混合 - Confusion_matrix ValueError: Classification metrics can't handle a mix of binary and continuous-multioutput targets 错误:分类指标无法处理多类多输出和多标记指标目标的混合 - Error: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets hwo 修复 ValueError:分类指标无法处理多类和连续多输出目标的混合 - hwo to repair ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets 接收分类指标无法处理多类混淆_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" 如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合 - how to handle ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error 如何修复 ValueError:分类指标无法处理模型的多类和多标签指标目标的混合? - How to fix ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets for model? ValueError:分类指标无法在 ROC 曲线计算中处理多类和多标签指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets in ROC curve calculation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM