Linear Regression Explained: How Predictions Are Drawn

Linear regression explained in plain terms means understanding the simplest way a computer can learn from data: draw the straight line that best fits what has happened, and use it to predict what will happen next. The central question is how a pile of past numbers can say anything about the future, and the answer is that when two things move together, a line through their history is often the best guide to tomorrow.

Linear regression is a supervised machine learning method that models the relationship between an input and an output as a straight line. Given examples, such as the size of houses and their prices, it finds the line that comes closest to all of them, and then uses that line to predict the price of a house it has never seen.

The idea is simple: fit the line, then follow it. The model has two numbers, the slope, which says how much the output changes for each unit of input, and the intercept, which says where the line crosses the axis, and those two numbers are all the model needs to make a prediction. This article explains what linear regression is, how it works, and where it is used.

Key takeaways

  • Linear regression fits a straight line to past data to predict the future.
  • It is supervised learning: it learns from examples with known answers.
  • The line is chosen by least squares, minimising the squared errors.
  • It is fast, transparent, and easy to explain to anyone.
  • It suits problems where the relationship is roughly a straight line.

What linear regression is

Linear regression assumes the output can be predicted from the input by a straight-line formula: the prediction equals a slope times the input, plus an intercept. With one input, that is a line on a graph; with several inputs, it is a flat surface in a higher-dimensional space, and the model learns one slope for each input, showing how much each one matters.

The model is trained by least squares. For each example, the error is the difference between the real answer and the line’s prediction, and the training finds the slope and intercept that make the total of the squared errors as small as possible. Squaring stops positive and negative errors from cancelling, and it punishes large errors more than small ones.

The quality of the fit is often measured by R-squared, a number between zero and one that says how much of the variation in the output the line explains. An R-squared of 0.8 means the line explains eighty per cent of the variation, and the rest is noise the model cannot capture. Our guide to data analytics explains the wider field that turns data into decisions, and our guide to predictive analytics explains how models like this one forecast what happens next.

How linear regression works

Training starts with data: a table of examples, each with inputs and a known output. The data is usually split into a training set, which the model learns from, and a test set, which is held back to check how well it predicts on examples it has never seen, so that the result is honest and not just memorised.

With one input, the best line has a direct formula, called the normal equation, which solves for the slope and intercept in one step. With many inputs, the same formula works in matrix form, though for very large datasets it is more efficient to use gradient descent, which starts with a guess and improves it step by step, following the slope of the error downhill.

Multiple regression extends the idea to several inputs at once. A house price might depend on size, bedrooms, and distance from the centre, and the model learns a slope for each, so that a prediction combines all of them. The slopes can be read as explanations: holding the other inputs fixed, each extra bedroom adds a certain amount to the price.

Assumptions matter. Linear regression works best when the relationship really is close to a straight line, when the errors are independent of each other and spread evenly, and when the inputs are not too closely related to each other. When those assumptions fail, the predictions can be misleading, and a different model, or a transformed input, is needed.

Regularisation keeps the model honest. Ridge regression adds a penalty for large slopes, and lasso can shrink some slopes to exactly zero, effectively dropping inputs that do not help. Both protect against overfitting, where a model with too many inputs fits the training data perfectly but predicts new data badly.

A laptop on a dark desk with its screen showing a glowing green scatter of data points with a straight line drawn through them
A laptop showing a glowing green scatter of data points with a straight line drawn through them shows how linear regression works: the line that best fits the past.

Comparing linear regression with other models

Linear regression is one of several predictive models, and each suits a different problem. Decision trees split data into branches, logistic regression predicts categories, and neural networks learn complex curves, but each has its own trade-offs.

The table below sets out the main contrasts between the leading approaches.

Aspect Linear regression Decision trees Neural networks
Predicts Numbers, on a line Numbers or categories Numbers or categories
Interpretability Very clear, slopes explain Clear, rules can be read Poor, a black box
Speed Very fast to train Fast Slow, needs much data
Handles curves No, straight lines only Yes, by splitting Yes, very flexible
Best for Simple, explainable models Rules and categories Complex, large datasets

