AI Wiki中文/EN

Supervised learning

监督学习

Define features and labels, fit a predictor, compare a simple baseline, and keep model selection separate from final evaluation.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

Learn a rule from examples with answers

A workshop records a sensor reading and whether a later inspection calls for rechecking an item. A supervised model receives past pairs of readings and labels, then predicts the label for a new reading. The inspector’s outcome supplies supervision; it need not be available when the prediction is made.

The label is part of the task definition. Predicting inspection duration is regression; predicting a recheck category is classification. The same input can support different tasks with different labels. Labels may come from people, instruments, or recorded events, and can be noisy or incomplete. D2L, Section 1.3.1

A fitted rule is judged on unseen examples

Represent the training data as pairs (xi,yi)(x_i,y_i). Features xix_i are available inputs, labels yiy_i are the target outcomes, and a predictor ff maps inputs to predictions. The hypothesis class F\mathcal F is the family of rules allowed by the model. A loss ℓ(y,f(x))\ell(y,f(x)) measures a prediction’s error according to the task.

Empirical risk minimization chooses a rule with low average training loss:

f^∈argmin⁡f∈FR^(f),R^(f)=1n∑i=1nℓ(yi,f(xi)).\hat f\in\operatorname*{argmin}_{f\in\mathcal F}\widehat R(f),\qquad \widehat R(f)=\frac1n\sum_{i=1}^{n}\ell(y_i,f(x_i)).

This is an objective, not a guarantee that a training algorithm finds a global minimum. We care about expected loss on the intended future population PP:

RP(f)=E(X,Y)∼P[ℓ(Y,f(X))].R_P(f)=E_{(X,Y)\sim P}[\ell(Y,f(X))].

The training average and future risk are different objects. A sufficiently flexible rule can memorize training labels while failing elsewhere. Generalization depends on the model, data volume, sampling process, regularization, and how closely evaluation matches intended use. MML, Sections 8.2.1–8.2.3

Use training data to fit candidates, validation data to choose among model settings, and a held-out test set for a final assessment of the chosen procedure. If test results repeatedly guide choices, that set is participating in selection. Separate evaluation is then needed for an honest assessment. MML, Section 8.6.1

Fit a threshold and compare a constant

This complete example uses deliberately small, invented sensor readings. We permit one rule family: readings greater than a learned threshold predict label 1. A constant classifier based on training-label frequency supplies a baseline. Threshold fitting examines candidate cuts using training data; validation selects between the two fitted methods; the selected method is then evaluated on test data.

import math

def error(model, data):
    if not data:
        raise ValueError("evaluation data must not be empty")
    return sum(model(x) != y for x,y in data)/len(data)

def fit_threshold(data):
    if not data or any(not math.isfinite(x) or y not in [0,1] for x,y in data):
        raise ValueError("finite readings and binary labels required")
    xs = sorted({x for x,y in data})
    cuts = [-math.inf]+[(a+b)/2 for a,b in zip(xs,xs[1:])]+[math.inf]
    return min(cuts, key=lambda t: (error(lambda x: int(x>t), data), t))

train = [(1,0),(2,0),(4,1),(6,1)]
validation = [(1.5,0),(3.5,1),(5,1)]
test = [(.5,0),(4.5,1),(7,1),(2.5,1)]
threshold = fit_threshold(train)
majority = max([0,1], key=lambda y: (sum(label==y for _,label in train), -y))
candidates = {'constant': lambda x: majority,
              'threshold': lambda x: int(x>threshold)}
selected = min(candidates, key=lambda name: (error(candidates[name], validation), name))
model = candidates[selected]
assert threshold == 3 and majority == 0
assert selected == 'threshold'
assert error(model, train) == 0
assert error(model, test) == .25

For nn training points, this simple implementation sorts readings and scans O(n)O(n) candidate thresholds, evaluating each on nn points: O(n2)O(n^2) training work and O(n)O(n) storage. More efficient scans exist. Prediction after fitting is O(1)O(1) per point. The constant classifier is cheaper still.

The selected threshold has zero training error but test error 1/41/4. The test reading 2.5 has label 1 and is misclassified. A single reading need not contain everything an inspection uses; a simple threshold need not match every label. These four test examples illustrate workflow, not a reliable industrial accuracy estimate.

Supervision does not supply the right task automatically

An input recorded only after inspection would be unavailable at prediction time. Including it can create leakage and apparently excellent evaluation. Fit preprocessing statistics using the appropriate training split, and keep records from the same underlying item together when required by the use case.

Historical labels may record an earlier decision rather than the outcome you actually want. Learning to imitate that decision can preserve its errors. Label definitions, disagreement, missing labels, and the cost of mistakes require inspection alongside model fitting.

Distribution shift can change sensor ranges, label frequencies, or relationships between inputs and labels. A held-out sample from yesterday’s process does not automatically validate tomorrow’s process. Report the population and conditions to which the evidence applies.

Supervision also does not imply that a deep network is necessary. A constant, a hand-written rule, a linear model, or a nearest-neighbor method may be sufficient. Compare the additional performance against data, computation, and maintenance costs.

Track what each step is allowed to know

  1. Does zero training error prove that a task is solved? Answer: no; the code’s test result is a direct counterexample.
  2. May the final test labels be used to select the threshold? Answer: not while treating that same test set as an untouched final evaluation.
  3. Is an inspection label guaranteed to be the desired real-world truth? Answer: no. Determine what produced the label and what it actually measures.
  4. Is predicting a real-valued duration classification because the data are labeled? Answer: no. Labeled numerical prediction is supervised regression.

From one example to a defensible experiment

Statistical learning theory studies how empirical performance relates to population risk under explicit assumptions. Practical learning adds feature construction, label quality, useful baselines, and evaluation design. Before changing architecture, identify which source of error a change is meant to address: insufficient inputs, mislabeled outcomes, an unsuitable rule family, poor optimization, or a mismatch between training and use.

Make a prediction, then check

You must choose between two fitted candidate models. Which data should serve that selection role?

Choose one answer

Learning objectives

  • Define inputs, labels, hypothesis classes, and loss; compare training error with population risk.
  • Complete a prediction workflow with a baseline and separate training, validation, and test data.

Prerequisites & connections

Explore related knowledge →

References

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. Dive into Deep Learning — Introduction
    Aston Zhang; Zachary C. Lipton; Mu Li; Alexander J. Smola · 2023

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.

  • Ran the separately split threshold workflow, confirming fitted threshold 3, constant baseline 0, validation selection, and final test error 1/4.
  • Checked empirical versus future-population risk, O(n²) threshold scanning, label provenance, leakage, and the limited evaluation scope of synthetic small samples.
  • Compared arguments, modeling assumptions, numbers, and exercise answers across languages; structural checks separately compare display formulas, code, references, and main sections. No independent human peer review.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.