QR and SVD solvers
Quick answer
Least squares can be solved without explicitly inverting a matrix. For a full-column-rank design matrix, QR factorization transforms the problem into a triangular solve. Singular value decomposition (SVD) also handles rank deficiency by separating directions that are identifiable from directions with little or no information. All code examples run locally in Python 3.13.5; library examples use scikit-learn 1.8.0 or NumPy 2.3.5. ByteMind package execution is unverified.
QR agrees with least squares
import numpy as npA=np.array([[1.,0.],[1.,1.],[1.,2.],[1.,3.]])y=np.array([1.,4.,5.,8.])Q,R=np.linalg.qr(A,mode='reduced')beta=np.linalg.solve(R,Q.T@y)other=np.linalg.lstsq(A,y,rcond=None)[0]print(np.round(beta,2).tolist())print(bool(np.allclose(beta,other)))Expected output:
[1.2, 2.2]TrueThe full-rank QR and rank-aware solver agree within tolerance.
How it works
Differentiating ||A beta-y||² gives 2*A.T@(A@beta-y); stationarity implies A.T@A@beta=A.T@y (normal equations). If A has full column rank, this gives a unique solution. Explicitly forming A.T@A squares the 2-norm condition number for full rank, magnifying sensitivity; explicit inverse is generally unnecessary. For reduced QR A=Q R, Q has orthonormal columns and R is square upper triangular when n>=p and full column rank. Solve R beta=Q.T@y using a triangular or linear solver. If rank deficient, ordinary unpivoted QR followed by solve is not a reliable general solution. SVD writes A=U diag(s) V.T; invert only retained singular values according to a tolerance, yielding the minimum Euclidean norm solution under the selected numerical rank. NumPy lstsq returns solution, residual sum array, effective rank and singular values; residual array may be empty for rank-deficient or non-overdetermined cases, so compute SSE directly if needed. Small singular values indicate sensitivity, not automatically useless features. These numerical choices do not cure data leakage or confounding.
Dependent columns and minimum norm
import numpy as npA=np.array([[1.,1.],[2.,2.],[3.,3.]])y=np.array([2.,4.,6.])beta,res,rank,s=np.linalg.lstsq(A,y,rcond=None)print(rank,res.size,np.round(beta,6).tolist())print(bool(np.allclose(A@beta,y)))Expected output:
1 0 [1.0, 1.0]TrueBoth columns encode the same direction. The minimum-norm solution splits the total coefficient evenly; the residual array is empty because rank is deficient.
Common mistakes
Avoid inv(A.T@A)@A.T@y as a default recipe. Do not treat an empty lstsq residual array as evidence of zero prediction error. In dependent-column problems compare predictions and residual norm, not arbitrary coefficients.
Practice and next steps
Duplicate a column of a small design matrix. Predict the effect on rank, then use lstsq and compare fitted values before and after duplication. Complete the chapter knowledge check and coding exercise. Next: Residual diagnostics.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
QR and SVD solvers — knowledge check
Apply qr and svd solvers to small regression fixtures.
What is the normal-equation stationarity condition?
What happens to the 2-norm condition number when forming A.T@A for full rank?
Which condition makes the stated reduced QR triangular solve valid?
Two identical columns yield what?
An empty NumPy lstsq residual array means what?