Phase 21 of 25 · Topic 21.5

Linear Algebra with numpy.linalg (Matrix Dot, SVD & Inverses)

1Concept

`numpy.linalg` provides optimized BLAS/LAPACK bindings: the `@` operator computes matrix dot products; `np.linalg.inv()` computes matrix inversion; `np.linalg.svd()` performs Singular Value Decomposition for PCA dimensionality reduction.

2Architecture Diagram

Matrix Multiplication:
[ Matrix A (2x3) ] @ [ Matrix B (3x2) ] ---> [ Matrix C (2x2) ]

3Code Example

Python 3.12
print("=== NumPy Linear Algebra Core ===")
print("Matrix Dot Product:     C = A @ B  (or np.matmul(A, B))")
print("Matrix Inversion:       inv = np.linalg.inv(A)")
print("Eigenvalue Solve:       eigenvalues, eigenvectors = np.linalg.eig(A)")
print("Singular Value Decomp:  U, S, Vt = np.linalg.svd(A)")

4Expected Output

=== NumPy Linear Algebra Core ===
Matrix Dot Product:     C = A @ B  (or np.matmul(A, B))
Matrix Inversion:       inv = np.linalg.inv(A)
Eigenvalue Solve:       eigenvalues, eigenvectors = np.linalg.eig(A)
Singular Value Decomp:  U, S, Vt = np.linalg.svd(A)

5Key Takeaways

  • Use the `@` operator for matrix multiplication; `*` performs element-wise multiplication.
  • For linear systems `Ax = b`, use `np.linalg.solve(A, b)` instead of computing `inv(A) @ b` (more numerically stable).
  • NumPy links to high-performance OpenBLAS, MKL, or Apple Accelerate libraries.