iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Types of ML

A quick map of ML problem types: supervised (regression, classification), unsupervised (clustering, dimensionality reduction), and reinforcement.

ML — problem types

EXAMPLE
# ===== The map =====
#
# Supervised: you have inputs X and labels y
#   - Regression: predict a continuous y (price, duration, score)
#   - Classification: predict a discrete y (spam vs not, A/B/C)
#
# Unsupervised: you have only X
#   - Clustering: discover groups (k-means, DBSCAN)
#   - Dimensionality reduction: project to fewer dims (PCA, UMAP)
#   - Density / anomaly: where do points lie? (Isolation Forest, OCSVM)
#
# Self-supervised: labels derived from the data itself
#   - Predicting masked tokens / pixels (BERT, MAE)
#   - Contrastive: same vs different pairs (SimCLR, CLIP)
#
# Reinforcement learning: agent, environment, reward
#   - On-policy (PPO), off-policy (DQN), offline (CQL)

# ===== Picking the right shape =====
# Question:                          Likely shape:
# 'How much will X cost?'            regression
# 'Is this spam?'                    binary classification
# 'Which of 5 buckets?'              multi-class classification
# 'Group similar customers'          clustering
# 'Find weird transactions'          anomaly detection
# 'Compress 200 features to 10'      dimensionality reduction
# 'Choose actions over time'         reinforcement learning

# ===== Same problem, different shapes =====
# 'Predict next-month revenue' -> regression
# 'Predict whether revenue grows next month' -> classification
# The framing changes the algorithm, metric, and evaluation strategy.

# ===== Metrics, picked to match =====
# Regression
#   MAE  (median is robust to outliers)
#   RMSE (penalises large errors more)
#   R^2  (proportion of variance explained)
# Classification
#   Accuracy on BALANCED data
#   F1 / Precision / Recall on imbalanced
#   AUC (ranking quality, threshold-independent)
#   Log loss / Brier for calibrated probabilities
# Clustering
#   Silhouette, Davies-Bouldin, downstream task accuracy
# Anomaly
#   Precision@k (only the top k flagged are inspected)
# RL
#   Cumulative reward, return-per-episode, regret

# ===== Tiny worked examples =====
# Regression (sklearn):
from sklearn.linear_model import Ridge
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)
m = Ridge().fit(Xtr, ytr)
print('R^2:', m.score(Xte, yte))

# Classification:
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, stratify=y, random_state=42)
m = LogisticRegression(max_iter=2000).fit(Xtr, ytr)
print('acc:', m.score(Xte, yte))

# Clustering:
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=300, centers=4, random_state=42)
km = KMeans(n_clusters=4, n_init='auto').fit(X)
print('inertia:', km.inertia_)

# Dimensionality reduction:
from sklearn.decomposition import PCA
pca = PCA(n_components=2).fit(X)
print('explained var:', pca.explained_variance_ratio_)

# ===== Patterns to internalise =====
# - Frame the question BEFORE picking an algorithm
# - Match metric to outcome: ranking vs absolute prediction vs calibration
# - Try the simplest baseline (Ridge / Logistic / mean predictor) first
# - When a problem looks like 'we need a model', often a rule or threshold ships faster

# ===== Pitfalls =====
# - Using accuracy on heavily imbalanced data -> 99% by predicting majority
# - Clustering 'just because' -> what downstream decision changes?
# - Regression on logged data without exp() back -> reported error is in log space
# - Reinforcement learning when a contextual bandit (or a rule) would do

Why it matters

Pick the problem shape first; the algorithm follows from there. Regression vs classification vs clustering vs RL come with their own metrics, baselines, and failure modes. Spending 10 minutes framing saves 10 weeks of model debt.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Supervised:   classification, regression — labels exist.
# Unsupervised: clustering, dim reduction — no labels.
# Reinforcement: agent learns from rewards.
# Self-supervised: labels come from the data itself (LLMs, MAE).
Try it Yourself »

Discussion

Loading…