Unsupervised Learning
Theme
Palette
Download
Caption (tap to copy)
📖 Deep dive (full written explanation)
This cover signals a shift in mode: from explanation to execution. The promise is a complete, runnable pipeline on a real dataset — not pseudocode, not a toy. By walking from raw unlabeled data all the way to a plot of discovered clusters, the post turns the abstract mechanics of the previous day into a concrete sequence the reader can copy and run.
The emotional payoff is real: watching structure emerge from data that genuinely had no answer key is the moment unsupervised learning stops being a concept and becomes a tool you trust.
Step zero is environment setup, and it doubles as a table of contents for the techniques to come. The imports name every moving part: load_wine for data, StandardScaler for the all-important scaling, KMeans for clustering, PCA for dimensionality reduction, silhouette_score for label-free evaluation, and matplotlib for the final plot.
Using scikit-learn's built-in wine dataset is deliberate — it's small, real, and ships with the library, so anyone can run the code immediately with no downloads. The pip line up top makes the only two external dependencies explicit so there are no surprises when the reader actually executes the snippet.
Loading the data establishes the unsupervised setup honestly. The wine dataset has 178 samples and 13 chemical features, and it does come with a target — but we deliberately ignore it. The comment 'pretend we have no labels at all' is the whole point: we're treating a labeled dataset as unlabeled so that later we can quietly check whether the clusters we discover happen to line up with the real classes.
Printing the shape confirms the dimensionality (178 by 13) and foreshadows a problem the rest of the post solves: 13 features are impossible to plot directly, which is precisely why PCA appears in step five.
Standardization is step two because it must come before any distance-based step, and this slide shows it in action. StandardScaler.fit_transform centers each feature to mean 0 and scales it to standard deviation 1, so the 13 chemical measurements — which live on wildly different numeric ranges — each contribute fairly to the distance calculation.
The printed means near zero are a sanity check that the transform worked. The inline comment restates the reason so it can't be missed: without this step, whichever feature has the largest raw range would dominate the clustering and the 'segments' would be a noisy reflection of that one variable rather than genuine multivariate structure.
Choosing k is step three, and this loop demonstrates the disciplined way to do it instead of guessing. For each candidate k from 2 to 5, it fits k-means, then prints two numbers: inertia (total within-cluster distance) and the silhouette score. Inertia always decreases as k grows, so it alone can't pick k — but the silhouette score peaks at the most natural number of clusters.
For the wine data, k=3 wins, which is a satisfying result because the dataset really does contain three wine cultivars. The loop embodies the elbow-plus-silhouette philosophy from the mechanics post: let quantitative signals choose k, and treat agreement between them as confirmation.
The pipeline diagram zooms out from the line-by-line code to show the shape of the whole workflow: load the unlabeled X, scale it, cluster it with k-means at k=3, reduce it to two dimensions with PCA, and plot. Each stage maps one-to-one onto a code slide in this post.
Having the visual map in the middle of the code-heavy post is intentional. It gives the reader a place to orient — to see how the snippet they're looking at fits into the end-to-end process — so the individual steps cohere into a single repeatable recipe rather than a list of disconnected commands.
This is the payoff step: fitting the final k-means model. With k fixed at 3 from the previous analysis, fit_predict both trains the model and returns the discovered cluster id for every sample in one call. Printing the first ten labels shows the integers the algorithm assigned, and the centroid shape (3 by 13) confirms there are three cluster centers, each living in the original 13-dimensional feature space.
The final silhouette print grades the result on its own terms — no labels needed. Together these three lines are the heart of the whole pipeline: everything before prepares the data, and everything after merely visualizes and validates what this step produced.
PCA enters as step five to solve the visualization problem. The data lives in 13 dimensions, which is impossible to plot, so PCA compresses it to two components that capture as much variance as possible. The explained_variance_ratio_ printout quantifies the trade: in this dataset the first two components retain roughly half the total variance — enough to reveal the cluster structure on a flat plot.
The important conceptual note is that this is lossy compression. We're discarding eleven dimensions worth of variation to gain a picture we can actually see. That's an acceptable trade for visualization, but it's also why post 5 warns against reading exact distances off a PCA plot as if no information were lost.
The plotting step is where the abstract finally becomes visible. The scatter plots every sample at its two PCA coordinates, colored by the cluster id k-means assigned. When you run it, three reasonably clean blobs appear — and that's the emotional climax of the post: structure the model discovered entirely on its own, with no labels ever provided, made visible on a single chart.
The axis labels PC1 and PC2 are a quiet reminder that these are principal components, not original features. The comment reinforces the headline message: the grouping you're looking at was found, not given, which is the entire promise of unsupervised learning delivered in one image.
This recap slide distills the pipeline into the five things that actually mattered, so the reader walks away with principles rather than just code. No label column was ever used — the unsupervised premise held throughout. Scaling made the distance metric fair. k=3 came from the data via silhouette, not from a guess. PCA made 13 dimensions plottable in two. And silhouette graded the whole thing without any labels.
Pulling these out as bullets turns the worked example into transferable knowledge. The next dataset will have different numbers, but this exact sequence of decisions — scale, choose k with metrics, fit, reduce, evaluate — carries over unchanged.
Knowing how to break the pipeline is as valuable as knowing how to run it, so this slide catalogs the easy failures. Skip StandardScaler and a single large-range feature hijacks the clusters. Forget n_init and a single unlucky random start can yield nonsense groups. Read PCA axes as if they were original features and you'll draw false conclusions, because they're blends. And don't assume the three discovered clusters perfectly match the true wine classes — sometimes they align well, often only partly.
That last point is the most important and the most honest: unsupervised structure is not guaranteed to recover any 'true' categorization, even when one exists. The clusters are a discovered organization of the data, useful but not authoritative — exactly the mindset post 5 expands into a full set of cautions.
The CTA transitions from doing to scrutinizing. Having run a clean, successful pipeline, the reader is now best positioned to learn what goes wrong, because they have a concrete reference for what 'right' looks like. The teaser frames post 5's theme precisely: the silent traps that make unsupervised results look real but mean nothing.
This ordering is intentional. Mistakes land harder once you've seen the correct workflow, so the final post can focus on the subtle failure modes rather than re-explaining the basics.