A rare-class decision report.
Show why accuracy hides a rare class and how a threshold changes false alarms and misses.
Why this workflow exists.
When the positive class is uncommon, always predicting negative can achieve high accuracy while catching nothing. This recipe generates a rare positive label from a noisy signal, then fits a classifier on training data. It compares precision and recall at two thresholds on one held-out test set. The point is to read the tradeoff rather than declare one threshold best.
Thresholds are part of a decision policy. In a real project they would be selected using validation data and the known cost of each error type. This script fixes the thresholds ahead of time, so the test remains an illustration of their behavior rather than a tuned final result.
Code context. Python 3 with NumPy and scikit-learn installed. The script generates local synthetic data.
The whole example.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(12)
X = rng.normal(size=(600, 2))
score = 1.5 * X[:, 0] - 0.5 * X[:, 1] + rng.normal(size=600)
y = (score > 2.0).astype(int)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=12
)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
probability = model.predict_proba(X_test)[:, 1]
for threshold in (0.3, 0.6):
predicted = (probability >= threshold).astype(int)
print("threshold:", threshold)
print("precision:", precision_score(y_test, predicted, zero_division=0))
print("recall:", recall_score(y_test, predicted, zero_division=0))
print(confusion_matrix(y_test, predicted, labels=[0, 1]))What each stage does.
The generated score makes class 1 relatively rare. train_test_split keeps approximate class proportions with stratify=y. The pipeline fits scaling and logistic regression only on training rows. predict_proba returns one probability per class; [:, 1] selects the class-1 column for every test row.
The loop compares two fixed decision rules on the same probability estimates. A lower threshold generally flags more cases, often increasing recall and false alarms. A higher threshold generally flags fewer, often increasing precision while missing more positives. The confusion matrices show the actual counts behind the rates. zero_division=0 defines a result when no positive predictions exist, preventing a warning from hiding the rest of the report.
What the output means.
The printed metrics expose the precision-recall tradeoff for this generated sample. Their exact values are not universal and do not prescribe a threshold for any real task.
Where the example stops.
Probability calibration, population shift, and group-specific error rates would need separate evaluation before the scores supported an important decision.