Supervised Learning
Beginner 10 min read Supervised Learning

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.

supervised learningclassificationregressionfeatureslabelsscikit-learn estimator
Prerequisites: ML Introduction
Before we start

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
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A teacher grading practice problems against an answer key

Think of

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.

Mechanics

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.

Features X
Label y
fit()
predict()
Hands-on

Examples

Two tasks, one API

Two tasks, one API
import numpy as np
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.model_selection import train_test_split
from 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.

Real world

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.
Guidelines

Best practices

  • Start interpretable (linear) and escalate only on evidence; stratify classification splits; log the label distribution so unlabeled or drifted data is caught.
Watch out

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.
When things break

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.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Predicting next-day inventory level is:

2

An estimator that separates spam (label 1) from ham (label 0) is:

Go deeper

Official documentation