scipy.linalg.lstsq
Industry-standard least-squares solver with selectable backends (gelsd/gelsy/gelss based on SVD/QR). Used inside half of scipy's optimization routines.
from scipy.linalg import lstsq
w, residuals, rank, sv = lstsq(X, y)Name things for the reader, not the writer.
Name things for the reader, not the writer.
Linear Regression
Every algebraic formula has a geometric story. For linear regression, the story is strikingly elegant: least squares is orthogonal projection. The fitted values hat{mathbf{y}} = Xhat{boldsymbolbeta} are the projection of mathbf{y} onto the column space of X — the subspace of mathbb{R}^N reachable by
The least-squares fit is the orthogonal projection of onto the column space of .
Without this geometric view, you can compute regression but cannot picture *why* it works.
Every algebraic formula has a geometric story. For linear regression, the story is strikingly elegant: least squares is orthogonal projection. The fitted values are the projection of onto the column space of — the subspace of reachable by linear combinations of the feature columns.
The residual is orthogonal to every column of : . This is exactly the normal equation . Geometrically, the closest point in the column space to is found by dropping a perpendicular; algebraically, the normal equations enforce that perpendicularity. The two views are identical.
The matrix that performs this projection has a name: is the hat matrix (or projection matrix), because . It is symmetric (), idempotent (), and has eigenvalues or — hallmarks of a projection. The complementary matrix projects onto the residual space, orthogonal to . Together they decompose into signal and residual subspaces.
The trace of equals the rank of , which equals for full-rank problems — this is the effective number of parameters. The trace of is , the degrees of freedom for residual estimation. The unbiased estimator of noise variance is , using the dimensionality of the residual space rather than .
This projection viewpoint generalizes beautifully. Ridge regression corresponds to shrinkage of projections along directions of small singular values. Kernel regression projects onto an infinite-dimensional feature space but computes everything via the kernel matrix. Principal Component Regression first projects onto the top principal components, then regresses. Whenever you see a closed-form least-squares solution, there is a projection hiding behind it — and understanding the geometry makes the algebra almost obvious.
Python (in browser)
Expected: X.T @ residual ≈ 0 (within machine epsilon)
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Industry-standard least-squares solver with selectable backends (gelsd/gelsy/gelss based on SVD/QR). Used inside half of scipy's optimization routines.
from scipy.linalg import lstsq
w, residuals, rank, sv = lstsq(X, y)Algorithms like POCS (projection onto convex sets) and ISTA repeatedly project onto feasible subspaces — the same geometry as least-squares projection, in service of sparse recovery.
Plotting residuals vs fitted values catches non-linearity, heteroscedasticity, and outliers. Standardized residuals + leverage = Cook's distance, the classic influence diagnostic.
Put your understanding to the test. Score + streak + speed all count.
3 quick questions. Get 2 right to mark this lesson complete.