What is the difference between supervised and unsupervised learning? Give one real-world example of each.
Supervised learning trains on labelled examples, inputs paired with the right answer, and learns to predict that answer for new inputs. Predicting a category is classification, like spam or not spam; predicting a number is regression, like a flat's price. Unsupervised learning gets no labels and finds structure by itself: clustering shoppers into segments, or reducing hundreds of features to a few. Spam filtering is supervised; customer segmentation is unsupervised.
Full answerHide full answer2 min read
| Supervised | Unsupervised | |
|---|---|---|
| Training data | Inputs and the correct outputs | Inputs only |
| Goal | Predict the label for new data | Find groups, patterns or a simpler representation |
| Main tasks | Classification, regression | Clustering, dimensionality reduction, anomaly detection |
| Example algorithms | Linear and logistic regression, decision trees, random forests | k-means, DBSCAN, PCA |
| How it's evaluated | Against the true labels: accuracy, F1, RMSE | Harder: silhouette score, business usefulness |
| Real example | Will this loan default? What will this flat sell for? | Which customers behave alike? Which transactions look unusual? |
The same data, with and without labels
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.linear_model import LogisticRegression
X, y = make_blobs(n_samples=300, centers=3, random_state=42)
clf = LogisticRegression().fit(X, y) # supervised: learns from X and the labels y
print(clf.predict(X[:8]), y[:8]) # [2 2 0 1 2 1 0 1] [2 2 0 1 2 1 0 1]
km = KMeans(n_clusters=3, n_init=10, random_state=42).fit(X) # unsupervised: X only
print(km.labels_[:8]) # [1 1 0 2 1 2 0 2]k-means found the same three groups, but numbered them differently: without labels, it has no way to know what each group should be called. Naming and interpreting clusters is up to you.
Other kinds you may be asked about
- Semi-supervised: a few labelled examples plus many unlabelled ones.
- Self-supervised: the labels come from the data itself. Large language models are pre-trained this way, by predicting the next token of ordinary text.
- Reinforcement learning: an agent learns from rewards for its actions, as in game-playing or robotics.
Follow-ups
- Classification or regression: predicting a customer's age? Regression, because age is a number. Predicting an age band such as "18–25" would be classification.
- Can unsupervised learning help a supervised model? Yes. Cluster labels or PCA components can become features, and anomaly scores can flag rows to check before training.