Zum Inhalt springen

English:Regression, numerical prediction and uncertainty - AI Year 8

Aus MOOCsWiki Staging
aiMOOC-Siegel aiMOOC

Regression, numerical prediction and uncertainty - AI Year 8

QR-Code



Introduction

A Cartesian coordinate plane
Choose labelled axes and sensible scales before plotting numerical pairs. This generic coordinate plane does not contain our regression data.

Fit a numerical line, calculate three predictions and compare unused cases with a simple baseline. Use residuals and an out-of-range stress case to distinguish a useful calculation from certainty or a causal claim.


Numerical targets and a fitted line


Decide what kind of target you need

Regression predicts a numerical quantity. Classification predicts a category, even when that category is recorded using a number. The target's meaning matters more than the appearance of its code. Keep inputs, targets, units and the prediction time clear before choosing a method.

Fictional task Target meaning Task type
Estimate assembly time for a card batch A duration in minutes Regression
Estimate a display fixture length A length in centimetres Regression
Estimate energy used by a fictional device A quantity in watt-hours Regression
Choose a folder for a fictional message A category such as club or science Classification
Choose an instrument family A named family category Classification
Predict a yes or no permission flag Two categories, even if coded zero and one Classification

These examples define types of prediction; they do not authorise collecting private messages or making real permission decisions. A regression value is an estimate, not a guaranteed measurement or a licence to act on a person.

StatQuest with Josh Starmer: Linear Regression, Clearly Explained!!! Optional explanation of fitting and residuals. Our original card-batch numbers below are not data from that recording. You can complete all core calculations from the text and tables without running an external model or providing personal data.


Define an original numerical example

Imagine assembly of fictional display-card batches. Input x is the number of cards and target y is the recorded duration in minutes in an entirely invented dataset. The zero-card value represents a fictional setup duration. None of these times was measured from a real person or device.

Training input: cards Fictional reference minutes
0 3
2 7
4 11
6 15

These four training points lie on the line with slope two and intercept three. The actual exact-arithmetic program fits that line from these four rows only; the later test rows do not choose its parameters.

ŷ=2x+3 The hat marks a predicted value. At x equal to one, three and four, the predictions are five, nine and eleven minutes. In particular, x equal to four gives eleven, as in the source worked example. The fitted line's agreement with its training rows is not proof that every later batch follows it.


Read and draw a scatter plot

Use a horizontal axis for card count and a vertical axis for minutes. Choose clear scales large enough for zero to six cards and zero to sixteen minutes, then plot the four training pairs as separate points. Draw the fitted line through them. A scatter plot displays pairs; connecting every observation in order is not necessary for a regression fit.

The coordinate-plane image is a generic guide, not a plot of our data. On a separate grid, add the two held-out points introduced next using a different marker as well as a colour. Inspect whether each lies above or below the line. This combines a visual check with the accessible numerical table rather than relying on a single summary score.



Held-out predictions, residuals and a baseline


Freeze the line before looking at test outcomes

The two original held-out cases are x one with reference y six, and x three with reference y eight. They were not used to fit the line. Both inputs lie inside the training input range zero to six, so these are interpolation cases. Keep their reference values separate from the prediction until you compare them.

A simple baseline predicts the training mean target for every input. The training mean is nine minutes. This baseline uses training information only, not the average of the later test outcomes.

Test input Fictional reference Line prediction Residual: reference minus prediction Mean baseline prediction
1 6 5 1 9
3 8 9 -1 9

At x one, the line predicts five against reference six: residual plus one, so the prediction is too small. At x three, it predicts nine against reference eight: residual minus one, so the prediction is too large. The signed residuals cancel when averaged, but neither prediction is exact.

e=y−ŷ

Compare absolute and squared error honestly

Mean absolute error, MAE, averages the sizes of residuals. Mean squared error, MSE, averages their squares and gives greater numerical weight to larger errors. They summarise different aspects and have different units: MAE here is minutes and MSE is squared minutes.

MAE⁡=1n∑i=1n|ei|

MSE⁡=1n∑i=1nei2

Method Two held-out residuals MAE MSE
Fitted line +1 and -1 1 1
Training-mean baseline -3 and -1 2 5

The fitted line has lower error on these two invented test cases. That limited comparison does not prove it is best for all future data, that errors will stay within one minute, or that a real service benefits. State the sample, metric, units and intended decision instead of calling it a universal accuracy percentage.


Inspect the complete fitting and evaluation code

This original standard-library Python was actually executed. It uses exact Fraction arithmetic, fits a one-feature least-squares line and evaluates the retained line and mean baseline separately. It rejects inexact or inappropriate numeric inputs and cannot fit a slope when all training inputs are identical. The fitting arithmetic is included for inspection; you can reason from the line and tables without deriving the fitting formula.

"""Original exact one-feature least-squares example using fictional data only."""
from fractions import Fraction as F
import json


def number(value):
    if type(value) not in [int, F]:
        raise ValueError("Use exact integers or fractions")
    return F(value)


def pairs(rows):
    if type(rows) not in [list, tuple] or not rows:
        raise ValueError("Supply nonempty numeric pairs")
    clean = []
    for row in rows:
        if type(row) not in [list, tuple] or len(row) != 2:
            raise ValueError("Each row requires an input and a target")
        clean.append((number(row[0]), number(row[1])))
    return clean


def fit_line(training):
    clean = pairs(training)
    if len(clean) < 2:
        raise ValueError("Need at least two training rows")
    mx = sum(x for x, y in clean) / len(clean)
    my = sum(y for x, y in clean) / len(clean)
    denominator = sum(pow(x - mx, 2) for x, y in clean)
    if denominator == 0:
        raise ValueError("Distinct training input values are required")
    slope = sum((x - mx) * (y - my) for x, y in clean) / denominator
    return dict(slope=slope, intercept=my - slope * mx, baseline_mean=my,
                input_min=min(x for x, y in clean), input_max=max(x for x, y in clean))


def prediction(model, x):
    return number(model["slope"]) * number(x) + number(model["intercept"])


def evaluate(model, rows, baseline=False):
    clean = pairs(rows)
    if type(baseline) is not bool:
        raise ValueError("Select the declared line or mean baseline")
    results = []
    for x, y in clean:
        estimate = number(model["baseline_mean"]) if baseline else prediction(model, x)
        results.append(dict(x=x, reference=y, estimate=estimate, residual=y - estimate))
    return dict(rows=results,
                mae=sum(abs(r["residual"]) for r in results) / len(results),
                mse=sum(pow(r["residual"], 2) for r in results) / len(results))


def demonstrate():
    training = [(0, 3), (2, 7), (4, 11), (6, 15)]
    test = [(1, 6), (3, 8)]
    model = fit_line(training)
    return dict(training=training, model=model,
                three_predictions=[dict(x=x, estimate=prediction(model, x)) for x in [1, 3, 4]],
                training_check=evaluate(model, training),
                held_out=evaluate(model, test), mean_baseline=evaluate(model, test, True),
                separate_stress=evaluate(model, [(10, 40)]),
                measured_real_minutes=False, causal_effect_established=False,
                prediction_interval_estimated=False)


if __name__ == "__main__":
    print(json.dumps(demonstrate(), indent=2, default=str))

Training residuals are all zero in this deliberately simple dataset. Held-out residuals are plus and minus one, and the separate stress case below is much larger. Keep expected and actual outputs when running an approved Python environment. A paper trace must be described as a paper trace, not as an executed or deployed model.



Extrapolation, uncertainty and transfer


Challenge extrapolation with a separate stress case

Now consider x ten, outside the training input range zero to six. The retained line predicts twenty-three. For a separate original fictional stress case, the reference is forty, giving residual plus seventeen and squared error two hundred eighty-nine. This case was not used to fit the original model or the mean baseline.

The stress case shows that the line can fail badly outside its observed input range. It does not prove that every extrapolation is wrong or explain the real cause of a failure. Retain the stress case and its limitation; do not secretly refit using it and continue to call it untouched evaluation. A revised version needs appropriately separated fresh evaluation.


Discuss uncertainty without inventing a confidence interval

Predictions depend on the chosen representation, dataset, method and context. Four perfectly aligned training rows and two small held-out residuals cannot establish the full spread of future outcomes. The observed residuals plus and minus one are not an estimated prediction interval or a promise about the next case. No probability distribution or confidence interval has been fitted here.

Inspect a scatter plot and residuals as well as metrics. Curved patterns, unusual points or changing spread can suggest that a straight-line description needs further investigation. A small residual plot alone is not a proof that every modelling assumption holds. For an additional verified illustration, inspect Justinkunimune's separate data and residual plots. Its points and fitted line are not our card dataset; use it to distinguish a data plot from a plot of differences around the fitted values.

CodeAI: AI, Training Data & Bias. Optional context about data choices. Our tiny invented dataset does not represent a real population or provide a fairness measurement. The film is not evidence that the fitted card line generalises.


Separate association, prediction and causation

A fitted line describes a relationship in selected data under a method. It does not show that changing x causes the predicted change in y. Other factors, selection effects or a common cause can produce associations. In our case, the numbers were authored for an exercise, so no real causal effect was measured at all.

Before interpreting a real prediction, ask what the variables mean, how the data were obtained, whether the range and circumstances match, and what the consequence of an error would be. A zero training error cannot answer those questions. Do not use this toy line for medical, financial, safety-critical or personal decisions.


