Supervised vs Unsupervised Learning
Python · Machine Learning (Scikit-Learn)
Theory
In supervised learning every training example comes with the answer, called the label or target. The model learns the mapping from features to label and then predicts labels for new rows. If the label is a number, such as a flat's price, it is regression; if it is a category, such as approved or rejected, it is classification. Linear regression, logistic regression, decision trees, random forests and KNN are supervised.
In unsupervised learning there is no label. The algorithm finds structure in the features alone: clustering (KMeans) groups similar customers into segments, and dimensionality reduction (PCA) compresses many columns into a few. In scikit-learn the difference is visible in the code: supervised models call fit(X, y), unsupervised ones call fit(X). Because there is no right answer to compare with, unsupervised results need human interpretation.
Why we use it
The first decision in any ML task is which family of methods applies. Knowing whether you have labels tells you which algorithms, metrics and validation methods you can use.
When to use it
Use supervised learning when historical data has the outcome you want to predict (price, churn, fraud). Use unsupervised learning to explore unlabelled data, segment customers, detect unusual records or reduce features before another model.
How it works
- Dataset with features X
- Does each row have a known outcome y? Yes: Supervised learning. No: Unsupervised learning.
- Supervised: is y a number? Yes: Regression (e.g. price). No: Classification (e.g. approved).
- Unsupervised: cluster rows or reduce columns
- Choose algorithms and metrics for that family
Syntax
# supervised: learn X -> y
model.fit(X, y); model.predict(X_new)
# unsupervised: structure in X only
kmeans.fit(X); kmeans.labels_
pca.fit_transform(X)Example
LinearRegression().fit(X, y) # supervised
KMeans(n_clusters=2, n_init=10).fit(X) # unsupervisedKey points
- Supervised = labelled data, fit(X, y)
- Regression predicts numbers; classification predicts categories
- Unsupervised = no labels, fit(X) finds structure
- Clustering and PCA are the common unsupervised tools
- Unsupervised results need human interpretation