#75 · Machine Learning · Easy
⊣ Solve on deep-ml.comGenerate a confusion matrix for binary classification given true labels and predicted labels. The confusion matrix is a 2x2 matrix showing true negatives, false positives, false negatives, and true positives.
def confusion_matrix(y_true, y_pred):
tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
tn = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 0)
fp = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 1)
fn = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 0)
return [[tn, fp], [fn, tp]]