Matrix Operations - Multiplication, Transpose, and Inverse
Matrix Operations: Multiplication, Transpose, and Inverse
Section titled “Matrix Operations: Multiplication, Transpose, and Inverse”Matrices are at the heart of machine learning. Operations on matrices power linear regression, neural networks, dimensionality reduction, and much more. In this lesson, we’ll cover three fundamental operations: multiplication, transpose, and inverse.
Matrix Multiplication
Section titled “Matrix Multiplication”Given two matrices and , their product is defined as:
- The number of columns of A must equal the number of rows of B.
- The result has shape .
::: info Explanation of Multiplication Matrix multiplication represents combining linear transformations.
- In ML: multiplying dataset matrix with parameter vector computes predictions.
:::
Mini Example:
Then:
Matrix Transpose
Section titled “Matrix Transpose”The transpose of a matrix flips rows and columns:
Example:
::: info Explanation of Transpose Transpose is essential in ML when switching between “row as samples” vs “column as features”.
- In regression: appears in the normal equation.
:::
Matrix Inverse
Section titled “Matrix Inverse”The inverse of a square matrix (if it exists) is such that:
where is the identity matrix.
- Not all matrices are invertible (singular matrices don’t have inverses).
- In ML: matrix inverses appear in the normal equation for linear regression:
Mini Example:
::: info Explanation of Inverse Matrix inverse is like division for matrices.
- But inversion is computationally expensive.
- In ML practice, we often avoid explicit inversion, using more stable methods (like QR or gradient descent).
:::
Hands-on with Python and Rust
Section titled “Hands-on with Python and Rust”::: code-group
import numpy as np
# Define matricesA = np.array([[1, 2], [3, 4]])B = np.array([[5], [6]])
# MultiplicationC = A.dot(B)
# TransposeAT = A.T
# InverseA_inv = np.linalg.inv(np.array([[4, 7], [2, 6]]))
print("A * B =\n", C)print("Transpose of A =\n", AT)print("Inverse of [[4,7],[2,6]] =\n", A_inv)use ndarray::{array, Array2};use ndarray_linalg::Inverse;
fn main() { // Define matrices let a: Array2<f64> = array![[1.0, 2.0], [3.0, 4.0]]; let b: Array2<f64> = array![[5.0], [6.0]];
// Multiplication let c = a.dot(&b);
// Transpose let at = a.t().to_owned();
// Inverse let a2: Array2<f64> = array![[4.0, 7.0], [2.0, 6.0]]; let a_inv = a2.inv().unwrap();
println!("A * B =\n{:?}", c); println!("Transpose of A =\n{:?}", at); println!("Inverse of [[4,7],[2,6]] =\n{:?}", a_inv);}:::
Connection to ML
Section titled “Connection to ML”- Multiplication → computing predictions, combining transformations.
- Transpose → appears in regression, covariance, gradient formulas.
- Inverse → theoretical solution in linear regression (though avoided in practice).
Next Steps
Section titled “Next Steps”Continue to Special Matrices: Identity, Diagonal, Orthogonal.