What Is Linear Regression? Definition and Examples
Quick answer
Linear regression explained: what it means, how least squares picks the line, a hand calculation with made-up data, and a scikit-learn example.
Linear regression is a method that predicts a number by fitting a straight line, or a flat plane for several inputs, through your data. It finds the line that keeps the total squared gap between actual and predicted values as small as possible. A common use is predicting a price or a rent from size.
Key takeaways
- Linear regression models an output as a weighted sum of inputs plus a constant.
- The weights are chosen by least squares: minimise the sum of squared differences between data and model.
- It works well when the relationship is roughly straight; curves, outliers and tightly correlated inputs cause trouble.
- In scikit-learn,
LinearRegressiondoes this in a few lines.
On this page:
- What linear regression means
- How it works, with a hand calculation
- Where it is used
- Common mistakes and confusions
- Related terms
- Frequently asked questions
| Term | Predicts | Example question |
|---|---|---|
| Linear regression | A number | What rent for a 900 sq ft flat? |
| Logistic regression | A class or probability | Will this customer repay? |
| Correlation | Strength of a relationship, not a prediction | Do size and price move together? |
| Neural network | Numbers or classes, with curved relationships | Price from many mixed inputs |
What does linear regression mean?
It means modelling a numeric outcome as a straight-line function of the inputs. The NIST/SEMATECH e-Handbook of Statistical Methods describes a linear function as one where each explanatory variable is multiplied by an unknown parameter, and says that linear least squares regression is by far the most widely used modelling method.
In textbook notation, with one input, the model is y = b0 + b1 x + error. Here b0 is the intercept, b1 is the slope and the error is what the line cannot explain. This is standard textbook notation.
How does linear regression work?
It picks the parameters that minimise the squared errors. According to NIST, in the least squares method the unknown parameters are estimated by minimising the sum of the squared deviations between the data and the model. The scikit-learn documentation (version 1.9.1) says LinearRegression fits a linear model with coefficients w to minimise the residual sum of squares between the observed targets and those predicted by the linear approximation.
A hand calculation with made-up data. House size in hundreds of sq ft is (1, 2, 3) and price in lakhs is (3, 5, 7). The line price = 1 + 2 x size passes through all three points, so every residual is 0.
Now add a fourth point, size 4 with price 6 (illustrative). The old line predicts 1 + 2 x 4 = 9, so the residual is 6 - 9 = -3 and the sum of squared errors is 9. Refitting on all four points gives price = 2.5 + 1.1 x size, whose squared errors add up to 2.7, which is lower. Least squares settles on that line because no other straight line gives a smaller total.
The code for the first three points:
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1], [2], [3]])
y = np.array([3, 5, 7])
model = LinearRegression().fit(X, y)
print(model.intercept_, model.coef_) # about 1.0 and [2.]Change the arrays to the four-point data and the printed values become about 2.5 and 1.1.
Where is linear regression used?
It is used for predicting prices, demand, sales and costs, and as a baseline before trying complex models. An illustrative Indian scenario: predicting monthly rent in a city from flat area, using a small made-up sample. Real rent also depends on locality, floor, age of the building and more, so one input gives only a rough guide.
When it struggles (general textbook guidance): the relationship is curved, a few outliers dominate, or two inputs are strongly correlated with each other.
What are the common mistakes and confusions?
- Using it for yes/no outcomes. Despite its name, logistic regression is the usual tool for classes.
- Assuming a line must fit. Plot the data first; a curve needs a different approach or new features.
- Reading a coefficient as proof of cause. The line describes association in your sample.
- Judging fit only on the training data. Check on data the model has not seen.
Which terms are related?
Inputs are prepared through feature engineering. Classification results are read with a confusion matrix, and a model that fits noise shows overfitting. For more, see neural networks, data science vs data analytics and Python libraries for data analysis.
Next step
Run the code, then try the four-point data and confirm the 2.5 and 1.1 yourself. For a first Python toolkit, read Python for data science: a beginner's guide.
Frequently asked questions
What is linear regression in simple words?
Linear regression predicts a number by fitting a straight line through your data. It chooses the line that makes the total squared gap between actual and predicted values as small as possible.
What is the least squares method?
According to NIST, in least squares the unknown parameters are estimated by minimising the sum of the squared deviations between the data and the model. NIST also calls linear least squares regression by far the most widely used modelling method.
What do the slope and intercept mean?
In y = b0 + b1 x, the intercept b0 is the predicted value when x is 0, and the slope b1 is how much y changes for one unit increase in x. This is standard textbook notation.
How do I run linear regression in Python?
Use scikit-learn's LinearRegression: create the model, call fit with your inputs and targets, then read intercept_ and coef_. The estimator minimises the residual sum of squares.
When does linear regression not work well?
It struggles when the relationship is curved, when outliers dominate the fit, or when inputs are strongly correlated with each other. Plotting the data first helps you spot these cases.