Python ML The Confusion Matrix
A confusion matrix is a two-by-two grid that sorts predictions into four boxes. The grid shows which mistakes a model makes, not just how many.
Build the grid
confusion_matrix() takes the true labels first. The grid shows 39 true negatives and 69 true positives.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
model = DecisionTreeClassifier(max_depth=3, random_state=42)
model.fit(X_train, y_train)
print(confusion_matrix(y_test, model.predict(X_test)))Name the four boxes
ravel() flattens the grid into four counts. The order is true negatives, false positives, false negatives, true positives.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
model = DecisionTreeClassifier(max_depth=3, random_state=42)
model.fit(X_train, y_train)
tn, fp, fn, tp = confusion_matrix(y_test, model.predict(X_test)).ravel()
print(tn, fp, fn, tp)Accuracy from the grid
Accuracy is the right boxes over all boxes. The grid gives 108 right answers out of 114 rows.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
model = DecisionTreeClassifier(max_depth=3, random_state=42)
model.fit(X_train, y_train)
tn, fp, fn, tp = confusion_matrix(y_test, model.predict(X_test)).ravel()
print(round((tp + tn) / (tp + tn + fp + fn), 3))Example
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
model = DecisionTreeClassifier(max_depth=3, random_state=42)
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("Accuracy:", round(accuracy_score(y_test, pred), 3))
print("Confusion matrix:")
print(confusion_matrix(y_test, pred))
tn, fp, fn, tp = confusion_matrix(y_test, pred).ravel()
print("True negatives:", tn)
print("False positives:", fp)
print("False negatives:", fn)
print("True positives:", tp)Key points
- Confusion matrix: a two-by-two grid of true and predicted labels
- True positive: a positive case the model calls positive
- True negative: a negative case the model calls negative
- False positive: a negative case the model calls positive
- False negative: a positive case the model calls negative
- Use confusion_matrix() to build the grid from true and predicted labels
- Rows hold the true labels. Columns hold the predicted labels.
In this example, breast cancer measurements fill the table, and label 1 means malignant. The test set is the 20 percent the split held back. A tree with max_depth=3 predicts the test rows. accuracy_score() gives 0.947, and the grid shows 39 true negatives, 4 false positives, 2 false negatives, and 69 true positives. The accuracy looks strong, yet the grid shows 6 mistakes, and 2 of them are missed malignant cases.
Note
Accuracy alone hides the story, because a model can score well while missing one kind of case.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What is a false negative?