Skip to main content

Confusion Matrix and Basic Metrics

Topic — In classification, a single overall accuracy figure frequently conceals catastrophic systematic failures. When data is imbalanced, a trivial model that always predicts the majority class can boast a 99% accuracy score while failing to catch a single positive case. This guide explores the foundational 2x2 Confusion Matrix, extracts the core counts (TP, TN, FP, FN), dissects the Accuracy Paradox, and establishes majority-class baseline benchmarks.


1. The Core Concept​

Every binary classification decision falls into one of four mutually exclusive outcomes formed by crossing the Actual Ground Truth with the Model Prediction:

  1. True Positive (TP): Ground truth is Positive (11); model predicts Positive (11). Correct hit.
  2. True Negative (TN): Ground truth is Negative (00); model predicts Negative (00). Correct rejection.
  3. False Positive (FP): Ground truth is Negative (00); model predicts Positive (11). Type I Error (False Alarm).
  4. False Negative (FN): Ground truth is Positive (11); model predicts Negative (00). Type II Error (Missed Detection).

These four counts form the Confusion Matrix:

Total Observations (N)=TP+TN+FP+FN\text{Total Observations } (N) = \text{TP} + \text{TN} + \text{FP} + \text{FN}

tip

If you only take one thing from this page

The diagonal elements (TP+TN\text{TP} + \text{TN}) represent correct classifications. The off-diagonal elements (FP+FN\text{FP} + \text{FN}) represent errors. Never evaluate a classifier using accuracy alone without first verifying the zero-rule majority baseline.


2. Interactive Confusion Matrix & Metrics Calculator​

Experiment with different error distributions or click preset scenarios to see how Accuracy, Sensitivity, Specificity, and Precision update in real time:

Interactive 2x2 Matrix and Metrics Simulator

Select a scenario preset or manually edit cell counts below:

Actual \ Predicted
Predicted Positive (1)
Predicted Negative (0)
Actual Pos (1)
True Positive (TP)
False Negative (FN - Type II)
Actual Neg (0)
False Positive (FP - Type I)
True Negative (TN)
Overall Accuracy
87.0%
(TP + TN) / N
Majority Baseline
50.0%
Zero-Rule Guess
Recall (Sensitivity)
84.0%
TP / Actual Pos
Precision
89.4%
TP / Predicted Pos
Specificity
90.0%
TN / Actual Neg

3. Dissecting the Four Quadrants​

In scikit-learn (sklearn.metrics.confusion_matrix), the default axis orientation is:

  • Rows: Actual True Class (yy)
  • Columns: Predicted Class (y^\hat{y})

  • The Scenario: Model predicted 11; Truth is 11.
  • Real-world Example: A spam email correctly routed to the Junk folder, or a cancer patient correctly flagged for urgent oncology review.
  • Goal: Maximize TP.

4. The Accuracy Paradox Deep-Dive​

Accuracy is defined as the proportion of total decisions that were correct:

Accuracy=TP+TNTP+TN+FP+FN=CorrectTotal\text{Accuracy} = \frac{\text{TP} + \text{TN}}{\text{TP} + \text{TN} + \text{FP} + \text{FN}} = \frac{\text{Correct}}{\text{Total}}

While intuitive for balanced classes, accuracy fails on imbalanced distributions:

The 99% Useless Model​

Imagine a fraud detection system with 10,000 transactions where only 50 are fraudulent (0.5% fraud rate):

  • A trivial model that predicts non-fraudulent on every transaction scores: Accuracy=0+995010000=99.5%\text{Accuracy} = \frac{0 + 9950}{10000} = 99.5\%
  • Despite its dazzling 99.5%99.5\% accuracy score, it missed every single fraudulent transaction (Recall=0%\text{Recall} = 0\%).

The Zero-Rule Baseline Rule​

Before accepting any accuracy figure, always compute the Majority-Class Baseline:

Baseline Accuracy=max⁡(Actual Positives,Actual Negatives)Total\text{Baseline Accuracy} = \frac{\max(\text{Actual Positives}, \text{Actual Negatives})}{\text{Total}}

A model is only providing real diagnostic utility if its accuracy significantly outperforms this zero-effort majority baseline.


5. Implementation Lab​

tip

Lab Exercise: Confusion Matrices & Baseline Comparisons

An interactive, fully functional Google Colab / Jupyter Notebook is available to compute confusion matrices via scikit-learn, extract quadrant counts, and evaluate the Accuracy Paradox live in your browser.

Open In Colab

How to run the lab:

  1. Click the "Open In Colab" badge above to launch the interactive notebook.
  2. Run cells sequentially to extract counts and compute baseline comparisons.

Key Experiments to Run:

  • Experiment 1 (Extracting Quadrants via cm.ravel()): Extract TN,FP,FN,TPTN, FP, FN, TP from a 20-sample spam filter and verify diagonal accuracy.
  • Experiment 2 (The 1% Disease Paradox): Benchmark a dummy classifier on 1,000 patients with a 1% prevalence rate to witness 99.0% accuracy with zero recalled positives.
  • Experiment 3 (Measuring Lift Over Baseline): Calculate genuine lift over majority guessing on an exam dataset (83.6%83.6\% accuracy vs 61.5%61.5\% baseline   ⟹  +22.1%\implies +22.1\% lift).

