
I used to think linear algebra was just another math class I had to get through. Then I started learning machine learning and realized it’s basically everywhere. All those matrix operations and vector spaces? They’re what actually run the algorithms behind modern AI.
Everything Is Vectors and Matrices
Machine learning is really just doing math on data. That data gets represented as vectors and matrices. Training a model? You’re doing a bunch of linear transformations on these mathematical objects.
Vector Spaces and Transformations
Say you have a dataset with $n$ features. Each data point lives in an $n$ dimensional vector space $\mathbb{R}^n$. When you apply a linear transformation, you multiply by a matrix $A$:
$$f(x) = Ax$$
This simple operation is what happens in every layer of a neural network. Just add a nonlinear activation function on top and you’ve got deep learning.
1 | import numpy as np |
Matrix Operations in Action
Here’s how matrix operations show up in some fundamental ML techniques.
Principal Component Analysis
PCA uses eigendecomposition to find the directions where your data varies the most. You compute the covariance matrix:
$$C = \frac{1}{n}X^TX$$
Then solve the eigenvalue equation:
$$Cv = \lambda v$$
1 | def compute_pca(X: np.ndarray, n_components: int) -> tuple[np.ndarray, np.ndarray]: |
Neural Networks
Deep learning networks are chains of linear transformations with nonlinear functions in between. Each layer does:
$$h = \sigma(Wx + b)$$
where $\sigma$ is the activation function, $W$ is the weight matrix, and $b$ is the bias vector.
1 | def neural_network_layer( |
Training
The training process also relies on linear algebra. Gradient descent computes partial derivatives with respect to matrices:
$$W_{t+1} = W_t - \alpha \frac{\partial L}{\partial W_t}$$
where $L$ is the loss function and $\alpha$ is the learning rate.
SVD and Other Stuff
Singular Value Decomposition is important for things like recommendation systems. SVD factorizes a matrix $A$ as:
$$A = U\Sigma V^T$$
where $U$ and $V$ are orthogonal matrices, and $\Sigma$ contains the singular values.
1 | def truncated_svd(X: np.ndarray, k: int) -> tuple[np.ndarray, np.ndarray, np.ndarray]: |
Wrapping Up
Linear algebra lets you express complicated transformations with simple matrix operations. I’ve been working with ML for a while now and I still find it pretty cool how these mathematical foundations make everything work. When you call model.fit()
or torch.matmul()
, there’s a lot of beautiful math happening under the hood.