0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:0950 features, one chart: the hook
Brightcart tracks fifty numbers about every customer. Visits, spend, categories bought, returns, reviews, delivery times, and many more. Fifty dimensions. Nobody can picture that. But here's a thought. Shine a light on a teapot from a bad angle, and its shadow is a shapeless blob. Find the right angle, and the shadow instantly says "teapot". A good shadow keeps most of the information, with fewer dimensions. Watch: these are nearly eighteen hundred handwritten digits, each made of sixty-four pixel values, squeezed down to a flat chart, and the different digits gather into their own regions.
How do we find the most informative shadow? That's dimensionality reduction, and its classic method is principal component analysis. Let's build it from scratch. Season One, Module Five, stop two: dimensionality reduction.
1:10Why reduce dimensions?
There are four good reasons to reduce dimensions. To see data, because we can only plot two or three axes. For speed, because fewer features train faster. To remove noise hiding in unimportant directions. And to fight the curse of dimensionality from episode thirteen. And there's usually room to do it, because features overlap. Look at six Brightcart customers: visits per month against monthly spend. They lie almost perfectly on a line, with a correlation of zero point nine nine three. Customers who visit more spend more.
These two numbers carry almost the same information. Could one number replace both?
1:54The best shadow: maximum variance
Let's find the best shadow by hand. First, centre the data: move the origin to the average customer. Now take a line through the centre and project every customer onto it, dropping a perpendicular, like a shadow. The shadows spread out along the line. Their variance tells us how much of the data's variety this direction captures. Rotate the line and watch the spread change. Along the long diagonal of the cloud, the shadows spread out the most: a variance of six point three nine. At right angles to that, they all bunch together: just zero point zero two.
That direction of maximum variance is the first principal component. The direction at right angles to it is the second. There's a second way to see it. Each point's squared distance from the centre is fixed, and it splits into two parts, by Pythagoras: the part along the line, and the part across it. So making the shadows spread out as much as possible is exactly the same as keeping the points as close to the line as possible.
3:03Eigenvectors and the covariance matrix
Deep dive: how PCA finds these directions without rotating a line by hand. First, compute the covariance matrix. Its diagonal holds each feature's variance, and the off-diagonal entries show how strongly the features move together. Here, a large positive three point one seven: when visits go up, spend goes up. Remember from episode two that a matrix transforms space, turning and stretching arrows. Apply the covariance matrix to arrows pointing in every direction. Almost all of them change direction. But a couple of special arrows don't turn at all; they're only stretched.
These are the eigenvectors, and the stretch factors are the eigenvalues. The eigenvectors of the covariance matrix are exactly the principal components, and each eigenvalue is the variance along its component. For Brightcart: the first principal component points along zero point seven three nine, zero point six seven four, with variance six point three nine. The second has variance zero point zero two. So the recipe is: centre the data, compute the covariance matrix, find its eigenvectors, sort them by eigenvalue, and project.
In practice, libraries use a closely related method called the singular value decomposition, which gives the same directions more reliably.
4:29Projection and reconstruction
Now, the compression. Each customer can be described by a single number: their position along the first principal component. For the first customer, that's their centred values, dotted with the component: minus three point three nine. A projection is just a dot product, again. Two columns become one. How much did we lose? Let's rebuild the customer from that one number: start at the average, and move minus three point three nine along the component. We land at one point nine nine seven, one point five zero three, almost exactly the original two and one point five.
The share of variance kept is the first eigenvalue over the total: ninety-nine point seven percent. One number holds nearly everything two numbers did. And this new number has a meaning. The component mixes visits and spend in similar proportions, so it acts like an "overall engagement" score. Principal components don't always have such a neat interpretation, but when they do, it's very useful.
5:38How many components to keep
With more features, we choose how many components to keep. Here are the handwritten digits again, sixty-four pixel features each. This scree plot shows the variance each component captures. The first explains about fifteen percent, the second about fourteen, and then the bars shrink into a long tail. Pause and predict: how many of the sixty-four components do we need to keep ninety-five percent of the variance?
Twenty-nine. Less than half the features, keeping ninety-five percent of the variance. Ninety percent takes just twenty-one. A common rule of thumb is to keep enough components for ninety to ninety-five percent of the variance. But the most reliable approach is to treat the number of components like any other setting, and choose it by validation on whatever task comes next.
6:36Standardise first, and PCA's limits
Two warnings. First, PCA chases variance, so features with large numbers dominate. Measure yearly spend in dollars and visits as a count, and the first component will point almost entirely along spend, just because its numbers are bigger. Standardise first, as in episode seven. And, like any preprocessing, fit it on the training data only. Second, PCA only finds straight directions. Data curled into a spiral can't be flattened by any straight projection. And the directions with the most variance aren't always the ones that matter for a prediction.
Sometimes the useful signal hides in a quiet, low-variance direction.
7:21t-SNE and UMAP
For visualisation, there are non-linear methods that often draw much clearer pictures. The best known are t-S-N-E and UMAP. Instead of preserving variance, they try to preserve neighbourhoods: points that are close in the original high-dimensional space should stay close on the map. t-S-N-E turns closeness into probabilities in both spaces and nudges the map until they match. On the handwritten digits, PCA's two-dimensional view is smudgy and overlapping; the t-S-N-E map shows ten crisp islands, one per digit. UMAP has a similar goal, is usually faster, and tends to keep more of the global layout.
But read these maps carefully. The size of a cluster on the map, and the distance between clusters, often don't mean anything. And different settings, or even a different random seed, can produce noticeably different pictures. They're superb for exploring data, and dangerous for drawing precise conclusions.
8:23Try it yourself: project a new customer
A new customer visits eight times a month and spends about seventy dollars. What's their engagement score on the first principal component? Remember to centre first, by subtracting the average customer. Pause and compute.
Centred, they're three point five visits and about three point two in spend above average. Dot that with the component, zero point seven three nine and zero point six seven four, and you get about two point five nine plus two point one seven: four point seven six. That's higher than any of our original six customers. A new customer, summarised in one meaningful number, using a direction learned from the data.
9:09PCA for denoising
PCA can also clean data. When measurements cluster around a few main directions, the small variation in the other directions is often just noise. Keep only the top components and reconstruct, and the points snap back towards the main structure, with the noisy wobble discarded. Rebuild handwritten digits from their top twenty-nine components, and they look slightly cleaner, as if gently smoothed. The caution: what PCA discards isn't always noise. Sometimes it's exactly the subtle signal a task needs, so validate the effect on whatever you do next.
9:48Eigen-images: what components look like
Here's a beautiful way to see what PCA finds. Each principal component of the handwritten digits is itself a list of sixty-four numbers, so we can reshape it into an eight-by-eight picture. They look like blurry stroke patterns. Any digit can then be approximately rebuilt as the average digit, plus a weighted mix of these component pictures. Watch a three being assembled: the average image, plus a bit of the first pattern, minus a bit of the second, and so on. In Season Two, neural networks will learn their own feature images, but those are learned to solve a task, not just to capture variance.
10:32PCA vs t-SNE vs UMAP: when to use what
So when should you use which? PCA is fast, linear and reproducible: great for compression, denoising, speeding up other models, and a first look at your data. t-S-N-E and UMAP are for exploring and visualising, not for measuring distances or as general inputs to models. If you need to keep original, interpretable columns, use feature selection, like the lasso. And in Season Three, we'll meet autoencoders, neural networks that learn non-linear compression, which you can think of as a powerful, curved cousin of PCA.
11:13Common PCA mistakes
Finally, the common mistakes. Forgetting to standardise features measured in different units. Fitting PCA on all the data, including the test set. Reading meaning into a component's sign: a component and its exact opposite describe the same direction, and software may flip the sign between runs. Choosing the number of components only by explained variance, instead of checking how well the next model performs. And applying PCA blindly to one-hot category columns, where its notion of variance may not mean much.
11:49Myth vs Fact
Myth: PCA selects the most important original features. Fact: it doesn't select anything. It creates new features, each a weighted blend of all the original ones. If you need to keep a subset of the original columns, that's feature selection, like the lasso from episode seven.
12:10Recap
Let's recap. Real features overlap, so we can often describe data with fewer numbers. PCA finds the directions of maximum variance: the eigenvectors of the covariance matrix, with eigenvalues measuring the variance along each one. Projecting onto the top components compresses the data; for the digits, sixty-four features down to twenty-nine kept ninety-five percent of the variance. Standardise first. And for pictures of complex data, t-S-N-E and UMAP draw clearer maps, but read them carefully. One sentence to remember: find the angle where the shadow keeps the most.
12:53Next: association rules
Mission complete. Dev can now look at Brightcart's fifty customer features on a single chart, and two closely related features have become one engagement score that keeps almost all of the information. Next, a different unsupervised question that every shop asks: which products are bought together? Bread and butter? Cereal and milk? We'll find rules hidden in shopping baskets, and learn three numbers that tell us whether a rule is real: support, confidence and lift. See you on the map.