Matrix Operations and Linear Transformations
π Matrix Operations: Transforming Data
A matrix is a 2D array of scalars. Beyond being a simple data structure, a matrix represents a Linear Transformation from an -dimensional space to an -dimensional space.
π’ Level 1: Core Operations
1. Matrix Multiplication ()
Matrix multiplication is not element-wise. Instead, the entry is the dot product of the -th row of and the -th column of :
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix Multiplication (Dot Product)
C = np.dot(A, B) # or A @ B
print(f"Matrix Product:\n{C}")
# Element-wise (Hadamard) Product
C_element = A * B
print(f"Element-wise Product:\n{C_element}")2. Transpose and Special Matrices
- Transpose (): Formed by swapping rows and columns ().
- Symmetric Matrix: A square matrix where .
- Identity Matrix (): A square matrix with 1s on the diagonal and 0s elsewhere. .
- Orthogonal Matrix: A square matrix where . Its columns are orthonormal.
π‘ Level 2: Rank and Invertibility
3. Matrix Rank
The rank of a matrix is the number of linearly independent rows or columns. It represents the dimension of the output space after the transformation.
- Full Rank: A matrix has full rank if its rank equals the smaller of its dimensions.
4. Determinant ()
The determinant is a scalar value that represents the βvolume scaling factorβ of the linear transformation.
- If , the transformation collapses the space into a lower dimension.
- If , the matrix is invertible.
5. Matrix Inverse ()
For a square, non-singular matrix, the inverse satisfies . It βundoesβ the transformation performed by .
# Calculating Determinant and Inverse
A = np.array([[1, 2], [3, 4]])
det_A = np.linalg.det(A)
inv_A = np.linalg.inv(A)
print(f"Determinant: {det_A}")
print(f"Inverse:\n{inv_A}")π΄ Level 3: Advanced Concepts
6. The Moore-Penrose Pseudoinverse ()
When a matrix is not square or is singular, we use the pseudoinverse for solving linear systems: ML Application: This is the mathematical foundation for finding the optimal weights in Ordinary Least Squares (OLS) linear regression.
7. Matrix Trace
The trace is the sum of the diagonal elements of a square matrix: . It is invariant under cyclic permutations and basis changes.