Linear Algebra for Machine Learning


Linear Algebra for Machine Learning

Simon
Welcome to our introductory tour of linear algebra in machine learning. We’re going to focus on three connected ideas: how data gets represented, how vector embeddings can make those representations more useful, and how dimensionality reduction helps reveal structure. This is a bird’s-eye view, not a technical derivation, but we’ll make the linear algebra behind each idea concrete.

Hope
And the starting question is practical: how can a computer work with things like pictures, language, or people’s preferences? Computers work with numbers, so a common strategy is to organize information into vectors—one-dimensional arrays of numbers.

Simon
A vector can be pictured as an array, or geometrically as an arrow with a length and a direction. All vectors with a fixed number of entries form a vector space: for instance, vectors with three entries belong to a three-dimensional vector space. In machine learning, a feature vector is simply a vector whose entries describe features of something we care about.

Hope
Say we’re describing a patient using height, weight, and age. Those three measurements can go into a feature vector, with one entry for each feature. The collection of such vectors can be called a feature space. The important point is that the entries have an interpretation—they’re not just numbers for their own sake.

Simon
Images offer a natural example. In a black-and-white image, each pixel could be represented as 0 or 1; in a grayscale image, each pixel could be represented by a number such as one between 0 and 255. That gives you a matrix of pixel values. To turn it into a vector, you can flatten the matrix—for example, read across the first row, then the next, and continue.

Hope
Text can be represented numerically in a different way. Suppose you have a collection of articles and want to represent the word “dog.” Count how many times it appears in each article, then put those counts into a vector. An article with no occurrences contributes a zero; one with seven occurrences contributes a seven.

Simon
You can also represent user preferences or interactions. Imagine a list of movies, with an entry for each movie showing whether a user watched it, or what rating the user gave it—perhaps from zero to five. Once again, the information becomes a vector that a machine-learning method can work with.

Hope
But what if the data is a word itself, rather than a count of that word in documents? One simple option is a one-hot encoding. For a tiny vocabulary—apple, cat, house, tiger—you assign each word a vector with one 1 and zeros everywhere else. Apple might be 1, 0, 0, 0; cat might be 0, 1, 0, 0, and so on.

Simon
In mathematics, those one-hot vectors are standard basis vectors. They’re easy to make, but they illustrate a drawback of some basic data representations: sparsity. Most of their entries are zero. And large user–movie data sets can also be sparse, because not every user has watched or rated every movie; unknown entries may be represented as zeros.

Hope
There’s another possible drawback: a representation may fail to express meaningful relationships. Intuitively, cat and tiger seem more related than cat and apple. But with one-hot encodings, those pairs all have the same kind of separation: their vectors have no shared 1.

Simon
A useful way to see that is with the dot product. Given two vectors of the same length, multiply their corresponding entries and add the results. So for three-entry vectors, you multiply first entry by first entry, second by second, third by third, and then add those three products. The result is a number, not another vector.

Hope
That number can serve as an indicator of similarity. Geometrically, you can picture one vector casting a shadow in the direction of the other. When the second vector has length one, the dot product gives the length of that shadow; in general, it is proportional to it. The details depend on the vectors, but the picture helps explain why dot products are useful for comparing them.

Simon
Now apply that to one-hot vectors. The dot product of apple and cat is zero, which might feel reasonable. But the dot product of tiger and cat is also zero, even though we might want a representation that reflects their relationship. That limitation motivates looking for better vector representations—not just any convenient list of numbers.

Hope
That brings us to vector embeddings. An embedding replaces an original vector with another vector in a smaller-dimensional space. The hope is that the new vector is more manageable and captures useful meaning or relationships in the data. You might picture a lower-dimensional surface sitting inside a larger space, although real machine-learning examples can involve far more dimensions than a simple drawing.

Simon
One route to embeddings uses matrix factorization, so let’s first clarify what a matrix does. A matrix is a two-dimensional array of numbers, and it can also represent a transformation: it takes an input vector and produces an output vector. That transformation might stretch, rotate, scale, or otherwise change vectors. It acts on the whole space, not just on one chosen vector.

Hope
Matrix multiplication lets you combine those transformations: multiply two compatible matrices and you get another matrix. Compatibility means, in particular, that the first matrix’s number of columns matches the second matrix’s number of rows. Factorization goes in the reverse direction: given a matrix, ask whether it can be expressed as a product of smaller or otherwise useful matrices.

Simon
A central example is the singular value decomposition, or SVD. It says that any matrix can be written as a product of three special matrices. One is diagonal, and the other two have orthonormal rows or columns: vectors of length one that are mutually perpendicular, meaning their pairwise dot products are zero.

