🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI Freenp.linalg.solve() Linear Systems

To solve a set of linear equations, find the values of its unknowns. NumPy’s np.linalg.solve() does that when the coefficient matrix has the same number of rows and columns and its equations are independent.
I’ll show how to put the equations into NumPy arrays and check the answer.
TL;DR
Use np.linalg.solve(A, b) to find x in A @ x = b when A is square and full rank.
- Put each equation’s coefficients in a row of A.
- Put constants in a one-dimensional b for one solution vector.
- Check the answer with np.allclose(A @ x, b).
What Is np.linalg.solve()?
np.linalg.solve() finds x for the linear system A @ x = b. A is the coefficient matrix, b holds known values, and x contains the unknowns.
For x + 2y = 4 and 3x − 5y = 1, A is [[1, 2], [3, -5]], b is [4, 1], and x contains the values for x and y. A must be square and full rank, which means its rows provide independent equations and the system has a unique solution.
Use the function for a determined system, not to fit noisy observations. Read the NumPy solve reference for the full signature.
How to Solve a Linear System Step by Step
Build A and b from the equations, call solve, then check the product against the original right-hand side.
Step 1: Write the coefficients as arrays
Each row of A follows one equation, with columns in the same variable order. Here, the first column represents x and the second represents y.
import numpy as np
A = np.array([[1, 2], [3, -5]])
b = np.array([4, 1])
The shape of A is (2, 2), while b has shape (2,). That one-dimensional right-hand side represents one value for each equation.
Step 2: Solve and verify the result
Call np.linalg.solve(A, b) and compare A @ x with b. I ran the example as python3 solve_system.py; the result was x = [2., 1.], and the product returned [4., 1.].
x = np.linalg.solve(A, b)
print("x:", x)
print("A @ x:", A @ x)
print("matches b:", np.allclose(A @ x, b))
x: [2. 1.]
A @ x: [4. 1.]
matches b: True

Floating-point calculations can have small rounding differences, so np.allclose() is more suitable than exact equality.
Step 3: Solve for several right-hand sides
Each column of a two-dimensional B is a separate right-hand side, and X keeps those columns in order.
B = np.array([[4, 1], [1, 2]])
X = np.linalg.solve(A, B)
print("multiple solutions:")
print(X)
print("matches B:", np.allclose(A @ X, B))
The first column solves A @ x = [4, 1], while the second solves A @ x = [1, 2]. NumPy supports stacks of matrices whose leading dimensions broadcast.
NumPy 2.0 and later treats only one-dimensional b as a vector. A column array shaped (M, 1) is a matrix right-hand side, not the same shape as (M,). Check b.ndim when adapting older examples.
The AskPython guides to np.linalg.eigvals() and np.linalg.norm() cover related linear algebra operations.
Shape Errors, Singular Matrices, and Alternatives
Check the dimensions and independence of A before interpreting a solve result. A non-square matrix or singular coefficients cannot produce the answer expected from a unique solve.
| Case | What happens | What to do |
|---|---|---|
| A is not square | LinAlgError because the last two dimensions must be square | Use np.linalg.lstsq() for an over- or under-determined fit |
| A is singular | LinAlgError: Singular matrix | Check for duplicate or dependent equations. If an approximate fit suits the task, use lstsq() |
| b has shape (M, 1) | Returns a two-dimensional solution array | Use a one-dimensional b when the desired answer is a vector |
I passed a singular matrix [[1, 2], [2, 4]] with b=[3, 6] and received LinAlgError: Singular matrix. The second row is twice the first, so A does not have full rank. In this case np.linalg.lstsq() returned [0.6, 1.2], a least-squares solution, rather than a unique solution from solve.
The lstsq result also includes residuals, rank, and singular values.
For an ordinary system, use solve directly rather than forming an inverse and multiplying by b.
Least squares minimizes residual error, measured by ||b − A @ x||. An overdetermined system has more equations than unknowns, while an underdetermined one can leave multiple exact candidates. Choose this method only when its fit objective matches the task.
Conclusion
Choose the solver that fits the system, and consult the NumPy references for details.
See the NumPy solve documentation and the least-squares reference for parameter and return details.
FAQ
These answers address common choices when calling np.linalg.solve().
What does np.linalg.solve() do?
It solves A @ x = b for x when A is square and full rank.
Why use solve() instead of inv()?
solve(A, b) directly solves the system and avoids forming an inverse just to multiply it by b.
What should I use when A is not square?
Use np.linalg.lstsq() when a least-squares solution is appropriate for an over- or under-determined system.


