Python 我无法理解使用argmax()删除OHE后得到的混淆矩阵

Python 我无法理解使用argmax()删除OHE后得到的混淆矩阵,python,numpy,machine-learning,scikit-learn,neural-network,Python,Numpy,Machine Learning,Scikit Learn,Neural Network,我无法解释我的混淆矩阵。我犯了一个值以下的错误 值错误:不支持多标签指示器 在阅读了大量帖子后,我意识到这个问题可能是由于预测中的OHE(一个热编码)造成的。因此,为了解决这个问题,我使用了argmax(),正如在各种帖子中建议的那样。下面是我的代码: from sklearn.metrics import confusion_matrix print(Y.shape) print(predictions.shape) print(Y) print(predictions) # print(co

我无法解释我的混淆矩阵。我犯了一个值以下的错误

值错误:不支持多标签指示器

在阅读了大量帖子后,我意识到这个问题可能是由于预测中的OHE(一个热编码)造成的。因此,为了解决这个问题,我使用了argmax(),正如在各种帖子中建议的那样。下面是我的代码:

from sklearn.metrics import confusion_matrix
print(Y.shape)
print(predictions.shape)
print(Y)
print(predictions)
# print(confusion_matrix(Y, predictions))
print(confusion_matrix(Y.argmax(axis = 1), predictions.argmax(axis = 1)))

(1, 200)
(1, 200)
[[1 1 0 0 1 1 0 1 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 1 0 1 0 1 0 0 1 1 1 0
  0 1 0 1 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 0 0 0 1
  0 1 0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1 1 0 1 0 1 1 1 1 1
  0 0 0 1 0 1 1 1 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 1 0 0 0 1 0
  0 0 1 1 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 0 1 0 1 0 0 1 1 1 1 1 0 0 1 1 1 1
  0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 1 0]]
[[1 1 0 0 1 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 1 1 1 0 1 0 1 0 1 0 0 1 1 0 0
  0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 1 1 0 1 1 1 1 1 0 0 1
  0 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 0 1 1 0 1 0 1 1 0 1 0 0 1 1 1 1
  0 0 0 1 0 1 1 1 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 0 0 0
  0 0 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0
  0 1 0 0 1 0 1 1 1 0 1 0 1 0 1 0 0 1 1 1]]
[[1]]

从输出中可以看出,我得到的是混淆矩阵
[[1]]
。我不知道如何解释它。我期待一个2x2的混淆矩阵,然后我会继续计算精度、召回率、F1分数等,以了解我的模型的性能。请说明我做错了什么?

问题在于输入数组的形状。你需要先把它们弄平。下面是一个重现您案例的示例:

from sklearn.metrics import confusion_matrix

Y = np.random.choice([0,1],size=(1,10))
pred = np.random.choice([0,1],size=(1,10))
由于两个数组都是二维的,如您的示例所示,
混淆矩阵
解释为您有多标签输出,但它不支持:

confusion_matrix(Y, pred)
ValueError: multilabel-indicator is not supported
您需要展平两个阵列:

confusion_matrix(Y.ravel(), pred.ravel())

IIUC的问题在于输入数组的形状。你需要先把它们弄平。下面是一个重现您案例的示例:

from sklearn.metrics import confusion_matrix

Y = np.random.choice([0,1],size=(1,10))
pred = np.random.choice([0,1],size=(1,10))
由于两个数组都是二维的,如您的示例所示,
混淆矩阵
解释为您有多标签输出,但它不支持:

confusion_matrix(Y, pred)
ValueError: multilabel-indicator is not supported
您需要展平两个阵列:

confusion_matrix(Y.ravel(), pred.ravel())

根据其他信息,输出是正确的。因为Y’shape是(1200),这表明您有一个样本,它有200个类。对其执行argmax()时,将得到大小为(1,1)的输出。混淆矩阵的大小为(n_类,n_类)。由于您只有一个样本,因此混淆矩阵的大小为(1,1)

要解决此问题,您需要传递多个样本,或者确保argmax正在执行预期的操作(即,您是否确定输出为OHE)?如果您的输出不是OHE,那么您只需要在获得混淆矩阵之前重新调整预测

from sklearn.metrics import confusion_matrix

Y = np.squeeze(Y) #should result in shape (200,)
predictions = np.squeeze(predictions) #should result in shape (200,)

cf = confusion_matrix(Y, predictions) #should result in shape (2, 2) if 2 classes 

根据其他信息,输出是正确的。因为Y’shape是(1200),这表明您有一个样本,它有200个类。对其执行argmax()时,将得到大小为(1,1)的输出。混淆矩阵的大小为(n_类,n_类)。由于您只有一个样本,因此混淆矩阵的大小为(1,1)

要解决此问题,您需要传递多个样本,或者确保argmax正在执行预期的操作(即,您是否确定输出为OHE)?如果您的输出不是OHE,那么您只需要在获得混淆矩阵之前重新调整预测

from sklearn.metrics import confusion_matrix

Y = np.squeeze(Y) #should result in shape (200,)
predictions = np.squeeze(predictions) #should result in shape (200,)

cf = confusion_matrix(Y, predictions) #should result in shape (2, 2) if 2 classes 

我的问题分为两类,即0,1。因此Y对于每个样本只有一个标签(0或1)。预测也是如此。所以,我相信形状在分类问题上是相同的。鉴于错误“ValueError:不支持多标签指示器”,我得出结论,这可能是OHE问题。明白了。所以看起来你的预测数组实际上不是一个热编码的数组——0和1是类本身。在获得混淆矩阵之前,请尝试np.squence()。是的,我考虑过这种可能性并尝试过。在这种情况下,它给出了“AxisError:轴1超出维度1数组的界限”错误。它应该在轴=0上压缩。尝试
np.挤压(Y,轴=0)
。这是
np.挤压(Y)
行上的错误吗?另一种确保的方法是手动重塑它:
Y=Y.restorate(Y.shape[1],)
。我的问题有两类,即0,1。因此Y对于每个样本只有一个标签(0或1)。预测也是如此。所以,我相信形状在分类问题上是相同的。鉴于错误“ValueError:不支持多标签指示器”,我得出结论,这可能是OHE问题。明白了。所以看起来你的预测数组实际上不是一个热编码的数组——0和1是类本身。在获得混淆矩阵之前,请尝试np.squence()。是的,我考虑过这种可能性并尝试过。在这种情况下,它给出了“AxisError:轴1超出维度1数组的界限”错误。它应该在轴=0上压缩。尝试
np.挤压(Y,轴=0)
。这是
np.挤压(Y)
行上的错误吗?另一种方法是手动对其进行整形:
Y=Y.reshope(Y.shape[1],)