Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print confusion matrix
#1
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

Attached Files

Thumbnail(s)
   
Reply
#2
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 840 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to plot confusion matrix of multiclass classification Vaishali 0 1,364 Feb-10-2022, 02:34 PM
Last Post: Vaishali
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,387 May-03-2021, 06:30 AM
Last Post: Gribouillis
  confusion on print function volcano 3 3,656 Mar-11-2018, 10:41 AM
Last Post: snippsat
  matrix from matrix python numpy array shei7141 1 3,719 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020