Fit a line and explain its errors.
Move a line over licensed flower measurements, inspect residuals and compare a fitted model with a mean baseline and shuffled labels.
By the end: Interpret line coefficients and residuals, distinguish MSE from MAE, and explain why training fit and validation evidence have different roles.
Start with something concrete.
A flower has a sepal length of 6 cm. Can that measurement help estimate its petal length? Start with the average petal length, then move a line and inspect the distance from each prediction to the observed value.
Try the interactive model ↓A line is fitted using shuffled training targets. Which labels should remain unchanged when comparing its validation error?
Change one thing. Watch what follows.
- Use the training-mean button and inspect a validation measurement. Calculate its residual in cm and squared error in cm².
- Move the slope and intercept separately. Describe how the line and the selected residual change on fixed axes.
- Use training-fitted coefficients and compare validation MSE and MAE against the training-mean baseline. Explain what the slope says and what it does not prove.
- Change the training-label shuffle from 1 through 3. Compare controls on the unchanged validation targets, then download a note explaining the evidence and limits.
Loading this browser experiment…
Start with a prediction in centimetres
We observe sepal and petal lengths in the licensed Iris dataset. Suppose you know only a flower’s sepal length and want to estimate its petal length. A regression model predicts a number. It differs from predicting a species category, even though the same source can support both learning exercises.
The training-mean baseline always predicts the average training petal length. A line lets the prediction change with the input. Move the line in the lab, select a validation flower and follow the vertical segment from prediction to observation.
Two coefficients describe the line
For a small worked example, suppose the intercept is 1 cm and the slope is 0.5 cm of petal length per cm of sepal length. At sepal length 6 cm, the prediction is 1 + 0.5 × 6 = 4 cm.
x is sepal length in cm; ŷ (“y hat”) is predicted petal length in cm. a is the intercept, in cm. b is the slope, in cm of predicted petal length per cm of sepal length. Read the formula as “prediction equals the starting value plus slope times input.” Changing a shifts the line vertically. Changing b changes how quickly its prediction rises or falls as x changes.
An intercept at x = 0 is a mathematical parameter. Zero sepal length lies outside these flower observations, so that intercept does not have a supported biological interpretation. A slope in observational data describes an association, not the causal effect of changing an input. In this dataset the mixture of species can contribute to the relationship.
Show an error before averaging it
If the actual petal length is 5 cm and the prediction is 4 cm, the residual is 1 cm, using actual minus predicted. If actual is 3 cm and predicted is 5 cm, the residual is −2 cm. Squaring gives 1 cm² and 4 cm². Their mean squared error is (1 + 4) / 2 = 2.5 cm².
i identifies a row; yᵢ is its observed value; ŷᵢ is its prediction; rᵢ is its residual; and n is the number of evaluated rows. The sum symbol means add one squared residual for each row. MSE is undefined for an empty sample, not zero. Squaring prevents opposite signs from cancelling and makes larger errors contribute disproportionately.
Mean absolute error, MAE, averages absolute residuals instead. The same two residuals give (1 + 2) / 2 = 1.5 cm. MAE retains the original output unit; MSE has squared units. A numerical value of 1 in these two metrics does not mean the same thing. An acceptable error should be connected to the task and measurement precision.
Fit using training data only
Least squares chooses the slope and intercept that minimize training MSE. You can search manually, but a one-input line with an intercept also has a direct solution. First find the training mean x and mean y. Subtract those means from each training pair to describe deviations from the center.
Optional: the centered-sum formula
The bars in x̄ and ȳ mean training averages. The numerator adds products of paired deviations: it records whether larger x values tend to accompany larger y values. The denominator adds squared x deviations: it measures how much the training inputs vary. Divide to get the slope, then choose an intercept that makes the line pass through the two means.
If all training x values are identical, the denominator is zero and the slope is not identifiable from those observations. The lab’s fitting function reports that condition and returns a slope of zero with the mean prediction. A nonzero denominator identifies a least-squares slope, but does not establish causality or useful future performance.
The lab uses one representative per identical four-measurement group and fits on training groups only. Its deterministic stratified group assignment and small historical sample limit what can be claimed. These groups are a deduplication convention, not known specimen identities. The source measurements are unaltered in this regression view; the artificial missingness and mixed units belong to a different teaching lab.
Compare against a deliberately broken relationship
The label-shuffled control preserves the training inputs and the collection of training target values while rearranging which target belongs to which input. It fits a new line to that broken pairing. Validation labels remain original, so every candidate is judged against the same task. The displayed training metrics also use original targets for a common comparison; they are not the shuffled optimizer’s fitting objective.
Five reproducible permutations let you see that controls themselves vary. A single control outcome is not a statistical significance test. Do not select whichever shuffle makes your preferred conclusion look strongest. Report the setup and seek appropriate independent evidence.
Interpret the result within its scope
Compare the fitted line with the mean baseline, inspect one residual, and explain the units of both error measures. Then ask what the current evidence leaves out: other collections, different measurement practices, species proportions, outliers and possible nonlinear relationships.
The plot keeps fixed axes. A manual line can predict outside the visible range; its full prediction still appears in the arithmetic. A negative prediction is mathematically possible for a line but is not a physically meaningful petal length. Choosing a model or transformation to respect such constraints is part of model design.
This is repeated development evidence on a small licensed dataset, not final deployment validation. The downloadable comparison is practice. Independent implementation and decision quality require runnable evidence and human review; learner pilots remain pending.
A model has residuals −1 cm and +3 cm. What is its mean squared error?
Take this idea with you.
A fitted line summarizes an association. Fit it on training measurements, compare against a mean and a control, and report residuals with their units and limits.