NumPy · Linear algebra
Matrix multiplication with @, solving linear systems, inverse vs solve, determinants, norms, and eigen decomposition.
This is the maths I’ll need for ML:
A * Bis element-wise.A @ Bis matrix multiplication (it needs inner dimensions to match:(m,k) @ (k,n)).- To solve
Ax = b, usenp.linalg.solve(A, b). It’s faster and more numerically accurate thaninv(A) @ b. np.linalg.det,np.linalg.norm,np.linalg.eig/eigh(eighfor symmetric matrices).- Check results with
np.allclose, because floats are rarely exactly equal.
import numpy as np
A = np.array([[3., 1.], [1., 2.]])
b = np.array([9., 8.])
print(A * A)
print(A @ A)
x = np.linalg.solve(A, b)
print("x =", x, "check:", np.allclose(A @ x, b))
print("det =", round(np.linalg.det(A), 6), "norm(b) =", round(np.linalg.norm(b), 4))
vals, vecs = np.linalg.eigh(A)
print("eigenvalues:", vals.round(4))
v = vecs[:, 0]
print("A v == lambda v:", np.allclose(A @ v, vals[0] * v))
print(np.linalg.inv(A).round(4))
Output:
[[9. 1.]
[1. 4.]]
[[10. 5.]
[ 5. 5.]]
x = [2. 3.] check: True
det = 5.0 norm(b) = 12.0416
eigenvalues: [1.382 3.618]
A v == lambda v: True
[[ 0.4 -0.2]
[-0.2 0.6]]Sources
- Linear algebra (numpy.linalg) — NumPy
- numpy.linalg.solve — NumPy
- CS229 Linear Algebra Review and Reference — Stanford CS229