601 words
3 minutes
NumPy Linear Algebra

V. NumPy Linear Algebra (线性代数)
The
numpy.linalg module provides standard linear algebra (线性代数) operations on 2-D arrays treated as matrices (矩阵). These are essential for machine learning, physics simulations, and engineering calculations. 1. Matrix Multiplication (矩阵乘法)
Core idea: Use @ or np.dot() for matrix multiplication — NOT * (which is element-wise).
import numpy as np
A = np.array([[1, 2], [3, 4]])B = np.array([[5, 6], [7, 8]])
A @ B # Matrix multiply (矩阵乘法)np.dot(A, B) # Same result: [[19 22] [43 50]]
A * B # Element-wise multiply (逐元素乘法) — different!Note: Always use @ or np.dot() for matrix multiplication. Using * gives element-wise results, which is a common mistake.
2. linalg.inv() — Inverse Matrix (逆矩阵)
Core idea: Find matrix such that (identity matrix).
A = np.array([[1, 2], [3, 4]])
A_inv = np.linalg.inv(A)# [[-2. 1. ]# [ 1.5 -0.5]]
# Verify: A @ A_inv ≈ Identity matrix (单位矩阵)np.round(A @ A_inv) # [[1. 0.] [0. 1.]]Note: Only square, non-singular matrices (非奇异矩阵) are invertible. A singular matrix (奇异矩阵) will raise LinAlgError.
3. linalg.det() — Determinant (行列式)
Core idea: A scalar value describing matrix properties. If det = 0, the matrix is singular (not invertible).
A = np.array([[1, 2], [3, 4]])np.linalg.det(A) # -2.0
B = np.array([[1, 2], [2, 4]]) # rows are proportionalnp.linalg.det(B) # 0.0 → singular matrix!4. linalg.eig() — Eigenvalues & Eigenvectors (特征值与特征向量)
Core idea: Find and such that . Core of PCA (主成分分析) and many ML algorithms.
A = np.array([[4, 1], [2, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)# eigenvalues: [5. 2.]# eigenvectors: columns are the eigenvectors
print(eigenvalues) # [5. 2.]print(eigenvectors) # [[0.707 -0.447] # [0.707 0.894]]5. linalg.svd() — Singular Value Decomposition (奇异值分解)
Core idea: Decompose any matrix . Used in data compression, image processing, and recommendation systems.
A = np.array([[1, 2], [3, 4], [5, 6]])
U, S, Vt = np.linalg.svd(A)# U: left singular vectors (shape: 3×3)# S: singular values (shape: 2,) — diagonal of Σ# Vt: right singular vectors transposed (shape: 2×2)
print(S) # [9.525 0.514]6. linalg.solve() — Solve Linear Equations (线性方程组)
Core idea: Solve for . More numerically stable than computing .
# Solve: 2x + y = 5# x + 3y = 10A = np.array([[2, 1], [1, 3]])b = np.array([5, 10])
x = np.linalg.solve(A, b)# [1. 3.] → x=1, y=3
# Verifynp.allclose(A @ x, b) # TrueNote: Always prefer linalg.solve() over inv(A) @ b for numerical stability and performance.
7. Quick Comparison Table
| Function (函数) | Mathematical Operation | Use Case |
|---|---|---|
@ / dot(A,B) | Matrix multiply | |
linalg.inv(A) | Invert matrix | |
linalg.det(A) | Check singularity | |
linalg.eig(A) | PCA, stability | |
linalg.svd(A) | Compression, rank | |
linalg.solve(A,b) | Linear systems |
💡 One-line Takeaway
Use
Use
@ for matrix multiplication, linalg.solve() instead of inv() for equation solving, and check det() before inverting. NumPy Linear Algebra
https://lxy-alexander.github.io/blog/posts/numpy/api/05numpy-linear-algebra/