The Three Types of Machine Learning
Machine Learning is broadly categorized into three main types based on how the algorithm learns from data. Understanding these types is fundamental to choosing the right approach for your problem.
1. Supervised Learning
Supervised learning uses labeled data — each input comes with a correct output. The model learns to map inputs to outputs by training on example pairs.
How It Works
Training Data:
Input: [email text] → Output: spam/not spam
Input: [house features] → Output: price
Input: [medical image] → Output: diagnosis
Classification
Classification predicts discrete categories:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
# Student performance prediction
# Features: [study_hours, sleep_hours, attendance_pct]
X = [
[8, 7, 95], [2, 5, 60], [6, 8, 85], [1, 4, 50],
[9, 6, 92], [3, 7, 70], [7, 8, 88], [4, 5, 65],
[10, 7, 98], [2, 6, 55], [5, 9, 80], [8, 5, 90],
]
# Labels: 0 = Fail, 1 = Pass
y = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
# Train classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
# Evaluate
predictions = clf.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2%}")
print(classification_report(y_test, predictions, target_names=['Fail', 'Pass']))
# Predict for a new student
new_student = [[7, 7, 88]]
result = clf.predict(new_student)
print(f"Prediction: {'Pass' if result[0] == 1 else 'Fail'}")
Regression
Regression predicts continuous values:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# House price prediction
# Features: [size_sqft, bedrooms, age_years]
X = [
[1200, 2, 10], [2500, 4, 5], [800, 1, 20],
[1800, 3, 8], [3000, 5, 2], [1500, 3, 15],
[2200, 4, 3], [1000, 2, 12], [2800, 4, 1], [1300, 2, 7],
]
prices = [250000, 500000, 150000, 380000, 650000, 300000,
480000, 200000, 620000, 280000]
model = LinearRegression()
model.fit(X, prices)
# Predict house price
new_house = [[2000, 3, 5]]
predicted_price = model.predict(new_house)
print(f"Predicted price: ${predicted_price[0]:,.0f}")
# Model performance
score = model.score(X, prices)
print(f"R² Score: {score:.4f}")
2. Unsupervised Learning
Unsupervised learning works with unlabeled data. The model discovers hidden patterns and structures on its own.
Clustering
Grouping similar data points together:
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import numpy as np
# Generate sample customer data
# Features: [annual_income, spending_score]
np.random.seed(42)
customers = np.vstack([
np.random.randn(30, 2) * 0.5 + [30, 20], # Low income, low spending
np.random.randn(30, 2) * 0.5 + [80, 80], # High income, high spending
np.random.randn(30, 2) * 0.5 + [80, 20], # High income, low spending
np.random.randn(30, 2) * 0.5 + [30, 80], # Low income, high spending
])
# Apply K-Means clustering
kmeans = KMeans(n_clusters=4, random_state=42, n_init=10)
clusters = kmeans.fit_predict(customers)
# Analyze clusters
for i in range(4):
cluster_customers = customers[clusters == i]
avg_income = cluster_customers[:, 0].mean()
avg_spending = cluster_customers[:, 1].mean()
print(f"Cluster {i+1}: Avg Income=${avg_income:.0f}k, Avg Spending={avg_spending:.0f}")
Dimensionality Reduction
Reducing the number of features while preserving important information:
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
# Load high-dimensional data
iris = load_iris()
X = iris.data # 4 features
print(f"Original dimensions: {X.shape[1]}")
# Reduce to 2 dimensions
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print(f"Reduced dimensions: {X_reduced.shape[1]}")
# How much variance is preserved?
explained_var = pca.explained_variance_ratio_
print(f"Variance preserved: {sum(explained_var):.2%}")
# Output: ~97.77% of variance preserved with just 2 features
Association Rules
Finding relationships between items:
# Market Basket Analysis (simplified)
from collections import defaultdict
transactions = [
['bread', 'milk', 'eggs'],
['bread', 'butter', 'milk'],
['bread', 'milk', 'butter', 'eggs'],
['milk', 'butter'],
['bread', 'milk'],
['bread', 'butter', 'eggs'],
]
# Count item pairs
pair_counts = defaultdict(int)
item_counts = defaultdict(int)
total = len(transactions)
for transaction in transactions:
for item in transaction:
item_counts[item] += 1
for i in range(len(transaction)):
for j in range(i+1, len(transaction)):
pair = tuple(sorted([transaction[i], transaction[j]]))
pair_counts[pair] += 1
# Calculate support and confidence
print("Association Rules (Bread → Milk):")
support_bread = item_counts['bread'] / total
support_both = pair_counts[('bread', 'milk')] / total
confidence = support_both / support_bread
print(f"Support: {support_both:.2%}")
print(f"Confidence: {confidence:.2%}")
3. Reinforcement Learning
Reinforcement Learning (RL) trains an agent to make decisions by interacting with an environment. The agent learns through trial and error, receiving rewards or penalties.
How It Works
Agent observes state → Takes action → Receives reward → Updates policy
import numpy as np
class SimpleQLearning:
"""Q-Learning agent for a simple grid world"""
def __init__(self, states, actions, learning_rate=0.1, discount=0.99):
self.q_table = np.zeros((states, actions))
self.lr = learning_rate
self.discount = discount
def choose_action(self, state, epsilon=0.1):
"""Epsilon-greedy: explore vs exploit"""
if np.random.random() < epsilon:
return np.random.randint(self.q_table.shape[1]) # Explore
return np.argmax(self.q_table[state]) # Exploit
def update(self, state, action, reward, next_state):
"""Update Q-value using Bellman equation"""
best_next = np.max(self.q_table[next_state])
current_q = self.q_table[state, action]
new_q = current_q + self.lr * (reward + self.discount * best_next - current_q)
self.q_table[state, action] = new_q
# Simple grid world: 5 states, 2 actions (left/right)
# Goal: reach state 4 (reward=1)
agent = SimpleQLearning(states=5, actions=2)
# Training loop
for episode in range(1000):
state = 0 # Start at state 0
for step in range(10):
action = agent.choose_action(state)
next_state = min(state + (1 if action == 1 else -1), 4)
next_state = max(next_state, 0)
reward = 1 if next_state == 4 else -0.01
agent.update(state, action, reward, next_state)
state = next_state
if state == 4:
break
# Test learned policy
print("Learned Q-values:")
print(agent.q_table)
print(f"Optimal action from state 0: {'Right' if agent.choose_action(0, epsilon=0) == 1 else 'Left'}")
Comparison Summary
| Aspect | Supervised | Unsupervised | Reinforcement |
|---|---|---|---|
| Data | Labeled | Unlabeled | Rewards/Penalties |
| Goal | Predict outcomes | Discover patterns | Maximize rewards |
| Examples | Classification, Regression | Clustering, PCA | Game AI, Robotics |
| Libraries | scikit-learn, XGBoost | scikit-learn, UMAP | Stable-Baselines3, RLlib |
| Difficulty | Moderate | Moderate | Advanced |
| Data Needs | Labeled data (expensive) | Raw data (cheap) | Environment simulator |
Semi-Supervised and Self-Supervised Learning
Modern ML also includes hybrid approaches:
# Semi-supervised learning: use a small labeled dataset + large unlabeled
from sklearn.semi_supervised import SelfTrainingClassifier
from sklearn.svm import SVC
# 5 labeled + 20 unlabeled samples
X_labeled = [[1, 2], [2, 1], [3, 3], [8, 7], [7, 8]]
y_labeled = [0, 0, 0, 1, 1]
X_unlabeled = [[1.5, 1.5], [2.5, 2.5], [7.5, 7.5], [8.5, 8.5]] * 5
X_semi = X_labeled + X_unlabeled
y_semi = y_labeled + [-1] * len(X_unlabeled) # -1 = unlabeled
# Self-training classifier
stc = SelfTrainingClassifier(SVC(kernel='rbf', probability=True))
stc.fit(X_semi, y_semi)
# Predict with more accuracy than using just 5 labeled samples
test_points = [[2, 2], [7, 7]]
predictions = stc.predict(test_points)
print(f"Semi-supervised predictions: {predictions}")
Choosing the Right Type
- Have labeled data? → Supervised Learning
- Want to find groups/patterns? → Unsupervised Learning
- Need to make sequential decisions? → Reinforcement Learning
- Limited labels? → Semi-Supervised Learning
Frequently Asked Questions
What is the most common type of machine learning?
Supervised learning is the most widely used type of ML in industry, as most business problems involve predicting outcomes from historical labeled data.
Can I combine different types of ML?
Yes! Modern ML systems often combine approaches. For example, a self-driving car uses supervised learning for image recognition, unsupervised learning for scene understanding, and reinforcement learning for driving decisions.
What is the difference between supervised and unsupervised learning?
Supervised learning requires labeled data (input-output pairs) to train, while unsupervised learning works with unlabeled data and discovers hidden patterns on its own.
Ready to practice ML? Join our Telegram Community for daily ML coding challenges, project ideas, and peer learning. 100+ developers are learning together!