Introduction
NumPy's linalg module provides standard linear algebra operations.
Matrix Operations
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix multiplication
C = A @ B # or np.dot(A, B)
# Element-wise
C = A * B
# Transpose
A.T
Linear Algebra Functions
import numpy as np
A = np.array([[1, 2], [3, 4]])
# Determinant
np.linalg.det(A)
# Inverse
np.linalg.inv(A)
# Eigenvalues and vectors
eigenvalues, eigenvectors = np.linalg.eig(A)
# Solve Ax = b
b = np.array([1, 2])
x = np.linalg.solve(A, b)
Decompositions
# SVD
U, s, Vt = np.linalg.svd(A)
# QR decomposition
Q, R = np.linalg.qr(A)
# Cholesky (for SPD matrices)
L = np.linalg.cholesky(A @ A.T + 1e-6 * np.eye(2))
Practice Problems
- Solve system of linear equations
- Compute matrix inverse
- Find eigenvalues and eigenvectors
- Perform SVD on image
- Implement least squares regression