Time-series feature regression
Lag features, rolling means, seasonal decomposition, and Fourier seasonality components all feed into linear regression to forecast retail demand, energy load, traffic.
Name things for the reader, not the writer.
Name things for the reader, not the writer.
Linear Regression
Linear regression is the first and most important supervised learning algorithm. The model asserts that the target y is a linear function of features mathbf{x} in mathbb{R}^D plus Gaussian noise: y = mathbf{x}^top boldsymbolbeta + varepsilon, quad varepsilon sim mathcal{N}(0, sigma^2). Given trainin
Linear regression: ; closed-form .
Without the closed-form and MLE view of least squares, you cannot derive Ridge, Lasso, Bayesian regression, Kalman filters, or the linear output layer of a neural network in any principled way.
Linear regression is the first and most important supervised learning algorithm. The model asserts that the target is a linear function of features plus Gaussian noise: Given training data , we stack features into the design matrix and targets into , giving the matrix form .
The least squares solution minimizes the sum of squared residuals: Setting the gradient to zero gives the normal equations . When is invertible, The term is the Moore-Penrose pseudo-inverse — the closest thing to an inverse when is tall and thin.
From a probabilistic view, this same is the MLE: under Gaussian noise with fixed , maximizing is equivalent to minimizing squared error. Linear regression is a rare case where ERM and MLE coincide exactly, and both admit a closed-form solution — no iterative optimization needed.
Feature engineering extends the reach dramatically. Replacing with gives polynomial regression — still linear *in the parameters*, hence 'linear regression'. Radial basis functions, splines, Fourier features, and even random neural-network features all fit this template: choose a feature map , then solve a linear least-squares problem. This is the generalized linear model view and the foundation of kernel methods.
When (more features than samples) or when features are correlated, is singular and the solution is not unique. Ridge regression adds an penalty: — always invertible, numerically stable, and equivalent to MAP with a Gaussian prior. Lasso ( penalty) yields sparse solutions and is the workhorse for feature selection. Both are convex and are implemented in every ML library on Earth.
Python (in browser)
Expected: Both vectors agree (modulo numerical noise)
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Lag features, rolling means, seasonal decomposition, and Fourier seasonality components all feed into linear regression to forecast retail demand, energy load, traffic.
Linear models over hand-engineered features (collaborative + content + popularity) are still the production fallback when deep models are too slow or unexplainable.
When (e.g. millions of SNPs, tens of thousands of TF-IDF columns), L1+L2 hybrid regularization shrinks irrelevant features and stabilizes the rest.
from sklearn.linear_model import ElasticNetCV
model = ElasticNetCV(l1_ratio=[.1,.5,.9], cv=5).fit(X, y)The first and last layer of nearly every neural network is nn.Linear — a linear regression. Everything in between learns the right .
import torch.nn as nn
fc = nn.Linear(in_features=512, out_features=1)Put your understanding to the test. Score + streak + speed all count.
3 quick questions. Get 2 right to mark this lesson complete.