-1

I have tried to run this code to output the confusion matrix. but there is an error when I run. it should come out with the matrix confusion diagram and its label.

Code:

#Output confusion matrix
import numpy as np
def print_confusion_matrix(y_true, y_pred):
    cm = confusion_matrix(y_true, y_pred)
    print('True positive = ', cm[0][0])
    print('False positive = ', cm[0][1])
    print('False negative = ', cm[1][0])
    print('True negative = ', cm[1][1])
    print('\n')
    df_cm = pd.DataFrame(cm, range(2), range(2))
    sn.set(font_scale=1.4) # for label size
    sn.heatmap(df_cm, annot=True, annot_kws={"size": 16}) # font size
    plt.ylabel('Actual label', size = 20)
    plt.xlabel('Predicted label', size = 20)
    plt.xticks(np.arange(2), ['Fake', 'Real'], size = 16)
    plt.yticks(np.arange(2), ['Fake', 'Real'], size = 16)
    plt.ylim([2, 0])
    plt.show()


threshold = 0.5
print_confusion_matrix(np.array(Y_val_org).astype(np.int32),(model.predict(X)>threshold).astype(np.int32))

Error:

118/118 [==============================] - 7s 61ms/step
ValueError: Classification metrics can't handle a mix of binary and multilabel-indicator targets

What coding do I need to change so that the matrix confusion is error-free? I have tried for a few days but it didn't work either

Sanjiro
  • 13
  • 1
  • 4

1 Answers1

0

This method has been removed, see https://discuss.tensorflow.org/t/sequential-object-has-no-attribute-predict-classes/10157

So an alternative would be:

import numpy as np

threshold = 0.5
print_confusion_matrix(np.array(Y_val_org).astype(np.int32), (model.predict(X)>threshold).astype(np.int32))
TanjiroLL
  • 1,354
  • 1
  • 5
  • 5
  • Sir, I try the alternative one but it show another error : Classification metrics can't handle a mix of binary and multilabel-indicator targets – Sanjiro Apr 19 '23 at 15:04
  • what the meaning 'list' object has no attribute 'dtype' ? I need to define first? – Sanjiro Apr 19 '23 at 15:08
  • It means that your Y_val_org is a list, not a numpy array as I supposed, which does not have an attribute of data type, I updated my answer to convert the list to numpy array first. – TanjiroLL Apr 19 '23 at 15:15
  • still not successful. Classification metrics can't handle a mix of binary and multilabel-indicator targets. I think my coding from the start not correct. I feel guilty for asking for help from you again. Thank you sir – Sanjiro Apr 19 '23 at 15:24
  • could you print both Y_val_org, and model.predict(X), then add them to question description? – TanjiroLL Apr 19 '23 at 15:29
  • You didn't write the values of Y_val_org, nor model.predict(X). – TanjiroLL Apr 19 '23 at 15:38
  • sorry what value I need to insert? am very first time in deep learning – Sanjiro Apr 19 '23 at 16:10