Python Forum

Full Version: Print confusion matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, I have tried my best to output the confusion matrix. but it didn't work either. I don't know what coding can produce the output for the confusion matrix.
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(confusion_matrix)
the output:
Error:
<function confusion_matrix at 0x7fb8e9bccca0>
I just want my coding show from the attachment but don't get it. Any help I will appreciate. Thanks
(Apr-18-2023, 03:53 PM)MrSonoa Wrote: [ -> ]hi, I have tried my best to output the confusion matrix. but it didn't work either. I don't know what coding can produce the output for the confusion matrix.
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(confusion_matrix)
the output:
Error:
<function confusion_matrix at 0x7fb8e9bccca0>
I just want my coding show from the attachment but don't get it. Any help I will appreciate. Thanks


It looks like you're trying to print the function itself rather than the result of calling the function. Here's how you can call the print_confusion_matrix function to print the confusion matrix:

predict_x = model.predict(X)
classes_x = np.argmax(predict_x, axis=1)
print_confusion_matrix(y_test, classes_x)
Make sure to replace y_test with your actual test labels. The classes_x variable should contain the predicted class labels.

Also, make sure that you have imported the necessary libraries at the beginning of your code:


from sklearn.metrics import confusion_matrix
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
This code assumes that you have already trained your model and split your data into training and testing sets. If you haven't done so, you'll need to do that first before calling predict on your model.

I hope this helps! Let me know if you have any further questions.