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

Confusion Matrix

A confusion matrix breaks a classifier's predictions into four exact categories — correct and incorrect predictions for each class — and is the raw material every other classification metric in this section is built from.

The Four Categories

Predicted Positive Negative Actual TP = 15 True Positive FN = 5 False Negative FP = 10 False Positive TN = 70 True Negative Positive Negative

Diagonal cells (TP, TN) are correct; off-diagonal cells (FP, FN) are the two distinct ways to be wrong.

TermMeaning
True Positive (TP)Predicted positive, actually positive — correct
False Positive (FP)Predicted positive, actually negative — a "false alarm"
False Negative (FN)Predicted negative, actually positive — a "miss"
True Negative (TN)Predicted negative, actually negative — correct

Worked Example

A fraud classifier tested on 100 transactions: 20 are actually fraudulent, 80 are legitimate. The model catches 15 of the 20 fraud cases (missing 5), and incorrectly flags 10 of the legitimate transactions.

\[ TP=15, \quad FN=5, \quad FP=10, \quad TN=70 \]

This exact confusion matrix is reused throughout Accuracy, Precision, Recall and F1-Score to compute each metric consistently from the same numbers.

from sklearn.metrics import confusion_matrix
import numpy as np

# 20 actual positives (1), 80 actual negatives (0)
y_true = [1]*20 + [0]*80
y_pred = [1]*15 + [0]*5 + [1]*10 + [0]*70   # 15 correct positives, 5 missed, 10 false alarms, 70 correct negatives

cm = confusion_matrix(y_true, y_pred)
print(cm)
# [[70 10]
#  [ 5 15]]  -- scikit-learn's default row/column order: [TN, FP], [FN, TP]

Watch the row/column convention: scikit-learn's confusion_matrix() outputs \([[TN,FP],[FN,TP]]\) by default (negatives first) — different from the diagram above (positives first), which follows the more common textbook convention. Always check labels= and the axis order before reading off values.

Visualizing With a Heatmap

from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt

ConfusionMatrixDisplay.from_predictions(y_true, y_pred)
plt.show()

Multi-Class Confusion Matrices

For more than 2 classes, the matrix expands to \(C \times C\) — each row is an actual class, each column a predicted class, and the diagonal represents correct predictions per class. Off-diagonal cells reveal exactly which classes get confused with which others, information a single accuracy number completely hides.

Practical Use Cases

  • Understanding how a classifier is wrong, not just how often — critical for deciding whether false positives or false negatives are the bigger business problem
  • The direct source data for every classification metric on this page's related notes

Common Mistakes

  • Misreading scikit-learn's row/column order — always explicitly check, rather than assuming a particular convention.
  • Looking only at overall accuracy from the matrix without examining which specific error type (FP vs FN) dominates.

Interview Relevance

Q: "Why is a confusion matrix more informative than accuracy alone?" It separates the two distinct ways a classifier can be wrong — false positives and false negatives — which usually carry very different real-world costs; accuracy collapses both into a single number that hides which specific error type is actually happening.

Practice Question

A medical test's confusion matrix shows FN=2, FP=40, out of 100 actual negatives and 10 actual positives. Which error type is the model making far more often, and would that be more or less acceptable for a screening test?

Want to go beyond the notes?

Join CodingNow 2.0's Machine 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 Machine Learning.

This free note from CodingNow 2.0 explains Confusion Matrix in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine 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