Sub topic 14
Choosing between supervised and unsupervised approaches
This chapter assumes you know what features and labels are. It also assumes you have seen classification, regression, clustering, and dimensionality reduction. Here you will turn that knowledge into a decision procedure.
Start from the question, then check the data
Two questions decide most cases.
- Do you want to predict a specific, known quantity, such as a category or a number?
- Do you have examples where that quantity is already recorded?
If both answers are yes, use supervised learning. If you want to explore or summarize the data, or no recorded outcome exists, use unsupervised learning.
| Situation | Approach | Typical task |
|---|---|---|
| Predict whether a customer will cancel, and past cancellations are recorded | Supervised | Classification |
| Predict a house price, and past sale prices are recorded | Supervised | Regression |
| Find groups of similar customers, and no groups are defined | Unsupervised | Clustering |
| Reduce 50 sensor readings to a few summary values | Unsupervised | Dimensionality reduction |
The same data, two approaches
The iris measurements show the difference. With the species labels, a classifier learns to predict the species. Without them, K-means can only propose groups, and you must decide what the groups mean.
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import adjusted_rand_score
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
# Supervised: labels are used for training and for scoring.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
clf = LogisticRegression(max_iter=200).fit(X_train, y_train)
print("supervised accuracy:", clf.score(X_test, y_test))
# Unsupervised: labels are ignored when fitting.
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
# Labels are used here only to check the clusters afterward.
print("cluster agreement:", adjusted_rand_score(y, km.labels_))The supervised model has a clear score, accuracy on held-out data. The clustering result has no built-in right answer. Here we compared it with the known species, which a real unlabeled project would not have. In practice you judge clusters by whether they are stable and useful for the decision you need to make.
Common errors
- Clustering when you need a prediction. Clusters are not guaranteed to match the categories you care about. If you have labels for the target, use them.
- Forcing supervised learning without trustworthy labels. A model trained on noisy or biased labels learns that noise. Check label quality before you build anything.
- Treating cluster counts as facts. You choose
n_clusters. The data may support several different values. - Dropping labels you could have used. Leaving them out discards information. Labeled data should usually be used for supervised learning.
Combining the two
The approaches work well together. You can reduce many features with PCA before training a classifier. You can cluster unlabeled data to find groups, then label a few examples from each group to start a supervised project. Chapters later in this tutorial cover semi-supervised and self-supervised methods, which formalize this idea.
Exercise
A retailer has two years of purchase records with no customer segments defined. A manager wants to design three marketing campaigns for different types of shoppers. Which approach fits, and which prediction task would instead require labels?
Show answer
Clustering, an unsupervised approach, fits because no segment labels exist and the goal is to discover groups. A task like predicting whether a customer will respond to a campaign would be supervised classification, because it needs recorded past responses as labels.
Exercise
You run K-means on a dataset and get three clusters. Why can't you report an accuracy score for it the way you can for a classifier?
Show answer
Accuracy compares predictions with known labels, and unsupervised learning has no labels to compare against. You need other checks, such as whether the clusters are stable across runs or whether they are useful for the task.