Linear Algebra with numpy.linalg

numpy.linalg is NumPy's linear-algebra submodule — a collection of fast, LAPACK-backed functions for determinants, matrix inverses, solving systems of equations, eigenvalues, and vector or matrix norms.

Learn Linear Algebra with numpy.linalg in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll use np.linalg.det , np.linalg.inv , np.linalg.solve , np.linalg.eig , and np.linalg.norm to do real linear algebra in a few lines of code.

The determinant is a single number computed from a square matrix. It tells you whether the matrix is invertible: if the determinant is zero , the matrix is singular and has no inverse. For a 2×2 matrix [[a, b], [c, d]] the determinant is simply a*d − b*c .

Expected output: 10.0 then 0.0 . The second matrix is singular because its second row is exactly twice the first.

The inverse of a matrix A is the matrix A⁻¹ such that A @ A⁻¹ equals the identity matrix. Only square, non-singular matrices have an inverse. np.linalg.inv computes it for you.

Expected output: the inverse matrix, and A @ A_inv rounded to the 2×2 identity [[1. 0.] [0. 1.]] .

A system of linear equations can be written as A x = b , where A is a matrix of coefficients, b is the right-hand side, and x is the unknown vector. np.linalg.solve(A, b) returns x directly.

Expected output: [2. 3.] (so x = 2, y = 3), and A @ x reproduces [12. 5.] .

An eigenvector is a direction the matrix only stretches or shrinks; the stretch factor is its eigenvalue . np.linalg.eig returns (eigenvalues, eigenvectors) . Separately, np.linalg.norm measures a vector's length — by default the Euclidean (L2) norm, the square root of the sum of squared elements.

Expected output: eigenvalues [2. 3.] with the identity as eigenvectors, then 5.0 , 10.0 , and the unit vector [0.6 0.8] .

Replace each ___ so the program solves the system 2x + y = 5 , x + 3y = 10 .

You tried to invert or solve with a matrix whose determinant is 0.

✅ Fix: check np.linalg.det(A) first; a singular system has no unique solution.

❌ LinAlgError: Last 2 dimensions of the array must be square

Most linalg functions require a square matrix (same number of rows and columns).

✅ Fix: confirm A.shape is (n, n) before calling det, inv, solve, or eig.

Two orders: 2 coffees + 1 muffin cost $11, and 1 coffee + 2 muffins cost $10. Find the price of one coffee and one muffin by solving the system.

Lesson 15 complete — you can do linear algebra!

You computed determinants and inverses, solved a system of equations with np.linalg.solve , found eigenvalues, and measured vectors with np.linalg.norm .

🚀 Up next: Matrix Operations & Dot Products — learn the crucial difference between element-wise * and the matrix product @ .

Practice quiz

Which function computes the determinant of a square matrix?

  • np.linalg.det
  • np.linalg.norm
  • np.linalg.solve
  • np.linalg.eig

Answer: np.linalg.det. np.linalg.det returns the single determinant value of a square matrix.

What is the determinant of [[4, 7], [2, 6]]?

  • 38
  • 10.0
  • 0.0
  • 24

Answer: 10.0. For a 2x2 matrix it is a*d - b*c = 4*6 - 7*2 = 24 - 14 = 10.

Why is np.linalg.solve(A, b) preferred over np.linalg.inv(A) @ b?

  • It returns more digits
  • It works on non-square matrices
  • It is faster and more numerically accurate
  • It is the only one that exists

Answer: It is faster and more numerically accurate. solve never forms the full inverse, so it is faster and avoids extra rounding error.

What does np.linalg.solve(np.array([[3,2],[1,1]]), np.array([12,5])) return?

The system gives x = 2, y = 3, printed as [2. 3.].

What does np.linalg.norm([3, 4]) return?

  • 7.0
  • 12.0
  • 1.0
  • 5.0

Answer: 5.0. The default Euclidean norm is sqrt(3**2 + 4**2) = sqrt(25) = 5.0.

A matrix whose determinant is 0 is described as what?

  • Singular (no inverse)
  • Orthogonal
  • Symmetric
  • Invertible

Answer: Singular (no inverse). A zero determinant means the matrix is singular and has no inverse.

What does np.linalg.eig return?

  • Only eigenvalues
  • A tuple of (eigenvalues, eigenvectors)
  • Only eigenvectors
  • A determinant

Answer: A tuple of (eigenvalues, eigenvectors). eig returns both eigenvalues and eigenvectors as a tuple.

For the diagonal matrix [[2,0],[0,3]], the eigenvalues are:

A diagonal matrix has its diagonal entries as eigenvalues: [2. 3.].

What does A @ np.linalg.inv(A) give for an invertible A?

  • The zero matrix
  • The transpose of A
  • A doubled
  • The identity matrix

Answer: The identity matrix. A times its inverse yields the identity matrix.

Which error is raised when you invert a singular matrix?

  • LinAlgError: Singular matrix
  • IndexError
  • KeyError
  • OverflowError

Answer: LinAlgError: Singular matrix. Inverting a determinant-zero matrix raises LinAlgError: Singular matrix.