Sub topic 11
Unsupervised learning: finding structure without labels
This chapter assumes you know what features and labels are, and that you have seen supervised learning, where a model learns from labeled examples. You should be able to read short Python and NumPy code.
What "unsupervised" means
In supervised learning, every training example comes with a label, and the model learns to predict it. In unsupervised learning, the data has features only. No column says what the "right answer" is.
The goal changes as a result. The model does not predict a known target. It looks for structure in the features themselves, such as groups of similar examples or a simpler way to describe each example.
| Row | Supervised | Unsupervised |
|---|---|---|
| Training data | Features and labels | Features only |
| Goal | Predict the label for new inputs | Describe structure in the data |
| Typical loss signal | Error against known labels | Internal criterion, such as cluster tightness |
| Evaluation | Compare predictions to true labels | Harder, often needs judgment |
Two common tasks
Most unsupervised work falls into two families.
- Clustering assigns each example to a group so that examples in the same group are more similar to each other than to examples in other groups. Customer segmentation is a typical use.
- Dimensionality reduction replaces many features with fewer ones that keep most of the useful information. It is used for visualization, noise reduction, and compression.
A worked example
The iris dataset has species labels, but we can ignore them to see whether the measurements form natural groups. K-means clustering is given the number of clusters to find. It repeatedly moves cluster centers to reduce the distance between each example and its nearest center.
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
X, _ = load_iris(return_X_y=True) # the labels are ignored
kmeans = KMeans(n_clusters=3, n_init=10, random_state=0)
kmeans.fit(X)
print(kmeans.labels_[:10])
print(kmeans.cluster_centers_)Two things in the output deserve attention.
labels_holds cluster numbers such as 0, 1, and 2. These are arbitrary names the algorithm invented. Cluster 0 does not mean "setosa".cluster_centers_has one row per cluster and one column per feature. Each row is the average position of the examples in that cluster.
We asked for three clusters because we already knew there were three species. On real unlabeled data you usually do not know the right number, so you must choose it.
Common errors
- Treating cluster numbers as classes. Cluster IDs have no meaning until you inspect the examples inside each cluster and decide what they share.
- Forgetting to scale features. K-means and PCA both depend on distances and variances. A feature measured in thousands will dominate one measured in fractions. Standardize features first when units differ.
- Assuming clusters are real. K-means returns the number of clusters you ask for, even if the data has no real groups. A result is a description of the data, not proof that the groups exist.
- Expecting a single accuracy score. Without labels there is no ground truth to compare against. You evaluate with internal measures such as the silhouette score, with stability across random seeds, or by checking whether the groups are useful for your task.
When unsupervised learning fits
Use it when labels are missing or expensive to get, when you want to explore a dataset before deciding what to predict, or when you need a compact representation as input to a later step. If you have reliable labels and a specific prediction to make, supervised learning is usually the better choice.
Exercise
You run K-means with three clusters on customer data and get cluster IDs 0, 1, and 2. A colleague says cluster 2 is "high-value customers". What must be true before you accept that claim?
Show answer
Cluster IDs are arbitrary, so the claim needs evidence. You must inspect the customers in cluster 2, for example their average spend and purchase frequency compared with the other clusters, and confirm they share the high-value trait. You should also check that the clustering is stable across seeds and that the features were scaled.