By the end
- Express linear regression as `nn.Linear`.
- Explain predictions, residuals, and mean squared error.
- Use batches to fit a weight and bias.
- Compare learned and hand-specified parameters.
Week 3 · Hands-on foundations
How can a familiar linear model become a complete learning workflow?
The long Week 3–4 regression notebook connects analytic regression, minibatch gradient descent, object-oriented code, real data, and nonlinear models. This chapter keeps the conceptual bridge: a linear equation is also a one-layer neural network. Students fit it first with compact PyTorch components and inspect residuals before adding complexity.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Add a second feature and explain why the visualization becomes harder even though `nn.Linear` changes very little.
Core ideas
A linear regressor combines each feature with a learned weight and adds a bias. The output is continuous, so the model represents a plane or hyperplane rather than a class boundary.
A residual is observed minus predicted. Squaring makes large errors matter more and removes cancellation between positive and negative residuals.
A batch estimates the average gradient from several observations. Minibatches balance noisy single-example updates against expensive full-dataset updates.
Mathematical intuition
The model maps features to a prediction; MSE maps all residuals to one optimization signal. Neither equation alone proves the relationship will generalize.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
x = [0., 1., 2.]
w, b = 1.5, 0.5
print([w * value + b for value in x])Predict the result, then click Run.
y = [1., 2., 4.]
pred = [0.5, 2., 3.5]
mse = sum((a-b)**2 for a,b in zip(y,pred)) / len(y)
print(round(mse, 3))Predict the result, then click Run.
features, outputs = 4, 1
weights = features * outputs
biases = outputs
print(weights + biases)Predict the result, then click Run.
Failure checks
What goes wrong: One feature dominates gradient updates because its numeric range is much larger.
Check: Compare feature ranges and standardize from training data.
What goes wrong: A feature contains information created after the outcome.
Check: Write the prediction time and inspect feature availability.
What goes wrong: Low MSE hides systematic failure for part of the range.
Check: Plot residuals against predictions and important slices.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. Students fit it first with compact PyTorch components and inspect residuals before adding complexity.
Standard answer: A linear regressor combines each feature with a learned weight and adds a bias. The output is continuous, so the model represents a plane or hyperplane rather than a class boundary.
Standard answer: A residual is observed minus predicted. Squaring makes large errors matter more and removes cancellation between positive and negative residuals.
Standard answer: A batch estimates the average gradient from several observations. Minibatches balance noisy single-example updates against expensive full-dataset updates.
Standard answer: The model maps features to a prediction; MSE maps all residuals to one optimization signal. Neither equation alone proves the relationship will generalize. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Plot or tabulate the target against each important feature.
Standard answer: Compare feature ranges and standardize from training data.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 4 keeps the regression task but focuses on the reusable training loop, validation, and the point at which added capacity stops helping.
Terminology
Go further
Next: Week 4 keeps the regression task but focuses on the reusable training loop, validation, and the point at which added capacity stops helping.