liveBook · Manning

Confusion matrix

As explained in Statistics Every Programmer Needs

A confusion matrix is a table that summarizes classification results by cross-tabulating actual classes against predicted classes.

A confusion matrix evaluates a classification model by cross-tabulating actual labels against predicted labels. For binary classification, it records true positives, true negatives, false positives, and false negatives, separating correct predictions from each type of error. section 5.2.4 section 6.3.4

Read the four cells as follows:

  • True positive (TP): actual class 1 and predicted class 1. section 6.3.4
  • True negative (TN): actual class 0 and predicted class 0. section 6.3.4
  • False positive (FP): actual class 0 but predicted class 1. section 6.3.4
  • False negative (FN): actual class 1 but predicted class 0. section 6.3.4

Constructing and interpreting a matrix

With pandas, `pd.crosstab(raisins_subset['Class'], raisins_subset['prediction'])` produces [[397, 53], [66, 384]]. Thus TN = 397, FP = 53, FN = 66, and TP = 384. These counts yield 85.33% sensitivity, 88.22% specificity, 86.78% accuracy, and 13.22% misclassification. section 5.2.4

Table 5.1 Confusion matrix for raisin classification. A confusion matrix stores classification metrics from a regression with actual and predicted binary outcomes.
Predicted
Kecimen (0)
Besni (1)
Actual
Kecimen (0)
tn
fp
Besni (1)
fn
tp

Use the table to trace actual classes by row and predicted classes by column, then identify each error type. section 5.2.4

How Statistics Every Programmer Needs covers it

How deep it goes
Defines binary cells, demonstrates pandas and scikit-learn construction, and derives sensitivity, specificity, accuracy, and error rates.
Examples
Uses Turkish raisin classification and fourth-down conversion prediction, with pandas, scikit-learn, and explicit cell counts.
What it assumes
Assumes binary class labels and model predictions, while defining the four confusion-matrix outcomes explicitly.
What you can do after
Diagnose classification behavior by calculating error types and comparing performance across classes.
How it connects
Connects classification models with logistic regression, decision trees, random forests, accuracy, sensitivity, specificity, and ROC analysis.

Also explained in

Compare how 14 books explain confusion matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.