Classification Intuition
Read a little, play a little. No scary maths, and no rush.
Sorting things into buckets
Spam or ham? Churn or loyal? Malignant or benign? Whenever the answer is a category, you're classifying. The workhorse starter is logistic regression — a confusing name for a clean idea: model the probability of each class, then pick the highest. Despite "regression" in the name, it outputs categories.
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
model = LogisticRegression(max_iter=1000).fit(Xtr, ytr)
print(classification_report(yte, model.predict(Xte)))
Accuracy is a liar (when classes imbalance)
A fraud detector facing 99% legitimate transactions scores 99% accuracy by predicting "legit" always — perfect score, worthless model. The honest report card:
- Precision: of the flagged ones, how many were real? (False alarms cost trust.)
- Recall: of the real ones, how many did we catch? (Misses cost money/lives.)
- F1: their harmonic mean — one number that refuses to be gamed by imbalance.
Always report these for the rare class you actually care about — the fraud, the churners, the diagnoses.
Thresholds are business decisions
Models output probabilities; you choose the cutoff. Flag fraud above 0.3 if missing fraud is expensive; require 0.9 if false alarms wake people at night. Moving the threshold trades precision against recall — a product decision wearing math clothes, and interviewers love asking about it.
The metrics with real numbers
Out of 1,000 transactions, 20 are fraud. The model flags 30, and 15 of those really are fraud.
- Precision = 15 ÷ 30 = 50%. Half the alerts are real.
- Recall = 15 ÷ 20 = 75%. Three quarters of the fraud was caught; 5 slipped through.
- F1 = 2 × 0.5 × 0.75 ÷ (0.5 + 0.75) = 0.6.
Accuracy here is 98%, which sounds superb and hides the 5 missed frauds and the 15 false alarms entirely.
The confusion matrix
Those four counts form a small table called the confusion matrix: true positives (15 caught), false positives (15 false alarms), false negatives (5 missed), and true negatives (965 correctly left alone). sklearn.metrics.confusion_matrix(y_true, y_pred) prints it. Every classification metric is computed from these four numbers, so read the matrix first and the summary scores second.
More than two classes
Classifying support tickets into five topics works the same way; logistic regression and forests handle many classes out of the box. Report precision and recall per class, because a model can be excellent on common topics and poor on a rare one. When you need one summary number, the macro average treats every class equally, while the weighted average favors the common ones.
Can you trust the probabilities?
If a model says 0.8, does that event happen about 80% of the time? Not always. Forests in particular often produce poorly calibrated probabilities. If you set thresholds from probabilities, check a calibration curve, and if needed wrap the model in scikit-learn's CalibratedClassifierCV to correct them. Calibration matters most when probabilities drive real decisions, such as which transactions a person reviews.
Remember this
- Categories out → classification. Imbalanced data → precision/recall/F1, never raw accuracy.
- Thresholds encode business costs; choose them deliberately, not by default 0.5.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue