Supervised LearningLinear RegressionRegression

Ordinary Least Squares (Linear Regression)

Primary task · Regression

Ordinary Least Squares (OLS) is the canonical linear regression model. It estimates coefficients that minimize squared prediction error and provides an interpretable baseline for continuous targets.

← Directory
Visual intuition

From data to learned behaviour

Ordinary Least Squares (Linear Regression) converts patterns in observed data into a reusable prediction or representation rule. The most useful way to understand it is to watch what internal structure changes during training and how that learned structure changes outputs.

Infographic
1Data2Initial state3Optimise4Validate5InferenceTraining transforms evidence into a reusable model state
Conceptual simulation

Watch the learning mechanism form

The structure below is synchronized with the same training state used by the prediction simulation.

Mechanism view
Training control centre

Control both simulations together

Reset regenerates the synthetic data and model state. Train animates to completion. Pause freezes the animation. Train Step advances one learning stage.

Step 0 / 20
Model simulation

Inspect the learned prediction / representation

Synthetic data are generated locally in your browser.

Model description

Understand Ordinary Least Squares (Linear Regression) after watching it learn

This section connects the animation to the actual statistical or computational idea behind the model.

Deep description

Ordinary Least Squares (Linear Regression) Ordinary Least Squares (OLS) is the canonical linear regression model. It estimates coefficients that minimize squared prediction error and provides an interpretable baseline for continuous targets.

What is learned. During training, the algorithm builds or adjusts the parameters and internal representation used by Ordinary Least Squares (Linear Regression). The core learning mechanism is: Fits a linear function by minimizing the sum of squared residuals between observed and predicted continuous targets.

How training becomes inference. Prepare data → initialise the model state → evaluate the current objective → update parameters or structure → validate progress → use the final state for inference. Once training stops, the fitted state is reused on unseen inputs rather than being reconstructed from scratch. The resulting output is: A continuous numeric prediction; some probabilistic variants can also provide uncertainty or intervals.

Why practitioners use it. Fast, transparent coefficients, closed-form solution when assumptions and matrix conditioning permit. Typical fits include Interpretable baseline regression, trend estimation, econometrics, calibration, effect-size analysis.

What to verify before trusting it. Sensitive to outliers and multicollinearity; only linear in the chosen features unless features are transformed. The visual simulation is intentionally simplified, so real use should still validate preprocessing, data independence, hyperparameters, uncertainty and task-appropriate metrics.

Internal statethe parameters and internal representation used by Ordinary Least Squares (Linear Regression)
Typical outputA continuous numeric prediction; some probabilistic variants can also provide uncertainty or intervals.
Good fitInterpretable baseline regression, trend estimation, econometrics, calibration, effect-size analysis.
Main cautionSensitive to outliers and multicollinearity; only linear in the chosen features unless features are transformed.
1Training data→
2Learning objective→
3Internal model state→
4Prediction / representation→
5Evaluation
Intuition

What the model is trying to learn

Ordinary Least Squares (Linear Regression) converts patterns in observed data into a reusable prediction or representation rule. The most useful way to understand it is to watch what internal structure changes during training and how that learned structure changes outputs.

Mathematical lens

Core logic

Fits a linear function by minimizing the sum of squared residuals between observed and predicted continuous targets. The mathematical objective determines which model states are considered better, while regularisation and validation constrain how much complexity should be trusted.

Training sequence

How learning progresses

Prepare data → initialise the model state → evaluate the current objective → update parameters or structure → validate progress → use the final state for inference.

Original mechanism

Taxonomy description

Fits a linear function by minimizing the sum of squared residuals between observed and predicted continuous targets.

Evaluation guide

How to evaluate this model responsibly

ValidationK-Fold; Group K-Fold for repeated entities; time-aware splits for temporal targets.
MetricsMAE and RMSE together, plus R²; inspect residuals rather than trusting one aggregate score.
HPORandom/Bayesian optimisation for continuous hyperparameters; use nested CV when model selection is intensive.
Post-processingInverse target transforms, clipping only with domain justification, and prediction intervals where uncertainty matters.
Hyperparameters

Key parameters

fit_interceptTypical: True

Whether to estimate an intercept.

positiveTypical: False

Optionally constrain coefficients to be non-negative.

Use & trade-offs

Where it fits

Typical applications

Interpretable baseline regression, trend estimation, econometrics, calibration, effect-size analysis.

Strengths

Fast, transparent coefficients, closed-form solution when assumptions and matrix conditioning permit.

Limitations

Sensitive to outliers and multicollinearity; only linear in the chosen features unless features are transformed.

Code example

Minimal Python implementation

# Purpose: demonstrate Ordinary Least Squares (Linear Regression) with a small, inspectable example.
# Follow the comments and printed stages to connect each operation with its result.
# Import the library or helper used in this example.
import numpy as np
# Import the library or helper used in this example.
from sklearn.linear_model import LinearRegression

# Print this intermediate result so you can verify the workflow step by step.
print("STEP 1 · Prepare the miniature example")
# Create the numerical values used in the calculation.
X = np.array([[0.],[1.],[2.],[3.],[4.]])
y = np.array([1.0, 2.8, 5.2, 6.9, 9.1])
# Print this intermediate result so you can verify the workflow step by step.
print("STEP 2 · Fit / train the model")
# Configure the estimator or pipeline with the chosen settings.
model = LinearRegression().fit(X, y)
# Print this intermediate result so you can verify the workflow step by step.
print("STEP 3 · Inspect predictions / metrics")
# Print this intermediate result so you can verify the workflow step by step.
print(round(model.coef_[0], 2), round(model.intercept_, 2))
# Generate predictions from the fitted model.
print(np.round(model.predict([[1.5], [3.5]]), 2))
Expected / representative output
STEP 1 · Prepare the miniature example
STEP 2 · Fit / train the model
STEP 3 · Inspect predictions / metrics
2.02 0.96
[3.99 8.03]