Linear systems & inverses
Step 5 of 18 in Foundations · Concept · Medium
Solving Ax = b asks which input produces a given output. When A is invertible there is one answer; when it is not, there are none or infinitely many. Linear regression is exactly this problem.
The lesson
Ax = b reads: I know the transformation A and the output b; which input x produced it? Gaussian elimination answers it by subtracting multiples of one row from another until the system is a triangle, then solving from the bottom up.
- Row 2 minus half of row 1: 2.5y = 7.5, so y = 3
- Back into row 1: 2x + 3 = 5, so x = 1
- Check in row 2: 1 + 9 = 10
A has an inverse when it squashes nothing flat: its columns are independent and its determinant is not zero. Then there is exactly one x. When A does flatten space (a singular matrix), some outputs b cannot be reached at all, and the ones that can are reached by infinitely many x.
x = np.linalg.solve(A, b) # use this, not np.linalg.inv(A) @ bCommon mistake. Do not compute the inverse to solve a system; solve is faster and more accurate. In your own elimination, always swap up the row with the largest pivot. Dividing by a tiny pivot magnifies rounding error.
Linear regression is this problem with more equations than unknowns. No x fits them all, so you take the x with the smallest squared error.
Check yourself
3x + y = 9 and x − y = −1. What is x?
Show the answer
2. Add the equations: 4x = 8, so x = 2 (and y = 3).
A = [[1, 2], [2, 4]]. The system Ax = b has:
- exactly one solution for every b
- no solution or infinitely many, depending on b
- no solution for any b
Show the answer
no solution or infinitely many, depending on b. Row 2 is twice row 1, so the determinant is 1·4 − 2·2 = 0. A flattens the plane onto a line. b on that line is reached by infinitely many x; b off it by none.
What is the determinant of [[2, 1], [1, 3]]?
Show the answer
5. ad − bc = 2·3 − 1·1 = 5. Not zero, so the matrix is invertible.
Build it
Gaussian elimination
solve(A, b) with Gaussian elimination and partial pivoting.
Done when: Matches np.linalg.solve; detects a singular matrix.
Runs in your browser: write the code on the map and the tests mark it.
Resources
Earn this star
On the map, your answers are marked and you log what you built. Finish the step and its star lights.
Open this step on the map