简体   繁体   中英

ValueError: Classification metrics can't handle a mix of binary and continuous targets

y_test.shape

output: (40,)

predict.shape

output: (40,1)

confusion_matrix(y_test, predict)

Error: ValueError: Classification metrics can't handle a mix of binary and continuous targets

I am applying Confusion Metrix at the prediction of CNN model on textual data, but an error message got that confusion matrix cannot handle mix of binary and continuous data

This sometimes happens when a classifier produces quasi-probabilistic estimates, such as when a crossentropy loss is used in a neural.network.

import numpy as np

y_test = np.array([0, 1, 0])
y_pred = np.array([[0.1], [0.7], [0.2]])

print(y_test.shape, y_pred.shape)
# (3,)    (3, 1)

This is solved by thresholding the predictions. 0.5 is a common choice:

from sklearn.metrics import confusion_matrix

confusion_matrix(
  y_test,
  (y_pred > 0.5),
)
# array([[2, 0],
#        [0, 1]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM