Editor

Read a Confusion Matrix

Intermediate · Write the code · from Python Classification: Build a Classifier with scikit-learn

From the lesson: Python Classification: Build a Classifier with scikit-learn

Your email inbox sorts messages into "spam" or "not spam." A bank flags transactions as "fraudulent" or "legitimate." A doctor classifies a tumor as "benign" or "malignant." These are all classification problems - the model predicts which category something belongs to.

Read the lesson

Train a LogisticRegression(random_state=42) on the data. Print the confusion matrix, then on the next line print the number of false positives (predicted 1 but actually 0). The confusion matrix layout is [[TN, FP], [FN, TP]].

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
X, y = make_classification(n_samples=200, n_features=4,
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# Train, get confusion matrix, print it and the false positives

More Classification exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz