Complete the code to import the function that computes a confusion matrix.
from sklearn.metrics import [1]
The confusion_matrix function from sklearn.metrics computes the confusion matrix for classification results.
Complete the code to compute the confusion matrix from true and predicted labels.
cm = confusion_matrix([1], y_pred)The first argument to confusion_matrix is the true labels, usually named y_true.
Fix the error in the code to display the confusion matrix as a heatmap using matplotlib.
import matplotlib.pyplot as plt import seaborn as sns sns.heatmap([1], annot=True, fmt='d') plt.xlabel('Predicted') plt.ylabel('Actual') plt.show()
The heatmap function needs the confusion matrix data, which is stored in the variable cm.
Fill both blanks to calculate precision and recall from the confusion matrix.
precision = cm[[1], [2]] / cm[:, [2]].sum()
Precision is calculated as true positives (row 1, col 1) divided by all predicted positives (sum of column 1).
Fill all three blanks to calculate recall from the confusion matrix.
recall = cm[[1], [2]] / cm[[3], :].sum()
Recall is true positives (row 1, col 1) divided by all actual positives (sum of row 1).