9951 explained code solutions for 126 technologies


python-pytorchHow can I generate a confusion matrix using Python and PyTorch?


A confusion matrix is a table used to evaluate the performance of a classification model. It can be used to measure the accuracy of a model in predicting the correct class for each data point. To generate a confusion matrix using Python and PyTorch, you can use the sklearn.metrics.confusion_matrix function from the Scikit-Learn library.

For example, the following code block will generate a confusion matrix for a binary classification problem with two classes (class 0 and class 1):

import numpy as np
from sklearn.metrics import confusion_matrix

# Generate true labels
y_true = np.array([0, 0, 0, 1, 1, 1])

# Generate predicted labels
y_pred = np.array([0, 1, 0, 0, 1, 1])

# Generate confusion matrix
confusion_matrix(y_true, y_pred)

The output of the code block above is:

array([[2, 1],
       [0, 3]])

The above code block consists of the following parts:

  1. import numpy as np and from sklearn.metrics import confusion_matrix: These lines import the necessary libraries for generating the confusion matrix.

  2. y_true = np.array([0, 0, 0, 1, 1, 1]) and y_pred = np.array([0, 1, 0, 0, 1, 1]): These lines generate the true and predicted labels, respectively.

  3. confusion_matrix(y_true, y_pred): This line generates the confusion matrix using the true and predicted labels.

For more information on generating confusion matrices using Python and PyTorch, see the following links:

Edit this code on GitHub