Linear systems and rank
Quick answer
A linear system Ax = b asks for unknown coefficients x that satisfy several equations at once. A matrix’s rank is the number of linearly independent columns, equivalently independent rows. A square n-by-n matrix of rank n has exactly one solution for every right-hand side. A rank-deficient system may have no solution or infinitely many, depending on b. In ML, duplicated features can make coefficients non-identifiable even when predictions are well determined. This is a small exact-system lesson, not yet a regression fitting method.
Eliminate an unknown by hand
For x + y = 7 and x - y = 1, add the equations to obtain 2x = 8, so x = 4 and y = 3. The following code verifies both equations rather than assuming the candidate is correct.
x, y = 4, 3print(x + y, x - y)print([x + y, x - y] == [7, 1])Expected output:
7 1TrueBoth residuals (predicted minus required right-hand side) are zero. The two independent equations determine the two unknowns.
Solve a full-rank system and diagnose a duplicate row
NumPy CPU lab: np.linalg.solve solves a square nonsingular system without explicitly forming an inverse. matrix_rank estimates numerical rank using a singular-value tolerance; the exact integer fixture is far from the ambiguous near-dependent case. allclose checks the floating-point residual with explicit tolerances.
import numpy as npA = np.array([[1., 1.], [1., -1.]])b = np.array([7., 1.])x = np.linalg.solve(A, b)print(x.tolist())print(bool(np.allclose(A @ x, b, rtol=0, atol=1e-12)))C = np.array([[1., 2.], [2., 4.]])print(int(np.linalg.matrix_rank(C)))try: np.linalg.solve(C, np.array([3., 6.]))except np.linalg.LinAlgError: print("singular system")Expected output:
[4.0, 3.0]True1singular systemThe second row of C doubles the first. For right-hand side [3, 6], both [3, 0] and [1, 1] satisfy the equations; for [3, 7], no solution exists. A small residual alone is not proof of a unique or numerically stable solution.
Common mistakes
Do not solve by computing an explicit inverse as a default. Do not use a determinant magnitude as a general measure of numerical stability; scaling changes it. Numerical rank depends on tolerance and scale. Rectangular or inconsistent systems require a separately defined objective, such as least squares, introduced later.
Practice and next steps
For equations x + 2y = 5 and 2x + 4y = 10, give two distinct solutions. Change the second right-hand side to 11 and explain the contradiction. Next, describe independent directions with bases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Linear systems and rank — knowledge check
Apply linear algebra to small numerical fixtures.
Which pair solves x+y=9 and x-y=3?
What is the exact rank of [[1,2],[2,4]]?
If x+y=3 and 2x+2y=7, how many exact solutions exist?
Which NumPy function directly solves a square nonsingular linear system?
Does a zero residual always prove a unique solution?