Fits a straight line (or hyperplane) y = β₀ + β₁x₁ + ... + βₙxₙ that best predicts a continuous target.
OLS (Ordinary Least Squares) finds the coefficients that minimize the sum of squared errors between predicted and actual values — squaring penalizes big misses much more than small ones and keeps the math differentiable.
Regularization adds a penalty for large coefficients to the cost function — it trades a little bias for a lot less variance, which fights overfitting.
Penalty ∝ sum of squared coefficients. Shrinks all coefficients toward zero but rarely to exactly zero. Good when most features are somewhat useful and are correlated with each other.
Penalty ∝ sum of absolute coefficients. Can shrink some coefficients to exactly zero — performs automatic feature selection. Good when you suspect many features are irrelevant.
Mix of L1 + L2. Gets Lasso's feature selection while handling groups of correlated features more gracefully than Lasso alone (which tends to arbitrarily pick one and zero out the rest).
Adds powers of existing features (x², x³, ...) as new inputs to a still-linear model — it's "linear in the coefficients" even though the curve it draws is not a straight line.
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2) X_poly = poly.fit_transform(X) # adds x^2 terms
An iterative way to find the coefficients that minimize the cost function — take small steps downhill on the cost "surface" in the direction that reduces error fastest.
Uses the entire dataset to compute each gradient step. Stable, accurate direction, but slow and memory-heavy on large data.
Updates using one random row at a time. Fast, can escape shallow local minima, but noisy — the path zig-zags toward the minimum.
Updates using a small batch (e.g. 32–256 rows). The practical default — balances speed and stability, and is what most deep learning training actually uses.
| Metric | What it measures | When to use |
|---|---|---|
| MAE | Average absolute error, same units as target | Easiest to explain to non-technical stakeholders; robust to outliers. |
| MSE | Average squared error | Penalizes large errors heavily — use when big misses are especially costly. |
| RMSE | Square root of MSE — back in original units | Like MSE's outlier sensitivity, but easier to interpret on the target's own scale. |
| R² | Proportion of variance in y explained by the model | Quick "how good is the fit" summary; 0–1 scale (can go negative for a bad fit). |
| Adjusted R² | R² penalized for adding features that don't help | Comparing models with different numbers of predictors — plain R² always rises (or stays flat) as you add features, even useless ones. |
from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score from sklearn.model_selection import train_test_split import numpy as np X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = LinearRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) rmse = np.sqrt(mse) r2 = r2_score(y_test, y_pred) print(f"MAE: {mae:.2f} | RMSE: {rmse:.2f} | R2: {r2:.3f}")