NumPy - Linear Algebra
NumPy - Linear Algebra (numpy.linalg)
Section titled “NumPy - Linear Algebra (numpy.linalg)”Linear algebra is a fundamental branch of mathematics crucial for many scientific and engineering domains, including machine learning, physics simulations, data analysis, and computer graphics. NumPy provides a dedicated module, numpy.linalg, offering a comprehensive set of functions for performing common linear algebra operations.
While scipy.linalg often provides more advanced and potentially more robust implementations (especially for edge cases or specific matrix types), numpy.linalg covers the essential routines needed for many standard tasks.
Here are some of the key functions available in numpy.linalg:
| Function | Description |
|---|---|
numpy.dot(a, b) | Computes the dot product of two arrays. For 2-D arrays, it’s equivalent to matrix multiplication. For 1-D arrays, it’s the inner product. |
numpy.linalg.multi_dot([...]) | Efficiently computes the dot product of two or more arrays in a single function call. |
numpy.vdot(a, b) | Computes the dot product of two vectors (flattened arrays), handling complex conjugation correctly. |
numpy.inner(a, b) | Computes the inner product of two arrays. For vectors, it’s similar to dot. For higher dimensions, sums products over the last axis. |
numpy.outer(a, b) | Computes the outer product of two vectors. |
numpy.matmul(a, b) or a @ b | Computes the matrix product of two arrays. This is the preferred function/operator for matrix multiplication (since Python 3.5). |
numpy.linalg.det(a) | Computes the determinant of a square matrix. |
numpy.linalg.slogdet(a) | Computes the sign and logarithm of the determinant of an array. |
numpy.linalg.solve(a, b) | Solves a system of linear scalar equations a * x = b for x, where a is a square matrix. |
numpy.linalg.inv(a) | Computes the multiplicative inverse of a square matrix. |
numpy.linalg.pinv(a) | Computes the Moore-Penrose pseudo-inverse of a matrix (useful for non-square or singular matrices). |
numpy.linalg.eig(a) | Computes the eigenvalues and right eigenvectors of a square matrix. |
numpy.linalg.svd(a) | Computes the Singular Value Decomposition (SVD) of a matrix. |
numpy.linalg.qr(a) | Computes the QR decomposition of a matrix. |
numpy.linalg.norm(a) | Computes the matrix or vector norm. |
Example: Matrix Multiplication and Solving Linear Equations
Section titled “Example: Matrix Multiplication and Solving Linear Equations”import numpy as np
# Define two matricesA = np.array([[1, 2], [3, 4]])B = np.array([[5, 6], [7, 8]])
# Matrix multiplication using the @ operator (preferred)C = A @ Bprint(f"Matrix A:\n{A}")print(f"Matrix B:\n{B}")print(f"Matrix product A @ B:\n{C}")
# Alternative using numpy.matmul()C_matmul = np.matmul(A, B)print(f"Matrix product using np.matmul():\n{C_matmul}")
# Define a system of equations: Ax = b# 2x + 3y = 8# 4x + 1y = 6
a_eq = np.array([[2, 3], [4, 1]])b_eq = np.array([8, 6])
# Solve for x (which contains [x, y])x_sol = np.linalg.solve(a_eq, b_eq)print(f"\nSystem matrix a_eq:\n{a_eq}")print(f"System vector b_eq: {b_eq}")print(f"Solution x: {x_sol}")
# Verify the solutionprint(f"Verification (a_eq @ x_sol): {a_eq @ x_sol}")Output:
Matrix A:[[1 2] [3 4]]Matrix B:[[5 6] [7 8]]Matrix product A @ B:[[19 22] [43 50]]Matrix product using np.matmul():[[19 22] [43 50]]
System matrix a_eq:[[2 3] [4 1]]System vector b_eq: [8 6]Solution x: [1. 2.]Verification (a_eq @ x_sol): [8. 6.]Application: Linear algebra is essential in fields like machine learning (e.g., in algorithms like PCA, linear regression, neural networks), physics (solving systems of differential equations), computer graphics (transformations), and optimization problems.
Dive deeper into the capabilities within the numpy.linalg documentation.