Learn

Learning preferences

Make learning more comfortable. Changes apply immediately.

Enlarges page content, including diagrams and their labels. You can also use your browser’s zoom.

Choose the colours that are easiest for you to distinguish. Applies to lesson diagrams and lab plots. Labels, shapes, hatching and line patterns also distinguish meanings. Source photographs and raw image brightness stay unchanged; mask-only views are available for image overlays.

ExampleComparisonSample link

Saved in this browser on this device. Preferences are separate from progress backups.

Lesson 2 of 10 in this unit

Understand Neural Networks · ORIGINAL LEARN AI LESSON

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 the exampleJump to the experimentRead the explanation
Prerequisites and path options

Why learn this? Estimate a numerical outcome and inspect its errors before assuming a straight-line relationship is adequate.

01 · TRY

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
02 · PREDICT
Before you experiment

A line is fitted using shuffled training targets. Which labels should remain unchanged when comparing its validation error?

A line is fitted using shuffled training targets. Which labels should remain unchanged when comparing its validation error?
03 · EXPERIMENT

Change one thing. Watch what follows.

Loading this browser experiment…

04 · UNDERSTAND
READ & CONNECT

Understand what changed.

Back to the experiment

A shorter explanation, with the original worked examples available below. Both explain the same experiment and its limits.

Same learning goal in both modes: Interpret line coefficients and residuals, distinguish MSE from MAE, and explain why training fit and validation evidence have different roles.

The idea in everyday terms

Regression predicts a number. Here the input is a flower’s sepal length and the target is its petal length. A constant baseline predicts the training average; a line lets the prediction change with the input.

What to watch in the experiment

Inspect the vertical difference between each prediction and measurement. Fitting chooses the line from training data alone. Compare its errors with the constant baseline and try the training-target permutation to see what happens when the useful relationship is disrupted.

What this does not establish

A fitted line describes the supplied measurements; it does not establish causation. These reused Iris development examples do not validate deployment, and a constant input cannot identify a unique slope.

Open the full worked examples, calculations and limitations

This is the same reference used in Technical mode. Expand it whenever you need the exact numbers, assumptions or further explanation.

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.

y^=a+bx\hat y=a+bx

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

ri=yi−y^i,MSE=1n∑i=1nri2r_i=y_i-\hat y_i,\qquad \mathrm{MSE}=\frac{1}{n}\sum_{i=1}^{n}r_i^2

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 b=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2,a=yˉ−bxˉb=\frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sum_i (x_i-\bar x)^2},\qquad a=\bar y-b\bar x

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.

05 · APPLY
Check your understanding

A model has residuals −1 cm and +3 cm. What is its mean squared error?

A model has residuals −1 cm and +3 cm. What is its mean squared error?
06 · CONTINUE

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.

My Learning
Explore related topics and the topic map

How this topic connects

Explore this topic on the interactive map

Use these connections to review a foundation or choose where to go next. You can open any topic without completing the others.

What it builds on

  • Simple baselines

    Find out whether a complicated model adds value over a cheap, understandable reference.

  • Averages refresher

    Combine outcomes with their frequencies or probabilities, keeping the denominator explicit when interpreting this lesson’s evidence.

  • Functions and graphs refresher

    Read axes, coordinates and scales so you can connect a plotted change with the numerical evidence in this lesson.

You are hereFit 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.

What it enables

Show 1 more connections