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
Given two matrices
- The number of columns of A must equal the number of rows of B.
- The result has shape
.
Explanation of Multiplication
Matrix multiplication represents combining linear transformations.
- In ML: multiplying dataset matrix
with parameter vector computes predictions.
Mini Example:
Then:
Matrix Transpose
The transpose of a matrix
Example:
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
The inverse of a square matrix
where
- 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:
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
import numpy as np
# Define matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5], [6]])
# Multiplication
C = A.dot(B)
# Transpose
AT = A.T
# Inverse
A_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
- Multiplication → computing predictions, combining transformations.
- Transpose → appears in regression, covariance, gradient formulas.
- Inverse → theoretical solution in linear regression (though avoided in practice).
Next Steps
Continue to Special Matrices: Identity, Diagonal, Orthogonal.