ValueError:不支持多标签指示符混淆矩阵

问题描述 投票:0回答:1
尝试运行时收到的错误消息是“不支持多标签指示器”: 你能给我任何解决方案或提示吗?

import seaborn as sns sns.heatmap(confusion_matrix(y_test, y_pred), annot = True)
ValueError                                Traceback (most recent call last)
<ipython-input-21-ee6823d584e9> in <cell line: 1>()
----> 1 sns.heatmap(confusion_matrix(y_test, y_pred), annot = True)

/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight, normalize)
    317     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
    318     if y_type not in ("binary", "multiclass"):
--> 319         raise ValueError("%s is not supported" % y_type)
    320 
    321     if labels is None:

ValueError: multilabel-indicator is not supported
    
python machine-learning scikit-learn classification confusion-matrix
1个回答
0
投票
您的 y_data 需要是扁平且分类的(二进制或多类),即形式

from sklearn.utils.multiclass import type_of_target >>> type_of_target([1.0, 2.0]) 'binary' >>> type_of_target([1, 0, 2]) 'multiclass' >>> type_of_target([1.0, 0.0, 3.0]) 'multiclass' >>> type_of_target(['a', 'b', 'c']) 'multiclass'
根据您的 y_pred 的外观,您需要对其进行分类以适应上述模式,例如不是单热编码。

您可能必须首先按以下方式对模型输出进行后处理

y_pred = outputs.argmax(axis=1)
    
© www.soinside.com 2019 - 2024. All rights reserved.