Transfer to another fictional quantity

Imagine predicting material length for original model-making kits rather than assembly minutes. Define input and target units, provenance and availability again. Decide whether a line and an absolute-error measure fit the purpose. If underestimation is much more costly than overestimation, a symmetric error average alone may hide the important consequence.

Fit only on the new task's training data, compare with a suitable baseline, retain independent test cases and challenge out-of-range inputs. Do not reuse slope two and intercept three merely because the calculation is convenient. Your evidence should state three predictions, residual signs, metric denominators, the extrapolation stress case and what remains unproved about accuracy, uncertainty and causation.



Interactive Tasks


Quiz: Test Your Knowledge

Which target is a regression quantity? (Assembly duration in minutes) (!An instrument-family category) (!A yes or no category stored as zero or one) (!A folder name)



What prediction does the fitted line give when x is four? (Eleven) (!Eight) (!Four) (!A guaranteed observed measurement)



What are the three stated predictions for x one three and four? (Five nine and eleven) (!Three seven and fifteen) (!Six eight and forty) (!One three and four)



What is the residual when reference six is compared with prediction five? (Plus one) (!Minus one) (!Zero because the other residual cancels) (!Six squared)



What does a residual of minus one mean here? (The prediction is one minute above the reference) (!The reference is one minute above the prediction) (!The prediction is exact) (!A causal effect has been proved)



What is the line MAE on the two held-out cases? (One minute) (!Zero because signed residuals cancel) (!Five squared minutes) (!A population-wide accuracy guarantee)



What is the training-mean baseline value? (Nine minutes) (!Seven minutes from the later test average) (!The true answer for every query) (!Twenty-three minutes)



Why is x ten an extrapolation? (It lies outside the training input range) (!It was one of the four training inputs) (!Every test input is outside the training range) (!The line has no intercept)



What does the separate x ten stress case show? (A residual of plus seventeen under the retained line) (!All future residuals must be one or less) (!The model has automatically retrained) (!Every possible extrapolation is wrong)



What has not been established by the exercise? (A real causal effect or an estimated prediction interval) (!The exact three predictions) (!The two held-out residuals) (!The training-mean baseline value)





Memory Game

Regression Predicting a numerical quantity
Classification Predicting a category
Residual Reference value minus predicted value
Baseline A simpler comparison method
Interpolation Prediction within the observed input range
Extrapolation Prediction beyond the observed input range





Drag and Drop

Keep the four supplied fitting rows separate Training evidence
Compare two unused cases without changing the line Held-out evaluation
Measure how large residuals are without sign cancellation Absolute-error comparison
Challenge the line at a value beyond its training range Extrapolation check
Avoid treating a fitted association as an intervention result Causal caution





Crossword Puzzle

TARGET What outcome is the model intended to predict?
SLOPE What coefficient gives the change along a fitted straight line?
INTERCEPT What term gives the fitted value when the input is zero?
RESIDUAL What difference subtracts prediction from reference?
BASELINE What simple comparison is used before claiming usefulness?
RANGE What observed input span matters for extrapolation?





LearningApps

Optional extra practice. Choose an appropriate activity with your teacher. If the external collection is unavailable, use the native activities and independent tasks in this course.


Cloze Text

Complete the explanation.
Predicting a numerical quantity is

. Predicting a category is

. The line adds an intercept of

. At input four it predicts

. The reference minus prediction is a

. Positive one means the prediction is too

. Signed errors can

. Absolute error measures their

. A simpler comparison is a

. Outside-range prediction is

. A fitted association does not prove

. The course estimates no prediction

.



Open Tasks


Easy

  1. Sort six target types: Explain why the three numerical quantities are regression targets and the three categories are classification targets, including the numeric flag.
  2. Calculate three predictions: Use the stated line for inputs one three and four, showing multiplication and the intercept separately.
  3. Draw the training scatter: Choose units and scales, plot the four original pairs and draw the fitted line without confusing it with guaranteed outcomes.
  4. Interpret two residual signs: Add the held-out points and explain which is above or below the line and why the signed errors cancel.


Standard

  1. Compare with the mean baseline: Calculate the training mean and both held-out error metrics with their denominators and units.
  2. Challenge perfect training fit: Contrast zero training residuals with the unused cases and explain what remains unknown about generalisation.
  3. Test outside the range: Use the separate x ten case to calculate prediction, residual and squared error without refitting the retained model.
  4. Inspect a residual illustration: Use the linked independent Commons illustration or your own plots to distinguish values from differences;do not attribute its data to our example.


