Sahaj.blogs
22 Feb 2024 · StudyPhase 3 · NumPy

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 * B is element-wise. A @ B is matrix multiplication (it needs inner dimensions to match: (m,k) @ (k,n)).
  • To solve Ax = b, use np.linalg.solve(A, b). It’s faster and more numerically accurate than inv(A) @ b.
  • np.linalg.det, np.linalg.norm, np.linalg.eig / eigh (eigh for 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]]

#numpy#linear-algebra

Sources

  1. Linear algebra (numpy.linalg) — NumPy
  2. numpy.linalg.solve — NumPy
  3. CS229 Linear Algebra Review and Reference — Stanford CS229