Predict a continuous number — a price, a temperature, a demand. Fit linear and polynomial regression, read the coefficients, and measure error the right way.
Why: linear regression fits a straight-line relationship between features and a numeric target — the simplest, most interpretable regressor and the baseline every other model must beat. When: start here for any continuous target. Where: the coefficients tell you each feature’s effect, which is why it stays useful even when a fancier model wins.
from sklearn.linear_model import LinearRegression
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
lr = LinearRegression()
lr.fit(X_train, y_train)
print("R^2:", lr.score(X_test, y_test)) # fraction of variance explained
print("coefficients:", lr.coef_) # effect of each featureWhy: R² alone hides the size of the mistakes — MAE and RMSE report error in the target’s own units, which is what stakeholders understand. When: report RMSE when large errors matter more (it squares them), MAE when all errors count equally. Where: always evaluate on the held-out test set, never the training set.
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np
preds = lr.predict(X_test)
mae = mean_absolute_error(y_test, preds)
rmse = np.sqrt(mean_squared_error(y_test, preds))
print(f"MAE {mae:.3f} RMSE {rmse:.3f}") # in the units of y
# RMSE >> MAE means a few large errors are dominating.Why: when the relationship curves, a straight line underfits — adding polynomial features lets a linear model capture curvature while staying a linear model under the hood. When: use it when residuals show a systematic curve. Where: high degrees overfit fast, so pair polynomial features with the regularization from the next lesson.
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
# Degree-2 features let a linear model fit a curve. Wrap in a pipeline
# so the feature expansion is learned and applied consistently.
model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())
model.fit(X_train, y_train)
print("R^2:", model.score(X_test, y_test))