Advanced

  1. Review an uncertainty claim: Explain why two residuals do not justify a guaranteed one-minute bound or a fitted prediction interval.
  2. Challenge a causal claim: Describe what the fictional association does and does not show and identify information a real causal question would require.
  3. Transfer the prediction task: Define a fictional model-kit material target, new units, baseline, error consequences and appropriate independent evaluation.
  4. Document a revised experiment: Preserve original training/test/stress evidence, then plan a revised version and genuinely separate new tests rather than hiding a used test.



Learning Check

  1. Compute and qualify three predictions: What does the retained line predict for one three and four cards?
  2. Interpret residuals without cancellation: What do the two held-out residuals show?
  3. Compare metrics and baseline: What are the line and mean-baseline MAE and MSE?
  4. Preserve the stress test: Why does the x ten case matter?
  5. Separate uncertainty and causation: What cannot be inferred from the neat fitted line?
  6. Transfer with an appropriate cost: What changes for model-kit material estimates?


Worked checks and assessment guidance

  1. Multiplying each input by two and adding three gives five, nine and eleven minutes. These are fitted predictions, not guaranteed measurements. The training input range is zero to six and the target units are minutes; the original four rows are fictional.
  2. At x one, the reference six minus prediction five gives plus one, so the prediction is too small. At x three, the reference eight minus prediction nine gives minus one, so the prediction is too large. Their signed average is zero, but both absolute errors are one.
  3. The line has residuals plus one and minus one, so MAE 1 minute and MSE 1 squared minute. The training mean is nine; baseline residuals are minus three and minus one, so MAE 2 minutes and MSE 5 squared minutes. The lower line error applies to these two original held-out cases only, not an entire population.
  4. The input ten is outside the zero-to-six training range. The line predicts 23 against the separate fictional reference 40, giving residual 17 and squared error 289. This exposes a possible extrapolation failure without showing every extrapolation must fail or proving a real causal explanation. Reusing this case for revision would require fresh independent tests.
  5. A perfect training fit and two small test residuals do not establish an interval covering future outcomes, all modelling assumptions or a causal effect. The values were authored; no real time or intervention was measured. Inspect range, context, scatter and residuals and state missing evidence rather than turning a summary into certainty.
  6. Define new inputs, target units, provenance, training data and prediction-time availability. Compare a suitable baseline and revisit whether a straight line and symmetric average error suit the task. Underestimation may have a different consequence from overestimation. Do not copy old parameters or claim practical mastery from a paper plan.


Learning evidence

Retain six task-type decisions, clearly labelled original scatter and line, three predictions, the frozen training/test split, two residuals, line/baseline MAE and MSE with units, the separate extrapolation stress case and a justified new-context plan. State the unestimated uncertainty and unproved causal claims. Preserve actual code and outputs when executed; distinguish paper reasoning from practical performance or complete curriculum coverage.


OER and Wikipedia

Google: Linear regression provides numerical-target and line-parameter context. NIST: How can I tell if a model fits my data? explains residual analysis and limits of a single fit summary. Relevant primary sections inspected 15 November 2026. This course does not perform the advanced NIST lack-of-fit test or estimate a prediction interval. DfE Computing, Key Stage 3 supplies the abstraction context; this Year 8 AI route is editorial enrichment.

2D Cartesian Coordinates.svg, H Padleckas, public domain as recorded on the Commons file page. Original coordinate-plane illustration unchanged; it is not a plot of the original card data. Optional external illustration: Linear regression residuals.svg, Justinkunimune,CC0. Actual diagram and source verified; its separate dataset is not our fitted example.

https://www.youtube.com/watch?v=nk2CQITm_eo — StatQuest with Josh Starmer: Linear Regression, Clearly Explained!!! https://www.youtube.com/watch?v=x2mRoFNm22g — CodeAI: AI, Training Data & Bias. Actual creator, playability, embedding and preview bytes verified. Original recordings embedded unchanged, with no transcript copy, endorsement, redistribution or relicensing implied. All core examples and tables are supplied independently.

All card counts, times, reference outcomes and stress values are original fictional teaching data. The complete standard-library Fraction program was actually executed; no real times, causal effects, prediction intervals or pupil competence were measured. No local generative LLM, paid API, private learner data or operational decision system was used.



Linked learning areas


aiMOOC Projects



Schulfach+




aiMOOCs



aiMOOC Projekte









MOOCwiki · Deutsch

Nach dem Lernen ist vor dem Lernen

Entdecke direkt den nächsten Lernkurs. Weitere Inhalte erscheinen, wenn Du weiter nach unten scrollst.

Zur MOOCwiki-Hauptseite

Mediathek

Suche verfeinern

Dein Lernplan bestimmt, was oben steht. Alle anderen Inhalte bleiben erreichbar. Seitenaufrufe beeinflussen die Auswahl nicht.

Inhalte werden geladen ...

Mediathek wird aus dem Wiki geladen ...