【问题标题】:Confusion matrix "Can't handle mix of multiclass and unkown"混淆矩阵“不能处理多类和未知的混合”
【发布时间】:2015-04-03 23:01:45
【问题描述】:

我的混淆矩阵显示了一个我无法理解的错误。 我想要一个混淆矩阵来显示两个数组之间的混淆,y_predy_test

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, roc_curve, auc
from sklearn.metrics import accuracy_score
import pylab as pl

# Code that fills up two numpy arrays, y_test and y_pred with integers

print y_test.shape
print y_pred.shape

cm = confusion_matrix(y_test,y_pred)
plt.matshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

错误是:

Traceback (most recent call last):
  File "C:\work_asaaki\code\test_samme_46_classes_unconfused.py", line 159, in <module>
    cm = confusion_matrix(y_test,y_pred)
  File "C:\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 742, in confusion_matrix
    y_type, y_true, y_pred = _check_clf_targets(y_true, y_pred)
  File "C:\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 115, in _check_clf_targets
    "".format(type_true, type_pred))
ValueError: Can't handle mix of multiclass and unknown

错误是什么意思? 当我打印出y_pred.shapey_test.shape 时,我得到了相同的形状(318L)。两个数组的值都在 0 到 29 之间。

【问题讨论】:

    标签: python numpy scikit-learn multilabel-classification


    【解决方案1】:

    没关系,我找到了答案,很简单。 问题是在代码(未显示)中,我使用dtype=object 将 y_pred 填充为 numpy 数组,喜欢这个:

    y_pred = np.array(pickle.load(file("PATH_TO_FILE")), dtype=object)
    

    我删除了dtype=object 部分,它工作正常。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2018-08-26
      • 2021-06-25
      • 2021-05-14
      • 2021-07-02
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      相关资源
      最近更新 更多