6. Common Mistakes​


  • The Misconception: "True Positives are always located in the top-left corner of the confusion matrix."
  • Wrong Thinking: Assuming all software libraries use the same matrix layout.
  • The Right Principle: Scikit-learn puts Actual rows on the vertical axis and Predicted columns on the horizontal axis (ytrue,ypredy_{\text{true}}, y_{\text{pred}}), meaning TN\text{TN} is top-left and TP\text{TP} is bottom-right. Medical literature often swaps this orientation. Always read the axis labels first!

7. Summary​

Matrix Foundations

  • Diagonal Elements: TP\text{TP} and TN\text{TN} represent correct hits and correct rejections.
  • Type I Error (FP): False alarm. Predicted positive for an actual negative.
  • Type II Error (FN): Missed event. Predicted negative for an actual positive.
  • Accuracy Formula: TP+TNTotal\frac{\text{TP} + \text{TN}}{\text{Total}}. Treats all error types identically.

Practical Diagnostics

  • Accuracy Paradox: On imbalanced datasets, trivial majority guessing yields inflated accuracy with zero recall.
  • Baseline Anchor: Always compare accuracy to max⁡(P,N)Total\frac{\max(P, N)}{\text{Total}}.
  • Axis Check: In scikit-learn, rows are actual true labels and columns are predictions.
  • Asymmetric Costs: Real-world applications demand tracking Precision and Recall separately.

info

Key Takeaways

  • Confusion matrix is the source of truth: All classification metrics (Precision, Recall, F1, Specificity, ROC-AUC) are derived directly from these four quadrant counts.
  • Accuracy lies on imbalanced data: When classes are skewed, high accuracy can coincide with a totally useless model.
  • Zero-Rule anchor: A model must show statistically significant lift above the majority class prevalence to provide real engineering value.

Next Section: Precision and Recall - mastering the fundamental tradeoff between false alarms and missed detections.


8. Active Recall​

Test your understanding by answering first, then clicking to reveal the underlying principles.

Interactive Checkpoint Quiz​

Interactive Checkpoint: Diagnosing the Accuracy Paradox

A airport security scanner tests 100,000 carry-on bags. 50 bags contain prohibited weapons. The scanner predicts 'Safe' on all 100,000 bags. What is the scanner's accuracy, and what is its True Positive rate (Recall)?

Review Flashcards​

1. [THEORY] How are the four quadrants of a confusion matrix mapped in scikit-learn when calling cm.ravel()?

  • In scikit-learn, binary confusion matrices are ordered by class labels [0,1][0, 1]:
    • Row 0: Actual Negatives (y=0y=0)   ⟹  \implies Columns [0,1][0, 1] are TN,FP\text{TN}, \text{FP}.
    • Row 1: Actual Positives (y=1y=1)   ⟹  \implies Columns [0,1][0, 1] are FN,TP\text{FN}, \text{TP}.
  • Calling tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel() unpacks them in exact row-major order: Index 0→TN,Index 1→FP,Index 2→FN,Index 3→TP\text{Index } 0 \to \text{TN}, \quad \text{Index } 1 \to \text{FP}, \quad \text{Index } 2 \to \text{FN}, \quad \text{Index } 3 \to \text{TP}
2. [ANALYZE] Why is a Type II error (False Negative) typically considered far more dangerous than a Type I error (False Positive) in medical diagnostics?

  • Type I Error (False Positive): A healthy patient is flagged as potentially having disease. The consequence is a follow-up test, temporary anxiety, and slight medical expense.
  • Type II Error (False Negative): A patient with an aggressive tumor is told they are healthy. The consequence is missed treatment, disease progression, and avoidable mortality.
  • Because the cost of missing a true positive is catastrophic, diagnostic models prioritize high Recall (minimizing FN\text{FN}) over high Precision.
3. [MATH] Given TN = 85, FP = 15, FN = 5, TP = 95, calculate Overall Accuracy and Specificity.

  • Total Observations: N=85+15+5+95=200N = 85 + 15 + 5 + 95 = 200.
  • Accuracy: Accuracy=TP+TNN=95+85200=180200=0.90(90.0%)\text{Accuracy} = \frac{\text{TP} + \text{TN}}{N} = \frac{95 + 85}{200} = \frac{180}{200} = 0.90 \quad (90.0\%)
  • Specificity (True Negative Rate): Specificity=TNActual Negatives=TNTN+FP=8585+15=85100=0.85(85.0%)\text{Specificity} = \frac{\text{TN}}{\text{Actual Negatives}} = \frac{\text{TN}}{\text{TN} + \text{FP}} = \frac{85}{85 + 15} = \frac{85}{100} = 0.85 \quad (85.0\%)
4. [PRACTICE] How do you compute the baseline accuracy of a dataset containing 850 non-churners and 150 churners?

  • Majority Class: Non-churners with 850 samples.
  • Total Samples: 850+150=1000850 + 150 = 1000.
  • Baseline Zero-Rule Accuracy: Baseline=8501000=0.85(85.0%)\text{Baseline} = \frac{850}{1000} = 0.85 \quad (85.0\%)
  • A churn prediction model that achieves 86.0%86.0\% accuracy provides almost zero value (+1.0% lift over blind guessing).
5. [THEORY] Can accuracy ever be higher than both Precision and Recall simultaneously?

  • Yes: If a dataset has heavy negative class dominance (e.g. 90% negatives) and the model accurately classifies negatives (TN\text{TN} is high), while missing some positives (FN\text{FN} is moderate) and making a few false alarms (FP\text{FP} is low).
  • Accuracy will be buoyed by the large TN\text{TN} count, even if Precision and Recall on the rare positive class are mediocre.