Systems of Linear Equations
Fitting a linear model, finding equilibrium prices or blending ingredients all lead to the same problem: find with . A system can have exactly one solution, none, or infinitely many, and ML runs into all three cases. This lesson shows how to tell them apart and how to solve each one.
Three possible outcomes
For , write the augmented matrix and row reduce it (Gaussian Elimination). Then:
| Case | Sign in the row-echelon form | Typical in ML |
|---|---|---|
| unique solution | a pivot in every column of , no contradiction | square, well-posed problems |
| no solution | a row with | more data points than parameters (), noisy data |
| infinitely many | a column of without a pivot (a free variable) | more parameters than data points () |
When there are infinitely many solutions, the general solution is one particular solution plus anything in the null space:
import numpy as np
A = np.array([[1.0, 2.0, 1.0],
[2.0, 4.0, 3.0]]) # 2 equations, 3 unknowns
b = np.array([4.0, 9.0])
x_p = np.linalg.lstsq(A, b, rcond=None)[0] # one particular solution (the shortest one)
n = np.array([-2.0, 1.0, 0.0]) # A n = 0: a null-space direction
print("rank:", np.linalg.matrix_rank(A))
print("A x_p =", np.round(A @ x_p, 10), " A n =", A @ n)
for lam in (0.0, 1.0, -3.0):
x = x_p + lam * n
print(f"lambda={lam:4}: x = {np.round(x, 4)}, A x = {np.round(A @ x, 10)}")rank: 2
A x_p = [4. 9.] A n = [0. 0.]
lambda= 0.0: x = [0.6 1.2 1. ], A x = [4. 9.]
lambda= 1.0: x = [-1.4 2.2 1. ], A x = [4. 9.]
lambda=-3.0: x = [ 6.6 -1.8 1. ], A x = [4. 9.]
Every choice of solves the system. In ML this is the over-parameterized case: many weight vectors fit the training data perfectly, and something extra (regularization, or the implicit bias of gradient descent) has to choose one.
Solving square systems in code
For a square invertible , the solution is , but code should call np.linalg.solve(A, b). It uses an LU factorization (Gaussian elimination stored as two triangular matrices), which needs about a third of the work of forming and loses less accuracy to rounding.
import numpy as np
A = np.array([[4.0, 1.0, 0.0],
[1.0, 3.0, 1.0],
[0.0, 1.0, 2.0]])
b = np.array([1.0, 2.0, 3.0])
x1 = np.linalg.solve(A, b)
x2 = np.linalg.inv(A) @ b
print("solve:", np.round(x1, 6), " inv:", np.round(x2, 6))
print("residual norm:", np.linalg.norm(A @ x1 - b) < 1e-12)solve: [0.222222 0.111111 1.444444] inv: [0.222222 0.111111 1.444444]
residual norm: True
No solution: the least squares compromise
With more equations than unknowns, noisy data usually makes the system inconsistent. Instead of an exact solution, we look for the that makes smallest. It solves the normal equations , and the Moore-Penrose pseudo-inverse (for independent columns) packages that solution. This is exactly linear regression.
In software
np.linalg.solve,scipy.linalg.solveandtorch.linalg.solveuse LU factorization with pivoting.np.linalg.lstsqandnp.linalg.pinvhandle the non-square and rank-deficient cases with the SVD.- Large sparse systems (finite elements, graph problems) use iterative solvers such as conjugate gradient instead.
Exercises
Exercise 1: Classify the system
How many solutions does , have?
Answer
None.
Solution
Subtract twice the first row from the second: , which says . The two lines are parallel and never meet.
Exercise 2: General solution
Find all solutions of (one equation, two unknowns).
Answer
for any real .
Solution
is free. Setting gives the particular solution . The homogeneous equation is solved by , so
Exercise 3: Solve and check (code)
Solve , , with np.linalg.solve.
Answer
, , .
Solution
import numpy as np
A = np.array([[2.0, 1, -1], [1, 3, 2], [1, -1, 1]])
b = np.array([1.0, 13, 2])
print(np.round(np.linalg.solve(A, b), 10))[1. 2. 3.]
Check the last equation: .
Test yourself
A row in the reduced augmented matrix means…
- the system has infinitely many solutions
- the system has no solution
- the matrix is invertible
A model has more parameters than training examples and fits them exactly. Typically…
- the fit is unique
- infinitely many parameter vectors fit equally well
- no parameter vector fits
- the data matrix is square
Why prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b?
invgives the wrong answersolvefactorizes once and is faster and more accuratesolveworks without numbers- they differ only in spelling