Linear Regression Visualizer

Click to add points, then watch the model learn the best-fit line through gradient descent, step by step.

Advertisement

Click anywhere on the chart to add a data point, then step through training.

Slope (m)
Intercept (b)
MSE Loss
0Training Steps
Advertisement

What Linear Regression Is Actually Doing

Linear regression finds the straight line (defined by a slope m and intercept b) that best fits a set of data points, "best" specifically meaning the line that minimizes the average squared distance between each point and the line, this is called Mean Squared Error (MSE). Rather than solving for the optimal line algebraically in one step (which is possible for simple linear regression using the "normal equation"), this visualization uses gradient descent, the same iterative optimization approach used to train far more complex models, to show the line gradually improving step by step.

Understanding the Red Residual Lines

The faint red vertical lines connecting each point to the current regression line represent residuals, the error between what the model predicts and the actual observed value. Squaring these residuals (hence "squared error") and averaging them gives the loss the model is trying to minimize. Watch how the residual lines shrink as training progresses, that visual shrinking is literally the loss decreasing, the model getting better at predicting y from x.

Why Squared Error, Not Just Error

Using squared error rather than raw error serves two purposes. First, it prevents positive and negative errors from canceling out, a point 3 units above the line and a point 3 units below shouldn't average out to "zero error." Second, squaring penalizes large errors disproportionately more than small ones, a residual of 4 contributes 16 to the loss, while two residuals of 2 each contribute only 4 total, this makes the model prioritize fixing its worst predictions rather than treating all errors as equally important.

From This Simple Example to Real Applications

Real-world linear regression extends this exact same idea to multiple input features simultaneously (predicting house price from square footage, location, and age together, not just one variable), but the core mechanism, adjusting parameters to minimize squared prediction error via gradient descent, remains identical. Linear regression also forms the conceptual foundation for logistic regression (used for classification) and is often the first model data scientists try on a new problem, both for its simplicity and because it establishes a baseline that more complex models need to meaningfully beat to justify their added complexity.