Linear Regression
In one sentence: Linear regression predicts a number by drawing the straight line that sits closest to the data points, so you can read off a guess for any new input.
Read it out loud: “y hat equals w times x, plus b. The predicted y is the slope w times the input x, plus the intercept b. The hat over y means the value is a prediction, not a measurement.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Slope and intercept of a straight line
A straight line can be written as . The slope says how much rises each time goes up by one. A slope of 0.7 means a step of 1 in adds 0.7 to . The intercept is the height of the line where , the point where the line crosses the vertical axis. A slope of 0 is a flat line, and a negative slope is a line that goes downhill.
- A bar over a letter, like , means average
A bar placed over a letter, read ‘x bar’, is the average of that list. So is the average of all the values. To find it, add up the values and divide by how many there are. The average of 1, 2, 3 and 4 is . The average is the balance point of the list, the number that the values spread around.
- The sum sign over data points
The Greek capital sigma, , means add up the list. means add up over every data point, where counts the points: point 1, point 2, and so on. The sum sign is shorthand for writing every term out in full and adding them, so each formula stays short even when there are many data points.
- Deviations from the average, like
A deviation is how far a value sits from the average. If the average hours studied is 2.5 and a student studied 1 hour, the deviation is , a negative number because the student is below average. Deviations are positive above the average, negative below it, and they always add up to 0 across the whole list.
- Residuals and least squares
A residual is the miss for one data point: the observed value minus the predicted value, . A line that fits well has small residuals. Least squares is the rule for picking the best line. It squares each residual, adds the squares, and chooses the slope and intercept that make the total as small as possible. Squaring keeps positive and negative misses from canceling out.
What each symbol means
| Symbol | Meaning |
|---|---|
| The predicted output, the value the line gives for a chosen input . The hat marks it as a prediction rather than a measured value. It has the same unit as , such as points out of 10 for a quiz score. | |
| The input, a number you already know, such as hours of study. It has whatever unit the input is measured in, for example hours. The line uses to produce the prediction, so every guess starts from a chosen value of . | |
| The slope, also called the weight. It says how much the prediction changes when goes up by one unit. Its unit is the unit of divided by the unit of , so a slope of 0.7 points per hour means each extra hour adds 0.7 points. | |
| The intercept, also called the bias. It is the prediction when is 0, and it sets the height where the line crosses the vertical axis. Its unit is the same as the unit of , such as points on the quiz score scale. |
The formula piece by piece
Start with the input scaled by the slope. Multiplying by says how far the prediction moves for this input, because every extra unit of adds another to the result. A bigger gives a bigger change, at a steady rate.
Add the intercept to the scaled input. The intercept lifts or drops the whole line, so the prediction starts at the height before the input changes it. Together these two parts give the height of the straight line above any input.
The whole formula in one sentence: the predicted output equals the slope times the input, plus the intercept, so every input gives one point on a straight line.
The standard least-squares formula for the best slope, written with the averages. The top adds up how the inputs and outputs move together, and the bottom adds up how much the inputs spread out. Dividing the first by the second gives the slope.
What the formula is doing, and why
Linear regression is the simplest way to use one number to predict another. You start with pairs of values: an input and the output that you actually observed. The formula describes a straight line. For any input , multiply by the slope , add the intercept , and you get the predicted output .
Why choose the least-squares line? The hard part is choosing and . For each data point, the residual is the observed value minus the predicted value, . Least squares picks the line that makes the sum of the squared residuals as small as possible. Squaring stops positive and negative misses from canceling out, and it punishes big misses more than small ones.
Why the slope formula looks like that? Let and be the averages of the inputs and the outputs. The top of the slope formula adds up, over every point, the product of the two deviations, . Points that are above average in both values push the sum up, and points that are below average in both also push it up. The bottom adds up the squared input deviations, which is how much the inputs spread out. The intercept then follows from the averages: . The fitted line always passes through the point .
What does the slope tell you? A positive slope means the output tends to rise as the input rises. A negative slope means it tends to fall. A slope near zero says the input tells you little about the output. The line is only as good as the data behind it: when the real relationship curves, a straight line will miss the pattern.
An everyday picture
Think of a teacher with a scatter of dots on graph paper, one dot per student, with hours studied across the bottom and quiz score up the side. Linear regression is the ruler laid across the dots so the line sits as close to all of them as possible. To estimate a new student’s score, find their hours along the bottom and read the height of the ruler above that spot.
Worked example: Predicting a quiz score from hours of study
Four students report their study hours and get a quiz score out of 10. The data are hours and points. We will find the best line and then predict the score for a student who studies 5 hours. The four points are far too few for a real study, so treat this as a demonstration of the method.
- Find the average hours. The average of the inputs is hours. This is the balance point of the study times, and it is the first thing the slope formula needs.
- Find the average score. The average of the outputs is points. The line will always pass through the point made of both averages, .
- Find each deviation from the average. For the hours, , , and . For the scores, , , and . Negative deviations mean below average.
- Add up the products of the deviations. The top of the slope formula is . Multiplying the matching deviations gives , , and . Their total is , which says the hours and scores move together.
- Add up the squared hour deviations. The bottom of the slope formula is , which measures how much the hours spread out. We get .
- Divide to get the slope. The slope is points per hour. Each extra hour of study is linked, on average, with about 0.7 more points on the quiz.
- Find the intercept. The intercept is points. This is the predicted score for zero hours of study, and it places the line so that it passes through the averages.
- Predict for 5 hours. Put into the line: points. The prediction is made by reading the height of the line above 5 hours on the bottom axis.
Answer: The best line is . A student who studies 5 hours is predicted to score about 5.5 points out of 10. Each extra hour adds about 0.7 points to the prediction. Four data points is far too few for a real study, so treat this as a demonstration of the method.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Swapping the input and the output. The line predicts from , so the value you know goes in as and the value you want to predict is .
- Forgetting to subtract the averages in the slope formula. Using the raw and gives a different, wrong slope.
- Predicting far outside the data. A line fitted to 1 to 4 hours of study says little about 40 hours, because the pattern may not continue that far.
- Expecting a straight line to fit curved data. Linear regression always draws a straight line, even when the real pattern bends.
Where you will meet it
- Predicting house prices from floor area, where the slope gives a rough price per square meter.
- Forecasting sales, demand or energy use from a steady trend over time.
- Calibrating sensors, such as turning a voltage reading into a temperature by fitting a line to known reference points.
- Serving as the baseline model in statistics and data science courses, which more complex models must beat.
Questions people ask
What does least squares mean?
It is the rule for choosing the line. Among all straight lines, pick the one that makes the sum of the squared vertical distances from the points to the line as small as possible.
Is linear regression the same as correlation?
No. Correlation measures how tightly the points cluster around a line, on a scale from to . Regression gives the actual line, with a slope and an intercept you can use to predict.
How is linear regression different from logistic regression?
Linear regression predicts a number such as a score or a price. Logistic regression predicts the probability of a yes-or-no outcome, using an S-shaped curve.
Why is it called linear?
The prediction changes at a constant rate. Each extra unit of adds the same amount, , to the prediction, which is exactly what a straight line does.
Related formulas
- Mean Squared Error (MSE): Mean squared error measures how wrong a model’s guesses are by squaring each miss, so big misses count much more, and then averaging all of the squared misses.
- Gradient Descent: Gradient descent nudges a model’s settings a small step in the direction that makes the error smaller, then repeats that step again and again until the error stops falling.
- Logistic Regression: Logistic regression takes the same weighted sum as a straight line, then squeezes it through an S-shaped curve so the result is a probability between 0 and 1 for a yes-or-no question.
- L2 Regularization: L2 regularization adds a penalty for large weights, so the model prefers small, moderate weights that tend to hold up on new data.