Sort produce
Group fruit by size, shape and color to identify batches that may need different handling.
Lecture 16 of 20
Clustering · Part 1
Find groups in unlabeled observations, fit centroids and decide how many groups are useful.
Clustering is an unsupervised learning task. Measure each item, then group similar items using those measurements. The data come without class labels.
Group fruit by size, shape and color to identify batches that may need different handling.
Group nearby stops into service zones that can be assigned to different drivers.
Group sensor readings into operating patterns and flag isolated readings for inspection.
A cluster can help you sort, plan or investigate. A person still has to decide whether the grouping is useful for the task.
Different algorithms describe the structure of the same cloud in different ways.
Assign each point to the nearest cluster mean. The crosses are the means.
Give each point a probability of belonging to each overlapping component.
Merge nearby groups into a tree. A horizontal cut chooses a partition.
Each dot is an observation. These methods return a hard partition, membership probabilities, or a tree of nested groups.
Parts 4 and 5 use principal component analysis (PCA) to study the features and prepare them for further analysis.
Find the directions of greatest spread. Projecting onto one direction gives one score per point.
Transform the features before another method sees them. Whitening rescales the principal scores.
The three clustering methods group observations. PCA describes their variation and can be used before a clustering step.
Imagine a basket of mixed fruit. Shape, color, and taste give us clues about which pieces belong together, even before anyone names the groups.
In classification, a target label tells us the category to learn. In clustering, shape, color, and taste are all features. The task is to discover groups whose members resemble one another and differ from those in other groups.
Each row is one fruit. The two measurement columns give features per fruit.
| Observation | Weight (g) X1 | Diameter (mm) X2 |
|---|---|---|
| x1 | 145 | 70 |
| x2 | 178 | 76 |
| x3 | 125 | 58 |
is the -th feature. The entry is feature of observation . Here g, and the whole second row is . A distance compares two such feature vectors.
Inspect this unlabeled cloud. Which points seem close to one another, and where are the gaps?
The coordinate axes are two features. Pan or zoom, then return to the full plot. Three compact regions are visible at this scale.
Points within a group should be similar. Points in different groups should be relatively dissimilar.
A grouping depends on which features and distance are useful for the task. With no supplied targets, accuracy against known labels is unavailable during fitting. Domain knowledge and geometric checks help interpret the result.
Assume there are groups. Represent group by an average observation, its centroid .
A centroid has the same coordinates as a data row. It can lie between observations. K-means learns the centroids together with the group assignments.
With clusters, give each cluster a column. Put a in the assigned cluster’s column and in the other two.
Assigned to C2
| C1 | C2 | C3 |
|---|---|---|
| 0 | 1 | 0 |
The membership vector is the row for observation . The collection contains one membership vector for each of the observations. In , the first 2 identifies the fruit and the second identifies the cluster.
From initial centroids, assign points to the nearest centroid, then recalculate each centroid as its group mean. Repeat to reduce the distortion .
The double sum adds the squared distance of each of the observations to its assigned centroid. The constraint keeps each row one-hot. The hats mark the minimizing centroids and assignments.
Fix the centroids at and . Move the observation to see its squared distances, membership row and contribution .
Centroid 1 is closer, so its entry in the membership row is 1.
Centroid 1 is closer, so its entry in the membership row is 1.
Centroid 1 is closer, so its entry in the membership row is 1.
Both squared distances are equal. This example assigns the point to centroid 1 on a tie.
Centroid 2 is closer, so its entry in the membership row is 1.
Centroid 2 is closer, so its entry in the membership row is 1.
Centroid 2 is closer, so its entry in the membership row is 1.
Each flag is either or , so differentiation does not directly choose the assignment. Add the selected squared distances over all observations to get .
To minimize with the centroids fixed, choose the membership flags . These are discrete choices, so the usual method of setting a derivative to zero does not apply directly.
For each observation, compare the squared distances and put its only in the column of the nearest centroid.
For observation , all but one flag are zero. Its contribution to is the squared distance to the centroid whose flag is , so choose the smallest squared distance.
Read the argmin as “the index with the smallest distance”. If the current centroid is tied for closest, keep that assignment. Use a fixed order for any other tie.
Keep the assignments fixed and find the new centroid for each nonempty cluster .
Only points with contribute to cluster ’s squared distances. The update adds those same points and divides by their count to get the cluster mean.
Differentiate with respect to . At the minimum, set the gradient to zero and collect the terms containing .
The numerator adds the observations assigned to , and the denominator counts them. Dividing gives the average of all observations in cluster . For a nonempty cluster, this mean uniquely minimizes the convex quadratic. An empty cluster needs a separate rule.
Drag the trial center for the points 0, 2 and 4. Where does their sum of squared distances reach its minimum?
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
The centroid is at the mean.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
Try moving the centroid closer to the mean at 2.
The mean is . At that position the squared distances are 4, 0 and 4.
One update of already finds the best assignments for the current centers. One update of already finds the best centers for the current assignments.
The centers stayed put. The same distances give the same memberships.
The groups stayed the same. Averaging the same members gives the same centroids.
Moving the centers can change the nearest cluster. Changing the members can change the mean.
With finitely many data points, there are finitely many ways to assign them to clusters. Each round either changes assignments and lowers , or leaves the assignments stable.
Every point that switches goes to a strictly closer center. This lowers the distortion J.
The mean is the best center for a nonempty cluster, so this step cannot raise J.
The same members give the same means. Both the assignments and the centers are stable.
During a tie, keep the current assignment if it is one of the nearest. Keep the previous center for an empty cluster. Each partition has a fixed distortion once its means are computed, so a strictly improving run cannot revisit it.
Advance through the five frames. Track one cross and the points with its color.
Crosses mark centroids. A point can switch groups as the centers move. In this example the visible groups settle after a few rounds.
Use Step to see which changes first. Then try a different initialization or change .
Each of the two operations holds the other set of choices fixed. The final measures how close the points are to their own fitted centers.
Each update improves one part of the fit with the other part fixed. A different partition may require changing assignments and centers together.
The left cluster keeps five points. Its mean is 3.6 and the right mean is 12.5.
The left cluster keeps three points. Its mean is 1 and the right mean is 10.
Same observations and same . Both fits are stable, but beats . Try several starting points and keep the lowest distortion you actually find.
One centroid summarizes the whole dataset. More centroids make it possible to fit smaller groups more tightly.
The best achievable distortion cannot increase with . At , placing one centroid at each observation gives . That solution remembers each observation separately. Individual runs can end at higher local solutions.
Choose from 1 to and press Run K-means. Compare the partition, the centroids and distortion on the same 12 observations.
The example runs K-means on these points each time. It tries one farthest-first initialization from each point and reports the run with the lowest .
Fit the same data for each . Look for a bend where adding clusters starts to buy much less reduction in distortion.
The plotted highlight marks a candidate bend. The next slide shows one way to calculate a candidate.
Draw a line between the first and last tested points. For each intermediate , compute , then pick the largest positive gap.
The line is at 54.5 and the fitted distortion is 37. Subtract the distortion from the line height. The candidate gaps are 17.5, 25, and 13.5, so the largest is at K = 3.
The line is at 39 and the fitted distortion is 14. Subtract the distortion from the line height. The candidate gaps are 17.5, 25, and 13.5, so the largest is at K = 3.
The line is at 23.5 and the fitted distortion is 10. Subtract the distortion from the line height. The candidate gaps are 17.5, 25, and 13.5, so the largest is at K = 3.
The dashed line gives . In this example, it drops from 70 to 8 in four equal steps of 15.5. Small gaps throughout mean there is little evidence of an elbow.
To compare choices of , first score each point. Start with , the average squared distance from to the other points in its own cluster.
is the cluster of . The index identifies another observation. Exclude itself, so the denominator is the cluster size minus 1.
For each other group, compute the average squared distance from to its members. Keep the smallest of those averages as .
is another cluster and counts its members. First average within each cluster, then compare the averages across clusters.
A positive score favors the current cluster. A score near zero signals a close call. A negative score means another cluster is closer on average.
The gap can be no larger in magnitude than the larger average. Dividing by that average scales the score to . A point alone in its cluster gets 0 by convention. Use 0 when both averages are zero. Compare partitions with .
How well does the ringed point fit its current cluster? Compare its average squared distance to its own cluster with the smallest average for the other clusters.
Labels show squared distances to the selected point.
A positive score means the point is closer on average to its own cluster than to the best alternative.
Change the inspected point. Compare its own-group average, nearest alternative and silhouette. Which points sit near a boundary?
The default uses squared Euclidean dissimilarity, matching the equations. The optional Euclidean view shows how a different convention can change the numbers.
For each tested , average the silhouette scores of all observations. For example, if give means , , and , choose .
is the average silhouette score for one fitted partition. The silhouette coefficient is the highest of these averages. Choose the tested number of clusters that gives that highest average.
Switch between distortion, mean silhouette and the partition. Does the elbow agree with the largest mean silhouette?
The two criteria reward different aspects of the same partitions. Interpret a candidate alongside the plotted groups and the goal of the analysis.