Linear Algebra for Researchers
Part I linear algebra
- Linear Algebra
- AI Research
We now move on to learning about linear algebra.
This article is a condensed guide to the fundamentals of linear algebra. It is Part I of the series, and I will publish more articles on this topic.
In this guide, I will cover some of the mathematics and code behind algebraic operations, which form the backbone of modern Machine Learning, ML.
The table of contents for this guide is as follows:
- Scalars, Vectors, Matrices, and Tensors
- Matrix Operations
- Dot Product
- Norms and Distances
- Linear Transformations
- Eigenvalues and Eigenvectors
- Matrix Factorization
- Singular Value Decomposition, SVD
- Orthogonality
- Projections
- Rank
- Positive Definite Matrices
Scalars, Vectors, Matrices, and Tensors
A scalar is a single number.
A vector is an ordered list of numbers.
A matrix is a rectangular table of numbers.
A tensor is a generalization of scalars, vectors, and matrices to higher dimensions.
In machine learning:
- a scalar can represent a loss value,
- a vector can represent an embedding,
- a matrix can represent a batch of data,
- a tensor can represent image batches, activations, or model weights.
For example, a batch of RGB images can be represented as a 4D tensor:
import numpy as np
images = np.random.randn(32, 224, 224, 3)
print(images.shape)Here:
means:
32images,- height of
224, - width of
224, 3color channels.
Vector Operations
Vectors can be scaled, added, and subtracted.
Given:
Multiplying by a scalar c gives:
Vector addition is computed componentwise.
If:
then:
Geometrically, vector addition places one vector after another.
In Python:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
print(2 * a)
print(a - b)Matrix Operations
Matrices can be added, subtracted, multiplied by scalars, transposed, and multiplied with other matrices.
Let:
Matrix addition is elementwise:
Scalar multiplication is also elementwise:
The transpose of a matrix flips rows and columns.
In NumPy:
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A + B)
print(2 * A)
print(A.T)Matrix Multiplication
Matrix multiplication is different from elementwise multiplication.
If:
and:
then:
The inner dimensions must match.
For example:
In Python:
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)Matrix multiplication is one of the core operations in neural networks.
A linear layer can be written as:
where:
Xis the input,Wis the weight matrix,bis the bias,Yis the output.
Dot Product
The dot product takes two vectors and returns a scalar.
For two vectors:
the dot product is:
Example:
The dot product also relates to the angle between two vectors:
This formula is important because it tells us how aligned two vectors are.
- If the dot product is positive, the vectors point in similar directions.
- If the dot product is zero, the vectors are perpendicular.
- If the dot product is negative, the vectors point in opposite directions.
In Python:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
dot = np.dot(a, b)
print(dot)Norms and Distances
A norm measures the size or length of a vector.
The most common norm is the Euclidean norm, also called the L2 norm:
Example:
Another common norm is the L1 norm:
Distance between two vectors is usually computed by applying a norm to their difference:
For Euclidean distance:
Norms are used in machine learning for:
- measuring error,
- regularization,
- clustering,
- nearest-neighbor search,
- optimization.
In Python:
x = np.array([3, 4])
y = np.array([1, 2])
l2_norm = np.linalg.norm(x, ord=2)
l1_norm = np.linalg.norm(x, ord=1)
distance = np.linalg.norm(x - y)
print(l2_norm)
print(l1_norm)
print(distance)Linear Transformations
A linear transformation maps vectors to other vectors while preserving addition and scalar multiplication.
A function T is linear if:
and:
Matrices represent linear transformations.
For example:
This matrix stretches vectors by a factor of 2 in the x-direction.
Another example is rotation.
This rotates a vector by 90 degrees counterclockwise:
In machine learning, many layers are built from linear transformations:
This appears in dense layers, embeddings, attention projections, and many other architectures.
Eigenvalues and Eigenvectors
An eigenvector of a matrix is a vector whose direction does not change after the matrix transforms it.
For a square matrix A, an eigenvector v and eigenvalue λ satisfy:
Here:
Ais the matrix,vis the eigenvector,λis the eigenvalue.
The eigenvalue tells us how much the eigenvector is stretched or compressed.
Example:
For:
we get:
So v₁ is an eigenvector with eigenvalue 2.
For:
we get:
So v₂ is an eigenvector with eigenvalue 3.
In Python:
A = np.array([[2, 0], [0, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
print(eigenvectors)Eigenvalues and eigenvectors are used in:
- Principal Component Analysis, PCA,
- dimensionality reduction,
- graph algorithms,
- spectral clustering,
- stability analysis.
Matrix Factorization
Matrix factorization means decomposing a matrix into simpler matrices.
Instead of working directly with one large matrix, we write it as a product of smaller or more structured matrices.
A general form is:
where B and C are matrices whose product reconstructs A.
Matrix factorization is useful because it can reveal hidden structure in data.
In machine learning, matrix factorization appears in:
- recommendation systems,
- dimensionality reduction,
- compression,
- topic modeling,
- collaborative filtering.
For example, in recommendation systems, we may have a user-item matrix:
where:
Ris the user-item rating matrix,Urepresents users,Vrepresents items.
The model learns hidden features that explain user preferences and item properties.
Singular Value Decomposition, SVD
Singular Value Decomposition, or SVD, is one of the most important matrix factorizations.
For a matrix A, SVD decomposes it as:
where:
Ucontains the left singular vectors,Σcontains the singular values,V^Tcontains the right singular vectors.
The singular values tell us how important each direction is.
If some singular values are very small, we can approximate the original matrix using fewer dimensions.
This gives a low-rank approximation:
where k is smaller than the original dimension.
This is useful for:
- compression,
- noise reduction,
- dimensionality reduction,
- latent semantic analysis,
- recommendation systems.
In Python:
A = np.array([[1, 2], [3, 4], [5, 6]])
U, S, Vt = np.linalg.svd(A)
print(U)
print(S)
print(Vt)The values in S are the singular values.
Orthogonality
Two vectors are orthogonal if their dot product is zero.
This means the vectors are perpendicular.
Example:
Their dot product is:
So the vectors are orthogonal.
Orthogonality is useful because orthogonal directions do not overlap in the information they represent.
In machine learning and statistics, orthogonality appears in:
- PCA,
- feature decorrelation,
- QR decomposition,
- numerical optimization,
- basis vectors.
A matrix Q is orthogonal if:
This means the columns of Q are orthonormal.
Projections
A projection maps a vector onto another vector or subspace.
Suppose we want to project vector a onto vector b.
The projection is:
This tells us how much of a lies in the direction of b.
Example:
Then:
and:
So:
This means the projection of (3, 4) onto the x-axis is (3, 0).
In Python:
a = np.array([3, 4])
b = np.array([1, 0])
projection = (np.dot(a, b) / np.dot(b, b)) * b
print(projection)Projections are used in:
- least squares,
- regression,
- PCA,
- dimensionality reduction,
- vector search.
Rank
The rank of a matrix tells us how many independent directions are contained in the matrix.
A matrix has full rank if none of its rows or columns are redundant.
Example:
The second row is just twice the first row.
So this matrix does not contain two independent rows. Its rank is 1.
Another example:
Both rows are independent, so the rank is 2.
In Python:
A = np.array([[1, 2], [2, 4]])
B = np.array([[1, 0], [0, 1]])
print(np.linalg.matrix_rank(A))
print(np.linalg.matrix_rank(B))Rank is important because it tells us whether a system has redundant information.
In machine learning, rank is connected to:
- feature independence,
- low-rank approximation,
- dimensionality reduction,
- model compression,
- matrix factorization.
Positive Definite Matrices
A symmetric matrix A is positive definite if:
for every nonzero vector x.
Positive definite matrices are important because they behave nicely in optimization.
For example:
For any nonzero vector:
we get:
This is always positive unless x is the zero vector.
Therefore, A is positive definite.
In Python, one way to check positive definiteness is to look at the eigenvalues.
A = np.array([[2, 0], [0, 3]])
eigenvalues = np.linalg.eigvals(A)
print(eigenvalues)
print(np.all(eigenvalues > 0))For a symmetric matrix, if all eigenvalues are positive, the matrix is positive definite.
Positive definite matrices appear in:
- covariance matrices,
- optimization,
- second-order methods,
- Gaussian distributions,
- kernel methods.
Why Linear Algebra Matters in Machine Learning
Linear algebra is everywhere in machine learning.
A dataset is often represented as a matrix:
where:
nis the number of examples,dis the number of features.
A neural network layer is often written as:
Attention uses matrix multiplication:
Embeddings are vectors.
Images are tensors.
Optimization uses gradients.
Dimensionality reduction uses eigenvectors and singular values.
So, when we study linear algebra, we are not just learning abstract mathematics. We are learning the language used to describe modern AI systems.
Summary
In this article, we covered the basic building blocks of linear algebra:
- scalars,
- vectors,
- matrices,
- tensors,
- matrix operations,
- dot products,
- norms,
- distances,
- linear transformations,
- eigenvalues and eigenvectors,
- matrix factorization,
- SVD,
- orthogonality,
- projections,
- rank,
- positive definite matrices.
These concepts form the foundation for many machine learning algorithms.
In Part II, we will go deeper into how these ideas appear directly in machine learning models, including linear regression, PCA, embeddings, and neural networks.
References for Further Study
- Gilbert Strang, Introduction to Linear Algebra
- David C. Lay, Linear Algebra and Its Applications
- Goodfellow, Bengio, and Courville, Deep Learning
- Mathematics for Machine Learning by Deisenroth, Faisal, and Ong