🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Deep Learning Notes
Topic #144

Confusion Matrix

The confusion matrix is the foundation every classification metric in this category is built from — a simple table breaking down exactly which predictions were correct, and which specific kind of mistake was made when they weren't.

The 2×2 Table (Binary Classification)

Predicted PositivePredicted Negative
Actual PositiveTrue Positive (TP)False Negative (FN)
Actual NegativeFalse Positive (FP)True Negative (TN)
CellMeaning
True Positive (TP)Correctly predicted positive
True Negative (TN)Correctly predicted negative
False Positive (FP)Incorrectly predicted positive (a "false alarm" — Type I error)
False Negative (FN)Incorrectly predicted negative (a "miss" — Type II error)

Worked Example

A spam classifier evaluated on 100 emails (30 actually spam, 70 actually not spam): it correctly flags 24 of the 30 spam emails (TP=24, so FN=6), and incorrectly flags 7 legitimate emails as spam (FP=7, so TN=63).

Predicted SpamPredicted Not Spam
Actual SpamTP = 24FN = 6
Actual Not SpamFP = 7TN = 63

Every classification metric covered in the rest of this category — accuracy, precision, recall, F1, specificity — is computed directly from these four numbers.

Code

from sklearn.metrics import confusion_matrix
import numpy as np

y_true = np.array([1,1,1,1,1,0,0,0,0,0])   # 1 = spam, 0 = not spam (simplified small example)
y_pred = np.array([1,1,1,0,0,0,0,1,0,0])

cm = confusion_matrix(y_true, y_pred)
print(cm)
# [[TN FP]
#  [FN TP]]  -- note sklearn's default row/column order; always verify against your own label convention

Extending to Multi-Class

For \(K\) classes, the confusion matrix becomes a \(K\times K\) grid — row \(i\), column \(j\) counts how many examples with true class \(i\) were predicted as class \(j\). The diagonal holds every correct prediction; every off-diagonal cell represents a specific kind of confusion between two particular classes, often revealing which classes a model most often mixes up.

Common Mistakes

  • Confusing which axis represents "actual" versus "predicted" — different libraries and textbooks sometimes use different row/column conventions; always check a specific tool's documentation rather than assuming.
  • Stopping at just looking at the raw counts without computing any derived metric — the confusion matrix is a starting point; the next several notes turn it into single, comparable numbers.

Interview Relevance

Q: "What's the difference between a false positive and a false negative, using a medical diagnosis example?" A false positive is predicting a disease is present when it actually isn't (a healthy patient incorrectly flagged as sick) — a "false alarm." A false negative is predicting a disease is absent when it's actually present (a sick patient incorrectly cleared as healthy) — a "miss." Which error type is more costly is entirely context-dependent, and this exact distinction is why single metrics like accuracy alone are often insufficient.

Practice Question

Out of 200 patients, a diagnostic model correctly identifies 45 out of 50 actually-sick patients, and incorrectly flags 15 out of 150 actually-healthy patients as sick. Fill in the full 2×2 confusion matrix.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Confusion Matrix – FAQs

Quick answers about learning Confusion Matrix in Deep Learning.

This free note from CodingNow 2.0 explains Confusion Matrix in Deep Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Confusion Matrix, is 100% free with no signup required.
With focused practice, most students grasp Confusion Matrix in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now