NumPy’s linalg module provides fast linear-algebra functions: matrix products, determinants, inverses, solving equations, eigenvalues, QR and SVD decompositions, and least squares. These power regression, PCA and much of machine learning.
What You Will Learn
- Matrix multiplication with @ and dot
- Trace, determinant, rank and norm
- Inverse and pseudo-inverse
- Solve linear systems and least squares
- Eigenvalues, QR and SVD
Prerequisites
Introduction to NumPy. Concepts are explained in Linear Algebra for ML.
Setup
import numpy as np
np.set_printoptions(precision=3, suppress=True)
A = np.array([[4, 1, 2], [1, 3, 0], [2, 0, 5]], dtype=float)
B = np.array([[1, 2, 0], [0, 1, 1], [1, 0, 1]], dtype=float)
Explanation: We use fixed matrices (instead of np.random.rand) so your output matches ours.
Matrix Multiplication
print(A @ B) # same as A.dot(B) or np.dot(A, B)
[[6. 9. 3.] [1. 5. 3.] [7. 4. 5.]]
Trace, Determinant, Rank, Norm
print(np.trace(A), round(np.linalg.det(A), 3))
print(np.linalg.matrix_rank(A), np.linalg.norm(A))
12.0 43.0 3 7.745966692414834
Explanation: Trace = sum of the diagonal (4 + 3 + 5). trace is in the main NumPy namespace, not linalg. A non-zero determinant means the matrix is invertible; rank 3 means its rows are independent.
Inverse and Solving Equations
print(np.linalg.inv(A))
print(np.linalg.solve(A, [7, 4, 7]))
[[ 0.349 -0.116 -0.14 ] [-0.116 0.372 0.047] [-0.14 0.047 0.256]] [1. 1. 1.]
Explanation: solve finds x in A·x = b directly (here x = 1, 1, 1), which is faster and more accurate than computing the inverse first.
Least Squares and Pseudo-Inverse
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4]], dtype=float)
y = np.array([2.1, 3.9, 6.2, 7.8])
sol, residuals, rank, sv = np.linalg.lstsq(X, y, rcond=None)
print(sol, residuals, rank)
print(np.linalg.pinv(X) @ y)
[0.15 1.94] [0.082] 2 [0.15 1.94]
Explanation: With more equations than unknowns there is no exact solution; lstsq finds the best fit – this is linear regression: y ≈ 0.15 + 1.94x. The pseudo-inverse gives the same answer and also works for non-square matrices.
Eigenvalues, QR and SVD
w, v = np.linalg.eigh(A) # eigh: for symmetric matrices
print(w)
q, r = np.linalg.qr(A)
print(np.allclose(q @ r, A))
u, s, vt = np.linalg.svd(A)
print(s)
[1.855 3.476 6.669] True [6.669 3.476 1.855]
Explanation: For a symmetric matrix the singular values equal the absolute eigenvalues. Use eig for general (non-symmetric) matrices – it can return complex numbers. QR and SVD are used inside least squares, PCA and recommendation systems.
| Function | Purpose |
|---|---|
| @, dot() | Matrix product |
| trace(), linalg.det() | Trace, determinant |
| linalg.inv(), linalg.pinv() | Inverse, pseudo-inverse |
| linalg.solve() | Solve A·x = b |
| linalg.lstsq() | Least-squares fit |
| linalg.eig(), eigh() | Eigenvalues and vectors |
| linalg.qr(), svd() | Decompositions |
| linalg.norm(), matrix_rank() | Size and rank |
Series
Interview Questions
Q: Why prefer solve() over inv()?
It is faster and numerically more stable.
Q: eig vs eigh?
eigh is for symmetric/Hermitian matrices and returns real, sorted eigenvalues.
Practice Exercises
- Solve 3x + 2y − z = 1, 2x − 2y + 4z = −2, −x + 0.5y − z = 0.
- Fit y = a + bx to 6 points of your own with lstsq.
Summary
numpy.linalg handles products, determinants, inverses, solving systems, least squares and decompositions – the building blocks of ML algorithms.
Machine Learning Tutorials, Courses and Certifications