Predict a category
Train classifiers, read confusion matrices, and choose thresholds for meaningful tradeoffs. This module has 3 lessons and takes about 1h 15m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 12 in the Learning LabTurn a probability into a decision
Train logistic regression to identify synthetic machine fault patterns.
Classification predicts a category such as normal or fault. Logistic regression learns a probability for a class. In predict_proba(X), each column follows the order in model.classes_. Our labels are 0 for normal and 1 for fault.
A threshold turns probabilities into decisions. At 0.5, a fault probability of at least 0.5 becomes class 1. Lowering the threshold flags more machines, usually catching more faults and creating more false alarms. A probability from this tiny synthetic sample is not a validated safety estimate.
See it in action
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
# Synthetic independent machine vibration and temperature measurements.
X = np.array([[1, 30], [2, 35], [1.5, 32], [2.5, 40],
[5, 60], [6, 70], [5.5, 65], [7, 75]])
y = np.array([0, 0, 0, 0, 1, 1, 1, 1])
model = make_pipeline(StandardScaler(), LogisticRegression(random_state=42))
model.fit(X, y)
new_machines = np.array([[2, 36], [4, 52], [6, 68]])
fault_probability = model.predict_proba(new_machines)[:, 1]
flags = (fault_probability >= 0.5).astype(int)
print("Class order:", model.classes_)
print("Fault probabilities:", fault_probability.round(3))
print("Inspection flags:", flags)Probabilities and decisions are different outputs. Make the threshold explicit.
Read errors when positives are rare
Calculate precision and recall for a synthetic review queue and recognize misleading accuracy.
Imagine a synthetic appointment review queue where only a few records need follow-up. A model that says no follow-up for everyone may have high accuracy while missing every positive case. Count the types of errors to understand its behavior.
Precision asks: of the records flagged, how many truly needed follow-up? Recall asks: of the records needing follow-up, how many did we flag? A confusion matrix shows true negatives, false positives, false negatives, and true positives. These invented labels are for learning, not medical triage.
See it in action
import numpy as np
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score
# Synthetic administrative review labels. No patient data or clinical use.
actual = np.array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1])
predicted = np.array([0, 0, 0, 1, 0, 0, 0, 0, 1, 1])
tn, fp, fn, tp = confusion_matrix(actual, predicted, labels=[0, 1]).ravel()
print("TN, FP, FN, TP:", tn, fp, fn, tp)
print("Accuracy:", accuracy_score(actual, predicted))
print("Precision:", precision_score(actual, predicted, zero_division=0))
print("Recall:", recall_score(actual, predicted, zero_division=0))When positives are rare, inspect missed cases and false alarms alongside accuracy.
Choose a threshold on validation data
Compare synthetic review thresholds using explicit precision and recall requirements.
A transaction-review system may have limited staff and a cost for missed cases. Choose a threshold using validation data and a stated goal, such as recall of at least 75%. Inspect the precision and number of flags at each candidate threshold.
Do not choose the threshold on the final test set. After the model and threshold are fixed, evaluate them together once on the test set. Synthetic review scores cannot establish real financial risk. In a real project, check score calibration and performance across relevant groups and time periods.
See it in action
import numpy as np
from sklearn.metrics import precision_score, recall_score
# Synthetic validation labels and review probabilities. Not financial advice.
actual = np.array([0, 1, 0, 1, 1, 0, 1, 0])
probabilities = np.array([0.1, 0.8, 0.4, 0.6, 0.35, 0.2, 0.9, 0.55])
for threshold in [0.3, 0.5, 0.7]:
flags = probabilities >= threshold
precision = precision_score(actual, flags, zero_division=0)
recall = recall_score(actual, flags, zero_division=0)
print("Threshold:", threshold, "Flags:", flags.sum(),
"Precision:", round(precision, 2), "Recall:", round(recall, 2))Choose thresholds with a clear goal, then test the complete decision rule.
Free, open and run by a nonprofit.
This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.