The practical result is that linear regression is the right choice when the relationship is simple and the model must be explainable, decision trees win where rules and categories matter, and neural networks are the choice for complex patterns in very large datasets.

Where linear regression is used

House prices are the classic example. Estate agents and lenders model price from size, location, bedrooms, and age, and the model gives a quick estimate for any house, as well as showing which factors matter most to the price.

Sales forecasting uses linear regression to predict demand. A retailer relates sales to price, advertising, season, and promotions, and the model forecasts next month’s sales, helping the business order the right amount of stock.

Finance uses it to measure risk and return. The capital asset pricing model relates a share’s return to the market’s return with a single slope, called beta, which tells investors how much a share moves when the market moves.

Medicine uses linear regression to study risk factors. Researchers relate blood pressure to age, weight, and exercise, and the slopes show how much each factor contributes, which guides advice and treatment.

Science and engineering use it everywhere a straight-line relationship is suspected. A laboratory calibrates an instrument by fitting a line to known measurements, an engineer relates fuel use to speed, and an economist relates spending to income, all with the same simple tool.

Strengths and limits

The greatest strength of linear regression is its transparency. The model is two numbers, or a few more, and anyone can read them: the slope says how much the output changes per unit of input, and the intercept says where the line starts. That makes it easy to explain, to trust, and to defend in front of people who are not specialists.

It is also a strength that linear regression is fast and reliable. It trains in seconds, needs little data, and its mathematics is well understood, which is why it is often the first model tried, and the baseline that fancier models must beat to be worth their complexity.

The limits are real, though. The world is not always a straight line, and a linear model will miss curves, thresholds, and interactions unless the inputs are transformed first. Outliers can also pull the line away from the bulk of the data, so careful checking of the data matters as much as the model itself.

Linear regression checklist

  • Plot the data first, and check the relationship looks roughly straight.
  • Split data into training and test sets before you start.
  • Scale inputs where needed, so no single input dominates.
  • Check the residuals for patterns, which mean the model is missing something.
  • Look for outliers, and decide whether they are errors or real.
  • Use regularisation when you have many inputs.
  • Report R-squared, but also check predictions on the test set.
  • Do not extrapolate far beyond the data you trained on.
  • Explain the slopes in plain language, so the model can be trusted.

Frequently asked questions

What is linear regression in simple terms?

Linear regression is a way of predicting a number from other numbers by drawing the straight line that best fits past examples. Once the line is found, a new input gives a prediction by reading off the line.

What is the difference between linear regression and logistic regression?

Linear regression predicts a number, such as a price, while logistic regression predicts a category, such as yes or no. They share the idea of fitting a model to data, but their outputs and their mathematics differ.

What is R-squared?

R-squared is a number between zero and one that says how much of the variation in the output the model explains. A value near one means the line fits well; a value near zero means it explains almost nothing.

What is least squares?

It is the method used to fit the line. For each example, the error is the gap between the real answer and the prediction, and least squares chooses the line that makes the total of the squared errors as small as possible.

When should I not use linear regression?

When the relationship is curved, when the output is a category rather than a number, or when the data has strong interactions between inputs. In those cases, trees, logistic regression, or a neural network will do better.

Is linear regression still used in machine learning?

Yes, widely. It is the baseline every new model is compared against, it is used wherever explanations matter, and it is often the right model when the data is small and the relationship is simple.

The bottom line

Linear regression explained in one line is this: the straight line that best fits the past, used to predict the future, with a slope and an intercept that anyone can read.

It is the oldest and simplest tool in machine learning, and it endures because it is fast, transparent, and often good enough. Every data scientist starts here, and every complex model is judged against the line.

This article is general information about how linear regression works, not advice about building models for a particular problem.

Latest articles

Scroll to Top