ML Notes
A complete guide to the three main types of machine learning: supervised, unsupervised, and reinforcement learning with examples, comparisons, and Python code.
Machine learning algorithms are broadly categorized based on how they learn from data. The three main types are supervised learning, unsupervised learning, and reinforcement learning. Each has distinct characteristics and is suited for different kinds of problems.
Overview of ML Types
| Supervised | Unsupervised | Reinforcement | ||
|---|---|---|---|---|
| Learning | Learning | Learning | ||
| Has Labels | No Labels | Has Rewards | ||
| (X → y) | (X only) | (Action→R) |
Supervised Learning
In supervised learning, the algorithm learns from labeled training data — each input has a corresponding correct output (label). The model learns to map inputs to outputs.
Types of Supervised Problems
| │ ├── Binary | spam/not spam |
| │ ├── Multi-class | cat/dog/bird |
| │ └── Multi-label | tags on a blog post |
Example: Classification
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Supervised: we have features (X) AND labels (y)
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# The model learns the mapping: X -> y
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train) # Learning from labeled examples
predictions = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2%}")Example: Regression
Unsupervised Learning
In unsupervised learning, the algorithm works with unlabeled data — no correct answers are provided. The model must discover hidden patterns or structures on its own.
Types of Unsupervised Problems
Example: Clustering
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import numpy as np
# No labels! We only have features
X, _ = make_blobs(n_samples=300, centers=4, random_state=42)
# The model discovers groups on its own
kmeans = KMeans(n_clusters=4, random_state=42)
clusters = kmeans.fit_predict(X)
print(f"Found {len(set(clusters))} clusters")
print(f"Cluster centers:\n{kmeans.cluster_centers_}")
print(f"Cluster sizes: {np.bincount(clusters)}")Reinforcement Learning
In reinforcement learning, an agent learns by interacting with an environment. It receives rewards or penalties based on its actions and learns to maximize cumulative reward over time.
Key Concepts
| Agent | ────────────────► | Environment |
|---|---|---|
| ◄──────────────── |
Simple Example: Q-Learning
Comparison Table
| Aspect | Supervised | Unsupervised | Reinforcement |
|---|---|---|---|
| Data | Labeled (X, y) | Unlabeled (X only) | Environment + rewards |
| Goal | Predict labels | Find patterns | Maximize reward |
| Feedback | Correct answer | No feedback | Delayed reward signal |
| Examples | Spam detection | Customer segmentation | Game playing |
| Algorithms | SVM, Random Forest | K-Means, PCA | Q-Learning, DQN |
| Evaluation | Accuracy, F1, MSE | Silhouette score | Cumulative reward |
Semi-Supervised Learning
A hybrid approach using a small amount of labeled data with a large amount of unlabeled data.
Self-Supervised Learning
The model creates its own labels from the data structure — popular in modern NLP and vision.
Examples:
- Language models: Predict the next word in a sentence
- Masked language models: Fill in blanked-out words (BERT)
- Contrastive learning: Learn similar vs. different images
When to Use Each Type
| ├── YES (lots) | Supervised Learning |
| ├── YES (few) | Semi-supervised or Transfer Learning |
| ├── NO | Is there a clear reward signal? |
| │ ├── YES | Reinforcement Learning |
| │ └── NO | Unsupervised Learning |
Interview Questions
- What is the fundamental difference between supervised and unsupervised learning?
Supervised learning uses labeled data (input-output pairs) to learn a mapping function. Unsupervised learning works with unlabeled data to discover hidden patterns or structures.
- Give a real-world scenario where you'd use each type of ML.
Supervised: Email spam classifier (labeled spam/not spam). Unsupervised: Customer segmentation (no predefined groups). Reinforcement: Training a robot to walk (learns through trial and error).
- Can you convert an unsupervised problem into a supervised one?
Yes. For example, after clustering customers, you can label each cluster and train a supervised classifier to assign new customers to segments.
- What is the difference between classification and regression?
Classification predicts discrete categories (cat/dog), while regression predicts continuous values (price = $250,000).
- Why is reinforcement learning harder than supervised learning?
RL has delayed rewards (don't know immediately if action was good), credit assignment problem (which past action led to reward), and exploration-exploitation tradeoff.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Types of Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, fundamentals, types, types of machine learning
Related Machine Learning Topics