Linear Algebra with NumPy
Why linear algebra is useful
Section titled “Why linear algebra is useful”Linear algebra appears in:
- Regression
- Optimization
- Dimensionality reduction
- Correlation and covariance
NumPy provides numpy.linalg for common operations like decompositions, inverses, and determinants.
Dot product
Section titled “Dot product”import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b)) # 1*4 + 2*5 + 3*6Matrix multiplication
Section titled “Matrix multiplication”* on two 2D arrays is element-wise. Matrix multiplication needs @ or np.matmul (equivalently np.dot for 2D arrays).
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[10, 20], [30, 40]])
print(A @ B)
print(np.matmul(A, B))flowchart LR A["Two matrices"] --> B["@ / np.matmul
matrix multiplication"] A --> C["np.linalg.solve(A, b)
solve Ax = b"] A --> D["np.linalg.inv(A)
matrix inverse"] A --> E["np.linalg.det(A)
determinant"] A --> F["np.linalg.eig(A)
eigenvalues/eigenvectors"] A --> G["np.linalg.norm(v)
vector length"]
Transpose
Section titled “Transpose”import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A.T)Determinant and inverse
Section titled “Determinant and inverse”import numpy as np
A = np.array([[1, 2], [3, 4]])
det = np.linalg.det(A)
inv = np.linalg.inv(A)
print("det:", det)
print("inv:\n", inv)Solve a system of equations
Section titled “Solve a system of equations”Solving A x = b directly with np.linalg.solve is faster and more numerically stable than computing inv(A) @ b.
import numpy as np
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])
x = np.linalg.solve(A, b)
print(x)Eigenvalues and eigenvectors
Section titled “Eigenvalues and eigenvectors”import numpy as np
A = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(A)
print("eigenvalues:", vals)
print("eigenvectors:\n", vecs)Norms (vector length)
Section titled “Norms (vector length)”import numpy as np
v = np.array([3, 4])
print(np.linalg.norm(v)) # 5Continue to: Statistical Functions in NumPy for mean/median/std/percentiles and basic descriptive analytics.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Dot Product
Section titled “Exercise 1 – Dot Product”Exercise 2 – Matrix Multiplication with @
Section titled “Exercise 2 – Matrix Multiplication with @”Exercise 3 – Solve a Linear System
Section titled “Exercise 3 – Solve a Linear System”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading