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 a supervised model (a classifier or regressor) and an unsupervised one (k-means or PCA) in earlier chapters, and that you can read short scikit-learn scripts. The goal is a repeatable way to pick an approach before writing any model code.
The question comes first
Beginners often start with a dataset and ask which algorithm to run. Start with the question you need answered. The approach follows from three things:
- What output do you need?
- Do you have trustworthy labels for that output?
- What will a person or system do with the result?
Supervised learning answers "what is the value of this specific target for a new example?" Unsupervised learning answers "what structure exists in these examples?" Those are different questions. A tool that answers one cannot be swapped in for the other.
The core criterion: is there a target you can name and measure?
Supervised learning needs a label column that records the outcome you care about. Three conditions must hold:
- The target is defined. "Will this customer cancel within 30 days?" is a target. "Is this customer interesting?" is not.
- Labels exist for enough examples. The number depends on the problem. A few hundred labeled examples can be enough for a simple model on clean tabular data. Noisy or high-dimensional problems need far more.
- Labels will be available at prediction time only through the model. If the label is known when you need the prediction, you don't need a model.
If all three hold, supervised learning is usually the default choice. It gives you a number you can measure on held-out data, such as accuracy or mean absolute error. That makes it easy to tell whether the model works.
If the target is undefined or labels don't exist, you are in unsupervised territory, or you need to create labels first.
Matching the task to the paradigm
| Need | Typical approach | Example |
|---|---|---|
| Predict a category from known examples | Supervised classification | Flag a transaction as fraud or not fraud |
| Predict a quantity from known examples | Supervised regression | Estimate delivery time in minutes |
| Discover groups with no predefined categories | Unsupervised clustering | Find segments in customer behavior |
| Compress many features to a few | Unsupervised dimensionality reduction | Reduce 200 sensor readings to 5 components |
| Find unusual examples without labeled anomalies | Unsupervised (density or distance based) | Spot odd server logs |
Two cases blur the lines:
- Anomaly detection can be supervised if you have many labeled anomalies. It is usually unsupervised because anomalies are rare and unlabeled.
- Dimensionality reduction is often a preprocessing step for a supervised model, not a goal in itself.
A decision flow
The diagram turns the criteria into a sequence of questions.
Notice that both unsupervised branches end in a validation step. That step matters most in practice and is covered below.
Evaluation is the practical difference
Supervised models have an objective test. You hold out labeled data, predict, and compare to the truth. A wrong answer is visible.
Unsupervised models have no ground truth, so "good" has to be defined by something else. Common options:
- Internal metrics, such as the silhouette score for clusters or explained variance for PCA. These measure geometric tidiness, not usefulness.
- A downstream task, such as whether cluster-based features improve a supervised model's test score.
- Expert review, where someone who knows the domain checks whether the groups are meaningful.
- Stability, meaning whether the same groups appear when you resample the data or change the random seed.
An unsupervised algorithm will always return something. K-means with n_clusters=3 returns three clusters whether or not the data has three groups. A tidy result is not evidence that the structure is real.
Comparing both approaches on one dataset
The Iris data has species labels, so we can run both approaches and compare them. Real unlabeled problems don't give you this check, which is why it is worth seeing here.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
# Supervised: learn the species label directly.
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
print("supervised accuracy:", clf.score(X_test, y_test))
# Unsupervised: fit on features only, then compare to the labels afterward.
km = KMeans(n_clusters=3, n_init=10, random_state=0)
km.fit(X_train)
ari = adjusted_rand_score(y_test, km.predict(X_test))
print("clustering agreement (ARI):", ari)The supervised model is scored with accuracy against the true labels. The clusters are scored with the adjusted Rand index (ARI), which measures how well two groupings agree. ARI ignores the arbitrary cluster numbers, so cluster "2" does not need to match species "2". A score of 1.0 means a perfect match and a score near 0 means chance-level agreement.
Expect the supervised accuracy to be high. Expect the ARI to be good but lower, because the clusters follow the shape of the measurements, not the species definition. Setosa separates cleanly. Versicolor and virginica overlap, so k-means mixes some of them. This gap is the general pattern: when you have the labels you care about, a supervised model targets them directly. Clustering finds whatever structure is strongest in the features.
Common errors
Using clustering to predict a known label. If you have species labels and want to predict species, don't cluster and map clusters to labels. You lose information the labels carry.
Treating clusters as ground truth. Clusters depend on feature scaling, the distance measure, the algorithm, and k. Rescale one feature and the groups can change. Standardize numeric features before distance-based methods unless their raw scales are meaningful.
Training on labels that don't match the real goal. Suppose you want to predict customer value but only have "clicked an ad" labels. A supervised model will predict clicks well and still miss your goal. Check that the label is the outcome you care about, not a convenient substitute.
Leaking the target into the features. A feature recorded after the outcome happens, such as "refund issued" when predicting cancellation, makes test scores look excellent and fails in production. This is a supervised problem, and unsupervised work has a version of it when a leaked column dominates the structure.
Assuming more clusters is better. Training inertia always drops as k grows. It reaches zero when every point has its own cluster. Choose k using silhouette scores, stability, or a downstream use, not inertia alone.
Running PCA and then interpreting components as raw features. Components are linear mixtures of the original columns. They preserve variance, which is not the same as preserving what predicts your target. A low-variance feature can still be the most predictive one.
Hybrid patterns that are common in practice
Real projects rarely stay in one paradigm. Three patterns come up often:
- Unsupervised feature engineering. Reduce dimensions with PCA, or add a cluster ID as a feature, then train a supervised model. Judge the step by whether the supervised test score improves.
- Exploration before modeling. Cluster or project the data to find mislabeled rows, odd subgroups, and data-quality problems. Then define the supervised task.
- Cheap labels from structure. Cluster the data, label a few examples per cluster, and use those to start a labeled dataset. This is a bridge toward semi-supervised methods, covered in the next chapter.
Decision criteria beyond the basics
| Factor | Leans supervised | Leans unsupervised |
|---|---|---|
| Target definition | Clear and measurable | Vague or still being discovered |
| Label cost | Low or already collected | High or impossible to collect |
| Need for accountability | Must report error rates | Exploratory, no formal error rate |
| Stability of the world | Labels stay valid over time | Categories shift, so fixed labels go stale |
| Interpretability goal | Explain a prediction | Describe the dataset |
Labeling cost deserves a concrete check. Estimate the cost of labeling 500 examples, then train on 100, 200, 300, and 400 of them and plot test score against training size. If the curve is still rising, more labels will help and the cost is justified. If it has flattened, the problem lies elsewhere, such as weak features or noisy labels.
Exercise
A retailer has two years of purchase records with no outcome column. The marketing team asks, "What kinds of customers do we have?" Which paradigm fits, and how would you check that your result is useful?
Show answer
Unsupervised learning, specifically clustering, because there is no target to predict and the question asks for structure. Standardize the numeric features first. Choose the number of clusters using silhouette scores and stability across resampled data and different random seeds, not training inertia alone. Then check usefulness by having marketing staff review the cluster profiles, and by testing whether campaigns tailored to each cluster behave differently. A cluster is only useful if it leads to a different decision.
Exercise
A team wants to predict whether a loan will default. They have a history of approved loans and a column recording whether each defaulted. One engineer proposes running k-means with two clusters and calling one cluster "default". What is wrong with this plan, and what should they do instead?
Show answer
The team has a measurable target and labels, so this is a supervised classification problem. K-means groups by feature similarity, and nothing makes one cluster correspond to default. Mapping clusters to labels discards the labeled information. They should split the data into training and test sets, train a classifier on the default column, and evaluate it on held-out loans with metrics suited to imbalanced classes, such as precision, recall, or area under the ROC curve. They should also check that no feature leaks the outcome.