无法在Keras中预测后创建混淆矩阵无法处理多标签指示器的混合

2024-10-01 02:23:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用混淆矩阵来评估我的Keras模型。但是,我无法使其工作,因为我总是得到相同的错误:

ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets

我在看这个问题:

confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"

我试着模仿一切,但都不起作用。我认为情况并非如此

这是我的代码:

validationTweets = validation.content.tolist() #data for validation
validation_features = vectorizerCV.transform(validationTweets) #vectorizing data for validation

prediction = model.predict(validation_features , batch_size=32) # making prediction

realLabels = validation.sentiment # true labels/classes (STRING VALUES)
realLabels = np.asarray(realLabels.factorize()[0]) # converting them to categorical
realLabels = to_categorical(realLabels, num_classes = 3) # converting them to categorical

print('true labels type', type(realLabels))  #<class 'numpy.ndarray'>
print('true labels shape',realLabels.shape)  # (5000, 3)

print('prediction type', type(prediction))  #<class 'numpy.ndarray'>
print('prediction shape', prediction.shape) #(5000, 3)


matrix = confusion_matrix(realLabels, prediction)

这就是我的真实标签的样子:

[[1. 0. 0.]
 [1. 0. 0.]
 [0. 1. 0.]
 ...
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

我的预测是这样的:

[[8.6341507e-04 6.8435425e-01 3.1478229e-01]
 [8.4774427e-02 7.8772342e-01 1.2750208e-01]
 [4.3412593e-01 5.0705791e-01 5.8816209e-02]
 ...
 [9.1305929e-01 6.6390157e-02 2.0550590e-02]
 [8.2271063e-01 1.5146920e-01 2.5820155e-02]
 [1.7649201e-01 7.2304797e-01 1.0045998e-01]]

我试过这个:

prediction = [np.round(p, 0) for p in prediction]

ERROR: multilabel-indicator is not supported

我也试过:

prediction = prediction.argmax(axis = 1) # shape is (5000,)

ERROR:    ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets

但我也犯了同样的错误


Tags: andoftypecanindicatormetricsvalidationhandle
1条回答
网友
1楼 · 发布于 2024-10-01 02:23:38

我对Keras confusion_matrix不是非常熟悉,但是四舍五入您的预测对于多类是不起作用的。对于每个样本,您需要选择概率最高的预测类,并使该条目等于您的预测中的一个条目。例如:

pred = np.array([0.3, 0.3, 0.4])
rounded = np.round(pred) # Gives [0, 0, 0]
most_likely = np.zeros(3)
most_likely[pred >= np.max(pred)] = 1 # Gives [0,0,1]

相关问题 更多 >