pythontensorflowkerasgoogle-colaboratoryconfusion-matrix

Name 'predicted_class' is not defined


i try to run confusion matrix codes. before this I'am use

 print_confusion_matrix(Y_val_org, model.predict_classes(X))

but this function is deprecated then changes to

predict_x=model.predict(X) 
classes_x=np.argmax(predict_x,axis=1.

Now i have new error which is cannot show the confssion matrix label.

coding:


 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()

predict_x=model.predict(X) 
classes_x=np.argmax(predict_x,axis=1)
print(predicted_class)   

error:

NameError: name 'predicted_class' is not defined

I dont know where to defined predicted_class and there is still error when i run the codes


Solution

  • I guess it is typo. Just change classes_x with predicted_class

    predicted_class=np.argmax(predict_x,axis=1)
    print(predicted_class) 
    

    Thank you sir, i just notice it. 1 more question if i want to print the confusion matrix. i just need input it before predict or after predict?

    For best practice, you could print confusion matrix after predict it.