Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
CHAPTER 11 / 28 · SUPERVISED MODELS

Lines, coefficients, and regularisation.

See how numeric features combine into a prediction and why coefficients need context.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

Linear regression predicts a number by adding weighted feature values. With one feature, the rule is a line: prediction = intercept + coefficient × feature. With several features, each receives its own coefficient and the contributions are added. A coefficient describes how the prediction changes when that feature increases by one unit while the other features are held fixed in the model. It is a relationship inside the fitted model, not automatic evidence of cause in the real world.

The fitting method chooses coefficients to reduce a loss on training examples, commonly squared error. If the target is house price and the feature is area, a positive coefficient means the model's predicted price rises as area rises. The actual relationship may bend, differ by neighborhood, or depend on interactions the model does not include. The word “linear” describes how the coefficients combine with the supplied feature columns; transformed features can let a linear model represent more complex curves.

02 / THE MODEL

How the idea works.

Coefficients depend on units. A coefficient per square meter will have a different number from a coefficient per square foot even if the prediction rule is equivalent. If two features carry nearly the same information, their individual coefficients can become unstable: the model can distribute weight between them in several ways while making similar predictions. Reading a coefficient without checking feature definitions, scaling, and correlations can be misleading.

Regularisation adds a preference for smaller or simpler coefficient values during fitting. Ridge regression penalises squared coefficient sizes, generally shrinking them toward zero. Lasso penalises absolute sizes and can drive some coefficients exactly to zero, acting as a kind of feature selection. The penalty strength is a hyperparameter: too weak may permit a fragile fit, while too strong may erase useful signal. It must be selected on validation data.

A linear model has advantages: it is fast, provides a clear mathematical form, and can be a strong baseline. Its limits are equally important. It extrapolates beyond observed feature ranges according to its line, even when the real process has a ceiling or changes regime. Residual patterns and subgroup performance help show when the simple shape is inadequate.

fitted rulefeaturetarget
A flexible rule can match observed points closely, yet its shape between and beyond them still matters.
03 / PYTHON, EXPLAINED

Follow the code.

linear-regression.py
from sklearn.linear_model import LinearRegression

X = [[1], [2], [3], [4]]
y = [3, 5, 7, 9]
model = LinearRegression()
model.fit(X, y)
print(model.coef_, model.intercept_)
print(model.predict([[5]]))

The four training rows pair input values 1 through 4 with targets 3 through 9. The relation is y = 2 × x + 1, so fitting finds a coefficient close to 2 and an intercept close to 1. model.coef_ is an array because a model may have several feature coefficients. The underscore at the end of a scikit-learn attribute conventionally marks learned state created by fit. predict receives a new one-row feature table [[5]] and returns a value close to 11.

This exact line is a teaching example. Real observations contain noise, so a fit will not usually pass through every point. Predicting at 5 is just beyond the training range; predicting at 5,000 would be an extreme extrapolation with no support from these examples. The code expresses the model form, while evaluation on unseen cases tells us whether that form is useful.

04 / THE BOUNDARY

What the result does not say.

A polynomial feature such as area squared can let a linear estimator fit a curve, but it also increases flexibility. Interactions such as area × neighborhood can represent different slopes for groups. These are design choices about representation and can overfit if added indiscriminately.

If errors have unequal importance, ordinary squared-error fitting may not match the task. Robust or quantile methods offer other objectives. The target's meaning should guide the loss, not the convenience of a default estimator.

05 / KEEP THIS

One clear idea.

Linear regression adds weighted features; coefficients describe its rule, while validation judges its usefulness.

THE CODE IN CONTEXT

The ML cookbook

Complete, explained Python workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.