v9.1.1
Menu

Confusion Matrix

easyFREEModel EvaluationMachine Learning 1

Build a confusion matrix for binary classification results.

Given lists of true labels and predicted labels (each 0 or 1), compute the 2x2 confusion matrix:

[TNFPFNTP]\begin{bmatrix} TN & FP \\ FN & TP \end{bmatrix}

where:

  • TP (True Positive): predicted 1, actual 1
  • TN (True Negative): predicted 0, actual 0
  • FP (False Positive): predicted 1, actual 0
  • FN (False Negative): predicted 0, actual 1

Return the matrix as a 2D list [[TN, FP], [FN, TP]].

Example

Input
y_true = [1, 0, 1, 1, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1]
Output
[[2, 1], [1, 2]]
Reasoning
  • Inputs: y_true holds the actual class of each of 6 examples and y_pred the model's guess; position ii pairs truth yiy_i with prediction y^i\hat{y}_i. Here the pairs are (1,1),(0,0),(1,0),(1,1),(0,0),(0,1)(1,1), (0,0), (1,0), (1,1), (0,0), (0,1).
  • Classify each pair: truth 0 and prediction 0 is a true negative, which happens at positions 2 and 5, so TN=2TN = 2. Truth 0 but prediction 1 is a false positive, only at position 6, so FP=1FP = 1.
  • Truth 1 but prediction 0 is a false negative, at position 3, so FN=1FN = 1. Truth 1 and prediction 1 is a true positive, at positions 1 and 4, so TP=2TP = 2.
  • Check: every example lands in exactly one cell, and 2+1+1+2=62 + 1 + 1 + 2 = 6.
  • Output: rows are the actual class and columns the predicted class, giving [[2, 1], [1, 2]]. Notice that row 1 sums to 3, the number of actual 0s, and the diagonal TN+TP=4TN + TP = 4 counts the correct predictions, an accuracy of 4/6≈0.6674/6 \approx 0.667.

Constraints

  • y_true and y_pred are lists of 0s and 1s of equal length
  • Return a 2D list [[TN, FP], [FN, TP]]

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.