Suppose a sensor produces a response that depends on force and temperature. If the relationship were known exactly, we could write a fixed formula. In practice, the sensor may have measurement noise, manufacturing variation, or a response that is hard to derive from first principles. We can collect examples of inputs and responses, then estimate a useful prediction rule.
Machine learning studies procedures that use data to improve performance on a specified task, measured by an appropriate criterion. The definition requires a task and a criterion: collecting data or using a complicated program does not by itself establish useful learning. A model may fit recorded examples and still fail when conditions change.
The central question of this book is therefore: how do we construct and evaluate a prediction procedure whose behavior we understand? The first complete answer appears in Chapter 14. Before that, we build the language, numerical tools, and experimental habits needed to make the answer defensible.
1.2 Learning objectives and prerequisites
Only elementary arithmetic is assumed here. By the end you should be able to:
Define an observation, feature, target, parameter, model, and prediction.
Distinguish regression, classification, unsupervised learning, and reinforcement learning.
State what information is available when a prediction is made.
Explain training, validation, testing, generalization, and leakage in ordinary language.
Distinguish synthetic demonstrations from evidence about a real application.
The mathematical and coding prerequisites are taught in the next chapters. You do not need to understand an optimizer to formulate a learning problem correctly.
1.3 Start with an observation and an intended use
An observation is one unit represented by a row. It might be a calibration measurement, a flower, one walking trial, one participant, or a time window. These are different units. Thirty windows extracted from the same trial are thirty rows but not thirty independent participants.
A feature is an input available to the prediction procedure. A target is the output we want to predict in supervised learning. A label is often used for a class target, although some software uses it more broadly. Feature definitions include units, timing, and processing: a mean computed over the complete future trial cannot be used to predict its start in real time.
Proposed task
Observation
Candidate inputs
Target
Intended use
Sensor calibration
One controlled reading
Force (N), temperature (degree Celsius)
Response (V)
New readings inside the calibrated range.
Flower classification
One flower
Length and width measurements
Species category
Flowers drawn from the intended population.
Walking-condition classification
One recorded trial
Permitted motion summaries
A documented condition label
New trials or new participants, as explicitly specified.
Next-hour forecasting
One forecast origin
Measurements available by that time
A future measurement
Later time periods.
The split and evaluation must reflect the intended use. Performance on a new window from a known participant does not answer the same question as performance on an unseen participant. Conditions such as carrying a bag are not automatically labels for disease. Before modeling, ask what the labels actually establish.
1.4 Three broad learning settings
1.4.1 Supervised learning
Training examples include both inputs and known targets. The procedure estimates a rule that maps an input to a prediction. Regression predicts a continuous quantity such as voltage or duration. Classification predicts a category, or probabilities assigned to categories, such as a documented walking condition.
Encoding classes as integers does not turn them into a continuous physical quantity. Species labels 0, 1, and 2 have no natural numerical spacing. Minimizing squared error between these arbitrary codes generally does not express the classification task we intended.
1.4.2 Unsupervised learning
The fitting task does not provide the target used in supervised prediction. For example, clustering groups observations according to selected similarities, and dimensionality reduction constructs a more compact representation. A cluster is a property of a chosen representation, distance, and algorithm. It is not automatically a clinically meaningful group or a true class.
Evaluation must match the purpose: reconstruction error, stability, downstream usefulness, and verified external labels answer different questions. The absence of training labels does not remove the need to define success.
1.4.3 Reinforcement learning
An agent selects actions, receives feedback such as rewards, and influences subsequent observations. For example, a simulated controller may choose actions to move toward a goal. Decisions affect later opportunities, so evaluating a sequence of actions differs from scoring independent labeled rows. Real-world control also introduces constraints not established by a successful simulated experiment. This is a later branch of the book.
These settings describe the learning problem. A single application may use several of them, but it is useful to identify the role each method plays. The official scikit-learn guide provides a map of supervised and unsupervised methods (User Guide, n.d.); this milestone develops one supervised regression workflow.
1.5 Model, parameters, and training
A model family specifies the prediction forms we are willing to consider. For one input \(x\), a simple family is
\[\hat y=b+wx.\]
Here \(\hat y\) is the predicted target, \(b\) is an intercept, and \(w\) is a slope. A particular choice of \(b\) and \(w\) gives one model. These adjustable quantities are parameters. If \(x\) is force in N and \(\hat y\) is voltage in V, then \(b\) is in V and \(w\) is in V/N. Units help catch an invalid equation before any code runs.
Training or fitting uses training examples to estimate parameters. A loss function assigns a numerical penalty to prediction errors. For regression, one possible loss is the average squared error:
\[L=\frac1n\sum_{i=1}^n(y_i-\hat y_i)^2.\]
\(n\) is the number of observations, \(y_i\) is observation \(i\)’s actual target, and \(\hat y_i\) is its predicted target. The square prevents positive and negative errors from canceling. The sum symbol means add the term for each row; Chapter 5 develops this notation. A fitting method searches for parameters that reduce this training objective.
A hyperparameter controls a modeling or training choice outside the ordinary fitted parameter vector, such as a tree’s allowed depth or an optimizer’s learning rate. Some choices control model flexibility; others control numerical accuracy. They should not all be interpreted as the same kind of tuning.
1.6 A tiny prediction calculation
Suppose a hypothetical calibration model is \(\hat y=0.5+0.1x\). At forces \(0,5,10\) N, it predicts \(0.5,1.0,1.5\) V. These are illustrative chosen coefficients, not learned results. The observed values below are also invented for arithmetic practice.
The code separates prediction from scoring. Python syntax is introduced in Chapter 2, so first follow the comments and compare the values with the hand calculation.
# Step 1: Define one illustrative model and three invented observations.# Force values, in newtons, are inputs available before prediction.force_values_newtons = [0.0, 5.0, 10.0]# Observed responses, in volts, are targets used only to score this fixed model.observed_responses_volts = [0.6, 0.9, 1.7]# Intercept is the model prediction at zero force, in volts.intercept_volts =0.5# Slope describes voltage change per newton in this illustrative model.slope_volts_per_newton =0.1# Step 2: Predict and accumulate squared errors one observation at a time.# The running sum will become the numerator of mean squared error.sum_of_squared_errors =0.0for observation_index inrange(len(force_values_newtons)):# Select one input and its matched observed response. force_newtons = force_values_newtons[observation_index] observed_voltage = observed_responses_volts[observation_index]# Apply the fixed formula; no training occurs in this illustration. predicted_voltage = intercept_volts + slope_volts_per_newton * force_newtons# Error is observed minus predicted, expressed in volts. error_volts = observed_voltage - predicted_voltage sum_of_squared_errors = sum_of_squared_errors + error_volts **2print("Force (N):", force_newtons, "Prediction (V):", predicted_voltage)# Step 3: Average over the observations to score the fixed predictions.# Mean squared error has squared voltage units.mean_squared_error = sum_of_squared_errors /len(force_values_newtons)print("Mean squared error (V squared):", mean_squared_error)assertabs(mean_squared_error -0.02) <1e-12
Force (N): 0.0 Prediction (V): 0.5
Force (N): 5.0 Prediction (V): 1.0
Force (N): 10.0 Prediction (V): 1.5
Mean squared error (V squared): 0.01999999999999999
The errors are \(0.1,-0.1,0.2\) V; their squared sum is \(0.06\) V squared and the average is \(0.02\) V squared. We checked only arithmetic on these three examples. No held-out evaluation has occurred and no claim about a real sensor follows.
1.7 Why training performance is insufficient
Imagine recording the correct output beside every training input and looking it up. This can achieve perfect training performance while offering little guidance for an unseen input. Generalization is performance on appropriate new observations. A useful evaluation therefore withholds examples from fitting.
The basic roles are:
Training: estimate parameters and learned preprocessing statistics.
Validation: choose settings or compare candidate procedures. Cross-validation repeats this role across several development partitions.
Testing: assess the fixed selected procedure using observations that did not influence its choices.
Overfitting occurs when a procedure captures accidental training detail that does not help the intended new observations. Underfitting describes a procedure too restrictive to represent useful structure. Neither can be diagnosed from a low training loss alone. Chapter 12 develops these ideas with controlled examples.
Data leakage occurs when information unavailable to the intended prediction procedure influences fitting or selection. Examples include fitting a scaler on test rows, including a future outcome in the inputs, placing overlapping windows in training and testing, or repeatedly choosing settings based on test scores. See the official common-pitfalls guide for implementation examples.
1.8 A defensible project sequence
Before selecting an algorithm, write a short specification:
What is one observation and what is the intended population or deployment setting?
What target is available, how was it measured, and what does its definition support?
Which inputs are available at prediction time, in which units?
Which observations must stay together, and which time boundaries must be respected?
What simple baseline and error measure express success?
What training, validation, and final test procedure will answer that question?
Then inspect data quality, fit a small baseline, compare candidate models using development data, and evaluate the fixed procedure. A more complex algorithm cannot repair invalid labels or an evaluation that answers the wrong question. Large datasets can contain systematic errors as well as useful information.
For a research claim, distinguish association, prediction, and intervention. A predictor may exploit a stable association without identifying its cause. A successful educational experiment does not establish measurement validity, safety, or clinical usefulness. Those conclusions require evidence appropriate to the intended use.
1.9 Exercises and worked answers
A table contains 100 people, each with 20 walking windows. How many rows and how many participants does it contain? Which count matters for evaluating unseen-person deployment?
Is predicting walking duration regression or classification? What about predicting a documented walking-condition category?
A feature uses the maximum acceleration over the complete next minute. Can it be used at the start of that minute for real-time prediction?
A model has training MSE zero. What else must be checked before calling it useful?
You repeatedly inspect the test error to choose polynomial degree. What role is that set actually playing?
Why is a cluster with a low average walking speed not automatically a clinical diagnosis?
Answers:
There are 2000 rows and 100 people. Unseen-person evaluation must keep each person’s windows in a single partition; independent participant count matters for the scope of the evidence.
Duration is a continuous regression target. A condition category is classification, assuming the documented labels match that task.
No: it uses future information. Either redefine the available window or redefine prediction time and the task.
Evaluate appropriate held-out observations, check leakage, compare with a baseline, and examine whether error and data coverage meet the intended use.
It is functioning as validation data. It can no longer provide an untouched final assessment of those choices.
A cluster depends on features and modeling choices, and low speed has multiple possible explanations. Clinical meaning needs defensible labels and separate validation.
1.10 Revision summary
Begin with the task, observation unit, target, available inputs, and intended use.
Regression predicts continuous quantities; classification predicts categories or their probabilities.
Parameters are fitted from training examples; model and training settings must be chosen without consulting final test outcomes.
Loss guides fitting; evaluation measures how useful a fixed procedure is on appropriate new observations.
Fit preprocessing within the training boundary and respect participant/time structure.
Synthetic arithmetic demonstrations teach mechanisms. They do not establish real-world performance.
This book combines prose, equations, and executable examples so that reasoning can be inspected alongside computation. The original starter chapter cited Knuth’s discussion of literate programming; that authoring connection remains relevant here (Knuth 1984). Continue with Chapter 2 to learn the programming tools used by subsequent examples.