Supervised Learning Overview
Learning from labeled examples
Supervised learning learns a map from features to labeled outcomes: classification for discrete labels, regression for continuous values — with evaluation grounded in out-of-sample generalization.
What you will learn
- Define supervised learning and name its two main task families
- Identify the right family from the target's type
- Understand the shared estimator API: fit / predict / score
What is this?
Supervised learning predicts a label y from features X by learning a function on labeled examples. Classification predicts categorical labels (spam/not, fraud/legit); regression predicts continuous values (demand, price, latency). Every scikit-learn estimator shares the same contract: .fit(X, y), .predict(X), .score(X, y) — so the plumbing stays identical while the model changes.
Why do you need it?
- The labeled-data contract makes quality measurable and models comparable.
- Classification and regression cover most business problems — churn, credit risk, pricing, forecasting.
- Identical API + clean evaluation is what lets you swap algorithms in minutes.
- Baselines and audits are built on the label: ground truth is defined, not guessed.
Mental model
A teacher grading practice problems against an answer key
Training supervised models on labeled data
The student (model) sees problems (X) and the keyed answers (y), adjusts, then must solve NEW problems — grading is the evaluation phase.
How it works
- Frame: choose target y and features X from the business question.
- Split: train / validation / test with a label-aware (stratified) split.
- Fit: an estimator maps X -> y while minimizing a loss specific to the task.
- Evaluate: score on validation with the task's metric, tune, then lock the test result.
- Predict: deploy the mapping for new incoming rows.
Supervised learning contract.
Examples
Two tasks, one API
import numpy as npfrom sklearn.linear_model import LogisticRegression, LinearRegressionfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import roc_auc_score, r2_score rng = np.random.RandomState(0)X = rng.normal(size=(300, 3)) y_cls = (X[:, 0] + 0.7 * X[:, 1] > 0).astype(int)y_reg = 2 * X[:, 0] - X[:, 1] + rng.normal(0, 0.2, 300) Xtr, Xte, ytr, yte = train_test_split(X, y_cls, test_size=0.3, stratify=y_cls, random_state=1)clf = LogisticRegression().fit(Xtr, ytr)print("classification AUC:", round(roc_auc_score(yte, clf.predict_proba(Xte)[:, 1]), 3)) Xtr, Xte, ytr, yte = train_test_split(X, y_reg, test_size=0.3, random_state=1)reg = LinearRegression().fit(Xtr, ytr)print("regression R2:", round(reg.score(Xte, yte), 3))Same split/prepare pattern; only the estimator, target and metric change between the classification and regression branches.
In production
- Credit: logistic classifier on default risk; regression for exposure amounts.
- Ops: classifier for incident-threat triage, regression for capacity forecast.
- Marketing: classifier for churn propensity; regression for CLV.
Best practices
- Start interpretable (linear) and escalate only on evidence; stratify classification splits; log the label distribution so unlabeled or drifted data is caught.
Common mistakes
- Using the wrong task family (treating ordinal as classification); forgetting stratification on rare labels; judging by train accuracy alone; scoring regression with classification metrics.
Debugging tips
- Print label distribution before any training run.
- Check feature dtype consistency live vs train.
- Quick leak probe: train accuracy 0.999 on fresh data — check for time/ID leakage.
Interview checkpoint
Quick quiz
Predicting next-day inventory level is:
An estimator that separates spam (label 1) from ham (label 0) is: