Skip to main content

Playground · Linear Regression

Linear regression, least squares

The simplest model that learns: fit a straight line through a cloud of points. Move the data around and watch the ordinary least squares best-fit line, its residuals, and the R² score recompute instantly — no libraries, just the closed-form maths.

Click empty space to add a point · drag a point to move it. The line and R² update on every change.

Best-fit line
slope m
intercept b

The fit is the exact closed-form solution: m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)² and b = ȳ − m·x̄ — no iteration, no learning rate.

How least squares works

What it minimises

For each point the residual is the vertical gap between the actual yᵢ and the line's prediction m·xᵢ + b. Least squares picks the m and b that make the sum of squared residuals (SSres) as small as possible. Squaring punishes big misses harder and gives one unique closed-form answer.

Slope & intercept

The slope m is how much y changes for a one-unit rise in x — the relationship's direction and strength. The intercept b is where the line crosses x = 0. Together they are the two parameters this model "learns" from the data.

What R² tells you

R² = 1 − SSres/SStot is the share of the variance in y the line explains. 1.0 is a perfect fit; 0 means the line does no better than just predicting the mean ȳ. It can even go negative for a truly bad fit.

Caveat: this is one input, one output, and illustrative data you place by hand. Real regressions have many features, need train/test splits, and check assumptions (linearity, independence, constant variance). Least squares is also the special case that gradient descent converges to — see gradient descent and the Learn shelf.