Supervised vs Unsupervised
The two main families of ML: learning from labelled answers, or finding structure alone.
Machine learning problems split into two big families.
Supervised learning
You have labelled examples: inputs X and known answers y. The model learns the mapping. Two kinds:
- Regression — predict a number (house price).
- Classification — predict a category (spam / not spam).
Unsupervised learning
No labels — the model finds structure on its own, e.g. clustering customers into groups.
Example
# Supervised: X (features) and y (labels) are both given
# Regression -> y is a number
# Classification -> y is a category
X = [[1400], [1600], [1700]] # house size
y = [200000, 250000, 270000] # price (regression target)
# Unsupervised: only X, no y — find groups
# from sklearn.cluster import KMeans
# KMeans(n_clusters=3).fit(X)
print('supervised needs labels; unsupervised does not')When to use it
- A bank trains a supervised classifier on labelled historical loan applications (approved/rejected) to predict whether new applicants should be approved.
- An e-commerce platform uses unsupervised k-means clustering on purchase history to discover customer segments without any predefined labels.
- A team first applies unsupervised PCA to reduce 200 raw features to 20 principal components, then passes the result to a supervised classifier.
More examples
Supervised classification example
Trains a supervised logistic regression classifier on labelled cancer data and evaluates it on unseen test examples.
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
model = LogisticRegression(max_iter=5000).fit(X_tr, y_tr)
print('Test accuracy:', model.score(X_te, y_te))Unsupervised clustering example
Runs k-means clustering without any labels, discovering four natural groupings by finding similar patterns in the data alone.
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=300, centers=4, random_state=0)
kmeans = KMeans(n_clusters=4, random_state=0).fit(X)
print(kmeans.labels_[:10]) # cluster assignment, no labels usedSupervised vs unsupervised pipeline
Combines unsupervised PCA (dimension reduction without labels) with a supervised classifier in one Pipeline, illustrating how the two families complement each other.
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
pipe = Pipeline([
('pca', PCA(n_components=40)), # unsupervised step
('clf', RandomForestClassifier()) # supervised step
])
pipe.fit(X, y)
print('Score:', pipe.score(X, y))
Discussion