Linear Algebra for Machine Learning
Linear Algebra Ideas Behind Machine Learning
Machine-learning systems need information in forms they can calculate with. Linear algebra provides a useful way to represent that information, compare different pieces of it, and simplify it without discarding all its important structure.
Three connected ideas help explain how this works:
- Data representations turn information into numbers arranged as vectors or matrices.
- Vector embeddings replace large, simple representations with smaller vectors that can capture meaningful relationships.
- Dimensionality reduction identifies a smaller space that contains much of the structure in the data.
Representing Data with Vectors
A vector is an ordered list of numbers, such as [1,4,2]. It can also be pictured as an arrow with a length and direction. In machine learning, a vector is useful because its entries can stand for measurable properties of an object.
For example, a patient could be represented by a vector containing height, weight, and age. Each entry is a feature: a property that describes the object being studied. A vector whose entries represent features is called a feature vector. The collection of possible feature vectors forms a feature space, which is another name for a vector space in this setting.
The number of entries in a vector determines its dimension. A vector with three entries belongs to a three-dimensional space; one with seven entries belongs to a seven-dimensional space. Each dimension corresponds to one place where a value can be recorded.
Turning different kinds of information into numbers
Different types of data can be organized into vectors in different ways.
- Images: A black-and-white pixel can be assigned a value such as 0 or 1. A grayscale pixel can be assigned a value from 0 to 255. An image is naturally arranged as a matrix of pixel values. To turn it into a vector, the rows can be laid end to end in a fixed order. This process is often called flattening the image.
- Documents and words: A word can be represented by counting how many times it appears in each document. For instance, the vector for “dog” might have one entry per article, with each entry recording the number of occurrences in that article.
- User preferences: A user can be represented by listing their interactions with items, such as movies. Entries might record whether a movie was watched or a rating the user gave it.
In each case, the representation gives a computer a numerical object to work with. The choice of representation matters, though: different choices preserve different kinds of information.
One-hot encodings
When a category has no natural numerical value, one option is a one-hot encoding. Each category receives a vector containing zeros everywhere except for a single 1.
For a vocabulary containing “apple,” “cat,” “house,” and “tiger,” the corresponding vectors could be:
- apple: [1,0,0,0]
- cat: [0,1,0,0]
- house: [0,0,1,0]
- tiger: [0,0,0,1]
The position of the 1 identifies the category. In mathematics, these vectors are examples of standard basis vectors.
One-hot encoding is straightforward, but it has two important drawbacks. First, it can be sparse, meaning that most entries are zero. A vocabulary with many thousands of words produces vectors with thousands of entries, almost all of them zero. User–item data can be sparse too: if there are many users and many movies, most users will have interacted with only a small fraction of the movies.
Second, one-hot vectors do not naturally express similarity between categories. The vectors for “cat” and “tiger” are no more similar, by this representation, than the vectors for “cat” and “apple.” The encoding marks categories as different but does not capture their relationships.
A numerical representation does more than make data computable: it determines which relationships the representation can express.
The dot product as a comparison
The dot product takes two vectors of the same length and returns a number. For vectors a=[a1,…,an] and b=[b1,…,bn], it is
a⋅b=i=1∑naibi.In words, corresponding entries are multiplied, and the products are added together. For example, the dot product of [1,2,3] and [4,5,6] is
1⋅4+2⋅5+3⋅6.Geometrically, the dot product is related to how much one vector points in the direction of the other. It can be understood through a “shadow” idea: the dot product is proportional to the length of one vector’s projection, or shadow, onto the direction of the other. When the second vector has length 1, the dot product itself gives the signed projection length.
This makes the dot product useful as a measure of alignment between vectors. However, it also reveals the limitation of one-hot encodings. Any two different one-hot vectors have dot product 0. So the encoding gives the same comparison for “cat” and “tiger” as it does for “cat” and “apple,” even if the first pair seems more related. The issue is not that the dot product is faulty; the representation does not contain the relationship one would like the comparison to reveal.
Think About It: If a representation gives every distinct pair of categories the same dot product, what kinds of relationships will it fail to show?
Vector Embeddings: Smaller, More Meaningful Representations
A vector embedding represents an object with a vector in a smaller-dimensional space. The goal is not merely to shorten a list of numbers. A useful embedding should also preserve or reveal meaningful relationships in the original data.
For example, an object that originally has a vector with thousands of entries might be represented by a vector with only a few entries. The smaller vector can be easier to work with, and its position in the new space can reflect patterns in the data. The new space is sometimes called a latent space because its dimensions can correspond to features that are not directly labeled but are inferred from patterns in the data.
A helpful image is a two-dimensional surface situated within three-dimensional space. The surface has fewer dimensions than the surrounding space, but it still has its own structure. In machine learning, an embedding similarly places data in a smaller space that can capture important aspects of the original information.
Matrices as transformations
A matrix is a rectangular array of numbers. Besides storing data, a matrix can represent a transformation: a rule that takes an input vector and produces an output vector. Depending on the matrix, the transformation can stretch, shrink, rotate, or otherwise change vectors.
The transformation applies to the whole space, not just one vector. If a matrix sends one vector to a new position, it also sends the surrounding vectors according to the same rule. This is why matrices are powerful tools for describing how representations change.
Matrix multiplication combines transformations. The number of columns in the first matrix must match the number of rows in the second matrix so that the multiplication is defined. When the product is defined, the resulting matrix represents the combined transformation.
Matrix factorization and SVD
Matrix factorization reverses the idea of multiplication. Instead of multiplying matrices to create a larger matrix, it starts with a matrix and seeks smaller or more structured matrices whose product reproduces it.
The central example is the singular value decomposition, usually abbreviated SVD. It expresses a matrix A as a product of three special matrices:
A=UΣV⊤.Here, Σ is a diagonal matrix, and the rows or columns of U and V have especially regular geometric properties: they are unit vectors and are mutually perpendicular within their respective collections. A collection with these properties is called orthonormal. The symbol V⊤ means the transpose of V, formed by exchanging rows and columns.
The SVD matters because its factors reveal structure in the original matrix. In applications involving embeddings, rows or columns of the factor matrices can serve as candidate lower-dimensional vectors for the original data. Instead of using a long, sparse vector directly, a system can use a shorter vector from the factorization.
SVD is not limited to machine learning. It is a fundamental linear-algebra technique used in areas including statistics, physics, genomics, and robotics.
Example: users and movies
Suppose a matrix has one row for each user and one column for each movie. A cell can contain a 1 if the user watched the movie and a 0 if the user did not. A rating can be stored instead, where a rating is available.
Each row then gives a basic representation of a user, and each column gives a basic representation of a movie. For instance, a user’s row records which movies they watched, while a movie’s column records which users watched it. These are useful representations, but they may be sparse and may not directly reveal deeper patterns.
The data might contain hidden features. Some users may tend to watch movies aimed at children, while others prefer movies aimed at adults. Some movies may be blockbusters, while others may be more specialized. These are examples of latent features: patterns present in the data even if no feature label is explicitly recorded in the matrix.
A factorization can provide shorter vectors for users and movies in a shared latent space. In a simple two-dimensional example, one dimension might reflect an age-related preference, while another might reflect a preference for blockbuster or more specialized films. The actual dimensions need not be assigned those labels in advance; the aim is for the patterns in the data to place related users and movies near one another.
If a child-oriented user and a child-oriented movie receive nearby vectors, their dot product may indicate strong alignment. This makes the embedding more useful than a raw list of watched movies: it can express a relationship between a user and a movie even when the user has not watched that particular movie.
Neural networks as another route to embeddings
A neural network can also produce an embedding. The original data vector is supplied as input, and the network outputs a new vector that can be used as the embedding. Under the hood, neural networks contain matrices, often many of them, as well as additional operations beyond basic linear algebra. They therefore connect the study of embeddings to deep learning.
Matrix factorization and neural networks are different methods, but they share a broad goal: transform high-dimensional data into a smaller representation that is more useful for revealing structure. Compression always risks losing information. A good embedding aims to reduce the number of dimensions while retaining the information that matters for the task.
Think About It: In a user–movie system, why might a short vector that captures shared preferences be more useful than a long list of every movie a user watched?
Dimensionality Reduction and Principal Components
Dimensionality reduction means representing data in fewer dimensions while retaining as much relevant structure as possible. This can make analysis more efficient, save computational resources, and make patterns easier to identify.
A dataset may be recorded in a high-dimensional space even though its meaningful variation lies mostly in a much smaller space. For example, many points in a two-dimensional plot may cluster along a line. If the main structure is along that line, analyzing both dimensions equally may be unnecessary. Finding the line can focus attention on the most important direction.
Eigenvectors help identify such directions.
Eigenvectors and eigenvalues
A matrix transformation usually changes a vector’s direction as well as its length. Some special vectors, however, keep their direction under the transformation and are only scaled. These are called eigenvectors.
If a matrix M transforms a vector v into a scalar multiple of itself, then
Mv=λv,where v is an eigenvector and λ is its eigenvalue. The eigenvalue tells how much the transformation scales that eigenvector. For example, if λ=2, the vector’s length doubles while its direction stays the same. A negative eigenvalue also reverses the direction as it scales the vector.
Eigenvectors matter in data analysis because they can reveal directions that are especially important to a dataset. Their role in dimensionality reduction becomes clear when many data points are arranged as a matrix.
Principal component analysis
A set of data points can be organized into a matrix, with each point represented as a vector. If the data matrix is rectangular, it cannot itself be used for an ordinary eigenvector calculation, which requires a square matrix. A square matrix can instead be formed from the data matrix and its transpose. The exact product depends on whether points are arranged in rows or columns.
In a standard principal-component analysis procedure, the data are first centered by subtracting the mean of each feature. A covariance-like matrix is then formed from the centered data. Its eigenvectors describe important directions in the data, and its eigenvalues indicate how strongly the data vary along those directions.
The eigenvector associated with the largest eigenvalue points along the principal direction: the direction in which the data show the greatest variation. If the points cluster mainly along a line, this eigenvector identifies the direction of that line. Further eigenvectors can identify additional directions that account for remaining variation.
Principal component analysis, or PCA, uses these directions to describe data with fewer dimensions. Keeping the leading principal directions gives a lower-dimensional representation that preserves much of the data’s structure. Discarding other directions simplifies the representation, but may lose some information. This is the same general tradeoff found in embeddings: fewer dimensions can improve efficiency, but compression must be chosen carefully.
Think About It: If a dataset varies strongly in one direction but very little in another, which direction is likely to be more valuable to keep in a reduced representation?
Comprehensive Review
Linear algebra gives machine learning a language for representing and transforming information. A vector can store an object’s numerical features; a matrix can store collections of data or describe a transformation. Images can be converted into pixel vectors, words can be represented by document counts or one-hot encodings, and user preferences can be stored in user–item matrices.
The representation determines what comparisons are possible. The dot product measures alignment between vectors, but simple one-hot encodings assign a dot product of zero to every pair of distinct categories. They therefore do not express degrees of semantic similarity.
Embeddings address this limitation by assigning data shorter vectors in a shared, lower-dimensional space. Matrix factorization, especially SVD, can provide candidate vectors through the factors of a matrix. Neural networks can also transform input vectors into embeddings. In either case, the goal is to retain useful relationships while reducing the representation’s size.
Dimensionality reduction searches for a smaller space that captures important structure in the data. Eigenvectors identify directions preserved by a matrix transformation up to scaling, and their eigenvalues describe the amount of scaling. In PCA, the leading eigenvectors of a covariance-like matrix identify the directions along which data vary most. Keeping those directions can make analysis more efficient while preserving much of the information that matters.
Together, these ideas show how linear algebra turns raw information into structured representations, reveals relationships among data, and helps machine-learning systems focus on the dimensions that carry the most useful structure.

Ask anything
We've read this whole study set, so questions about it get the best answers.