0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09No labels: can data find its own groups?
Until now, every problem came with answers. Customers labelled "stayed" or "left". Reviews labelled "spam" or "real". But often there are no labels at all. Meera wants to send different campaigns to different kinds of customers. The trouble is, nobody has ever defined what those "kinds" are. All she has is how much each customer spends in a year, and how often they visit. Can the data itself reveal natural groups? That's clustering, the first big tool of unsupervised learning. Today we'll learn three very different ways to find groups, and how to tell whether the groups we found are real.
Season One, Module Five: unsupervised learning. First stop: clustering.
0:57What makes a cluster?
A cluster is a group of points that are more similar to each other than to the points in other groups. And, as in k nearest neighbours, "similar" usually means "close together". But "group" can mean different things. Some groups are round blobs. Some sit inside bigger groups, like neighbourhoods inside cities. And some are long, curved shapes, with a few stragglers that belong nowhere. Each of today's three algorithms is built around one of these pictures.
1:30k-means: the algorithm
The most famous clustering algorithm is k-means. It needs one decision from us: k, the number of clusters. Let's say three. It starts by placing three centre points, called centroids, here marked as stars. Then it repeats two simple steps. Step one, assign: every customer joins the nearest star. You can see the space split into three territories. The blue star gets the four customers at the bottom left, the gold star gets two on the right, and the pink star grabs everyone else, including two customers on the right who don't really belong with the top group.
Step two, update: each star moves to the average position of the customers it was given. Pink gets dragged towards the middle, because it's trying to represent two different groups at once. Now repeat. Assign again: those two customers on the right are now closer to gold, so they switch. Update again: gold moves to nine, three, and pink rises to the top group, at five point five, eight point seven five. Assign once more, and nobody switches. The algorithm has converged. Three clean groups. What is k-means actually doing?
It's minimising a cost called inertia: the total squared distance from each customer to their own centroid. Both steps can only lower it or leave it unchanged. Assigning each point to its nearest centroid can't increase it, and moving a centroid to its group's average can't increase it either. That's why the algorithm always settles down.
3:10Local optima and k-means++
But k-means has a weakness. Watch what happens if all three stars happen to start inside the same group. Two of them split the bottom-left customers between themselves, and the third is left trying to represent both other groups at once. The algorithm still converges, because nobody wants to switch, but its inertia is ninety-nine, ten times worse than the nine point seven five we found before. It's stuck in a local optimum, just like the dips in a lumpy landscape from our gradient-descent episode. Two simple fixes are used everywhere.
First, smarter starting points, called k-means plus plus: after the first centroid, each new one is more likely to be chosen far away from the existing ones, so the starts are spread out. Second, run the whole algorithm several times from different starts, and keep the result with the lowest inertia.
4:08How many clusters? Elbow and silhouette
We told k-means to find three groups. But how would we know three is right? One idea: run k-means for k equals one, two, three, and so on, and plot the inertia. It always goes down. With twelve clusters, every customer is their own cluster and inertia is zero, so we can't just pick the smallest. Instead, we look for the elbow, where adding another cluster stops helping much. For Brightcart: two hundred and twelve, ninety-nine, then a dramatic drop to under ten at k equals three, and only small gains after that. A clear elbow at three.
A second tool is the silhouette score. For each customer, compare how close they are to their own cluster with how close they are to the nearest other cluster. A score near one means they sit comfortably in their own group; near zero means they're on a border; negative means they're probably in the wrong group. Average it over everyone. Pause and predict: which k gets the best silhouette?
Three again, with zero point seven eight. Two independent tools agree. In real data they often don't, so this is where business sense comes in. Can Meera actually run three distinct campaigns?
5:34Interpreting the clusters
Now the most important step, the one no algorithm does for us: interpreting the clusters. The blue group spends about two hundred and fifty dollars a year with one or two visits a month: occasional shoppers. The gold group spends around nine hundred a year but visits only a few times a month: big-basket planners. And the pink group visits about nine times a month with moderate spending: frequent top-up shoppers. Now Meera's campaigns write themselves: a reminder to bring occasional shoppers back, bulk-buy rewards for the planners, loyalty points per visit for the top-up crowd.
Remember, the algorithm found the groups, but humans gave them meaning. Always check that clusters make sense, and are useful, before acting on them.
6:25Hierarchical clustering and dendrograms
k-means needs us to choose k in advance. Hierarchical clustering doesn't. It builds a whole family tree of groupings. Start with every customer as their own tiny cluster. Find the two closest clusters and merge them. Repeat, again and again, until everyone is in one big cluster. Each merge is recorded in a diagram called a dendrogram, where the height of each bracket shows how far apart the merged groups were. Look at Brightcart's dendrogram. All the early merges happen at small heights, below two. Then there are two big jumps, to six point seven and seven point three.
Those tall brackets mean we're forcing together groups that are genuinely far apart. Cut the tree just below them and you get three clusters, the same three again. To measure the distance between groups rather than single points, we choose a linkage rule. The closest pair, the farthest pair, the average of all pairs, which is what we used here, or Ward's method, which merges whichever pair increases the spread the least. Hierarchical clustering is wonderfully informative, but it gets slow on large datasets.
7:41DBSCAN: clusters by density
k-means imagines clusters as round blobs around a centre. But what about shapes like these two interlocking crescents? Ask k-means for two clusters and it slices them straight down the middle. Completely wrong. DBSCAN takes a different view: a cluster is a region where points are packed densely together. It looks at a small circle, of radius epsilon, around each point. If enough neighbours fall inside, the point is a core point, and its neighbours join its cluster. Like ink spreading through paper, the cluster flows outwards from core point to core point, following whatever shape the dense region takes.
On the crescents, it recovers both moons perfectly. And there's a bonus. Points that aren't near any dense region are labelled as noise. Add two unusual customers to Brightcart's data, one who visits constantly but spends almost nothing, and one big spender who visits all the time, and DBSCAN still finds the three groups and flags those two as outliers that belong to no group. Its weaknesses: it struggles when different clusters have very different densities, and epsilon has to be chosen carefully, on scaled features, because it's a distance.
9:04Try it yourself: assign a new customer
A new customer signs up and, after a few months, spends about seven hundred dollars a year and visits seven times a month. Which segment do they belong to? Pause and measure their distance to the three centroids. To the occasional-shopper centre: about seven point one. To the big-basket planners: about four point five. To the frequent top-up shoppers: about two point three. The nearest is pink, so they join the frequent top-up group, and they get the loyalty-points campaign. Once trained, k-means doubles as a simple classifier: every new customer goes to the nearest centroid.
9:49Why scaling changes the clusters
Here's a trap: units. In our example, spend was in hundreds of dollars, conveniently on a similar scale to visits per month. Measure spend in dollars instead, from zero to a thousand, and the distance is dominated by spend, so visits are almost ignored. With our customers that happens to give the same three groups, because their spending bands already separate them. But on most data, the clusters turn into stripes sorted by spending alone. Just like k-N-N, k-means needs features on comparable scales, so standardise first, and choose your features deliberately, because they define what "similar" means.
10:32Soft clusters: Gaussian mixtures
k-means gives hard answers: every customer belongs to exactly one cluster. But a customer sitting between two groups might be seventy percent a top-up shopper and thirty percent a planner. Gaussian mixture models capture that. Each cluster is a bell-shaped cloud, with its own centre, spread and even orientation, so it can fit elongated, tilted groups that k-means struggles with, and each customer gets a probability of belonging to each cluster. They're trained by an alternating procedure that looks a lot like k-means: estimate memberships, update the clouds, repeat.
11:14k-means as compression
k-means also has a surprising second job: compression. Replace every point with its nearest centroid, and you only need to store the cluster number, instead of the full coordinates. The inertia measures how much detail you lost. Apply it to the colours of a photo, cluster thousands of colours into eight, and the image keeps its look with far less information. This idea, called vector quantisation, reappears in Season Three, inside the codecs that turn audio into tokens.
11:49Using clusters responsibly
Finally, using clusters well. After clustering, profile each group: averages, typical behaviours, what makes it distinct. Give each a name, design a campaign, and test it properly with an experiment, rather than assuming it works. Re-run the clustering every few months, because customers change. Check stability: run it again with different starting points and samples; if the clusters change dramatically, they're probably not real structure. And think about fairness. If clusters are built from sensitive information, or from proxies for it, like postcode, segment-based treatment could become unfair.
Always review which features define your groups.
12:36Myth vs Fact
Myth: clustering discovers the one true set of groups in your data. Fact: every algorithm finds groups according to its own definition, and the result depends on the features you choose, how you scale them, and settings like k or epsilon. Clustering is a tool for exploring and suggesting. Check its answers against what you know about the business.
12:59Recap
Let's recap. k-means alternates between assigning points to the nearest centroid and moving each centroid to its group's average, minimising inertia. It can get stuck, so we use smart starts and several runs. We choose k with the elbow, the silhouette score and business sense. Hierarchical clustering builds a dendrogram we can cut at any level. And DBSCAN finds dense regions of any shape and flags outliers as noise. One sentence to remember: clusters are discovered by algorithms, but defined by people.
13:37Next: PCA and dimensionality reduction
Mission complete. With no labels at all, the data revealed three kinds of Brightcart customers, and Meera has three campaigns ready to go. But here we used only two features, so we could see everything on a flat chart. Real customer data has dozens or hundreds of features. How do you look at fifty dimensions at once? Next: principal component analysis, the art of squeezing many features into a few without losing what matters. See you on the map.