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 A1A^{-1} such that AA1=IA \cdot A^{-1} = I (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 proportional
np.linalg.det(B) # 0.0 → singular matrix!

4. linalg.eig() — Eigenvalues & Eigenvectors (特征值与特征向量)#

Core idea: Find λ\lambda and vv such that Av=λvA \cdot v = \lambda \cdot v. 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 M=UΣVTM = U \Sigma V^T. 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 Ax=bAx = b for xx. More numerically stable than computing A1bA^{-1} \cdot b.

Ax=bx=linalg.solve(A,b)Ax = b \quad \Rightarrow \quad x = \text{linalg.solve}(A, b)

# Solve: 2x + y = 5
# x + 3y = 10
A = np.array([[2, 1], [1, 3]])
b = np.array([5, 10])
x = np.linalg.solve(A, b)
# [1. 3.] → x=1, y=3
# Verify
np.allclose(A @ x, b) # True
Note: Always prefer linalg.solve() over inv(A) @ b for numerical stability and performance.

7. Quick Comparison Table#

Function (函数)Mathematical OperationUse Case
@ / dot(A,B)ABABMatrix multiply
linalg.inv(A)A1A^{-1}Invert matrix
linalg.det(A)det(A)\det(A)Check singularity
linalg.eig(A)Av=λvAv = \lambda vPCA, stability
linalg.svd(A)UΣVTU\Sigma V^TCompression, rank
linalg.solve(A,b)Ax=bAx = bLinear systems
💡 One-line Takeaway
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/
Author
Alexander Lee
Published at
2026-03-12
License
CC BY-NC-SA 4.0