AI & ML: Regression Explained
Forecasting a Number
Regression predicts a continuous number. Tomorrow temperature, tomorrow sales, the price of a flat, the demand for electricity at noon. The core idea is a line or curve through a scatter of points that best matches the pattern of past data.
The Regression Equation
The simplest model is a straight line. The output equals an intercept plus a slope multiplied by the feature.
price = intercept + slope * size
price = 20000 + 1500 * sizeWith these values, a 90 square metre flat predicts 20000 plus 135000, around 155000. Machine learning fits the intercept and slope from data instead of guessing them.
How the Line Is Fit
For every training point, the line produces a prediction, and the vertical gap between prediction and true value is the residual. The model seeks the line that minimises the total squared residual, a recipe called least squares. Squaring removes the sign problem so above and below errors do not cancel out, and it punishes large errors harder.
def predict(size, intercept, slope):
return intercept + slope * size
def loss(y_true, y_predicted):
return (y_true - y_predicted) ** 2These two functions are a complete mental model of simple linear regression: predict with a line, score mistakes with squared error, then adjust the line until the score is small.
Reading the Result
The slope tells you the relationship direction and strength. A positive slope means larger features go with larger predictions; a negative slope means the opposite. The intercept anchors the line when the feature is zero, which is often outside the data range, so treat it as a technical constant rather than a meaningful fact.
Limits
Real relationships are rarely one straight line. Homes have neighbourhood effects, sales have seasons, and prices have ceilings. More features, non-linear models, and cross terms extend regression far beyond a single slope, but the core habit stays the same: fit, measure error, and check the line against data it never saw.
Key Points
- Regression predicts a continuous number such as a price or temperature.
- A line combines an intercept and slopes learned from data.
- Least squares minimises the sum of squared residuals.
- Always validate on holdout data the model has not seen.