Hope
You don’t need the full formula to grasp why that matters here. The rows or columns of the matrices in a factorization can serve as candidates for embeddings. In other words, a factorization can turn a large, direct representation into smaller vectors that may capture patterns in the original data.

Simon
Let’s use the user–movie example. Put users in the rows and movies in the columns of a matrix. A check mark—or a numerical 1—can indicate that a user watched a movie, while an empty cell indicates no recorded interaction. A row gives a direct representation of a user, and a column gives a direct representation of a movie.

Hope
Those direct representations tell us which users watched which movies, but they don’t necessarily reveal why the patterns occur. Maybe some users are children and others adults; perhaps some movies are blockbusters and others are arthouse films. Those underlying characteristics are called latent features because they’re hidden in the interaction data rather than explicitly listed.

Simon
The goal is to find a smaller space that captures those features. To keep the picture simple, imagine a two-dimensional latent space: perhaps one direction corresponds roughly to audience age and another to blockbuster versus arthouse. If users and movies can be placed meaningfully in that space, their positions can help describe relationships that weren’t obvious in the original large matrix.

Hope
A factorization can provide those lower-dimensional vectors. For example, instead of representing a user by a long row containing an entry for every movie, a factorization might give that user a vector with only two entries. It can likewise give a movie a compact vector. The rows or columns of the resulting matrices are the candidate embeddings.

Simon
In the illustrative example described in the talk, user number three’s original representation is 1, 1, 1, 0, 0, while the embedding shown for that user is 0.2, negative 1. The original representation for Shrek is 1, 0, 1, 0, while its embedding is 1, negative 1. Instead of five or four entries, each candidate embedding has two.

Hope
Plot those two-dimensional embeddings and the user and Shrek appear close together. If that user represents a young child, that closeness may make sense because Shrek appeals to a younger audience. Their dot product offers a numerical way to discuss the relationship, just as the geometric picture of a shadow helps us reason about similarity.

Simon
The example also illustrates an important caution: compression loses information. The aim isn’t simply to make vectors shorter; it’s to reduce their dimension wisely, preserving useful structure and avoiding too much information loss. SVD is one approach to finding embeddings, and related matrix-factorization techniques are used for similar purposes.

Hope
Another route is a neural network. You feed the original data vector into the network, and its output can be used as a candidate embedding. Under the hood, neural networks contain matrices—often many of them—along with other elements that take us beyond basic linear algebra and into deep learning.

Simon
Both routes lead to the broader idea of dimensionality reduction: represent high-dimensional data in a smaller space without discarding too much that matters. That can make work more efficient, save resources, and reveal relevant structure. A key linear-algebra concept in this part of the story is the eigenvector.

Hope
Recall that a matrix represents a transformation. Some vectors have a special property under a transformation: they keep their direction and only get scaled, growing or shrinking. Those are eigenvectors. The amount of scaling is the eigenvalue. If a matrix sends a vector back to twice that vector, then the vector is an eigenvector and its eigenvalue is two.

Simon
Eigenvectors matter because they can tell us about the data, not just about the transformation. Imagine many data points in a space. Even if the space has many dimensions, the points might cluster near a line or another lower-dimensional region. If we can identify that region, we can focus on the dimensions where the data’s structure is concentrated.

Hope
Conceptually, you start by organizing the data points into a matrix: each point is a vector, and you stack the vectors together. To find directions of variation, one can examine eigenvectors of a square matrix formed from the data matrix and its transpose, rather than simply looking for eigenvectors of the original rectangular data matrix.

Simon
There are statistical details behind doing this well. For example, the data may need to be centered by subtracting the mean, and the matrix may be scaled. But at a high level, the eigenvector with the largest eigenvalue identifies the principal direction—the direction along which the data is most strongly clustered or varies most in this kind of analysis.

Hope
That leads to principal component analysis, or PCA. In the simple case where points cluster along a line, the principal direction tells you the direction of that line. More generally, the idea is to find important directions in the data and describe it using a smaller number of them.

Simon
So the big picture is that relevant data may occupy only a small part of a much larger ambient vector space. Linear algebra gives us tools to represent that data with vectors, compare vectors with dot products, build embeddings through matrix factorization or neural networks, and find lower-dimensional structure through eigenvectors and PCA.

Hope
And those are the reasons these ideas are so useful in machine learning: better representations can expose relationships, and dimensionality reduction can make analysis more efficient while highlighting relevant patterns. If you want to go further, the talk points learners toward resources such as Grant Sanderson’s eigenvectors-and-eigenvalues video, Gilbert Strang’s Introduction to Linear Algebra, and the paper The Extraordinary SVD. The introductory tour ends here, but there’s plenty more to explore.

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