Types of Machine Learning

Three Main Types

Machine Learning can be categorized into three main types based on how the model learns:

1. Supervised Learning

Learning from labeled data where we know the correct answers.

How It Works

The algorithm learns from input-output pairs. Like a teacher supervising a student, we provide the correct answers during training.

Real-World Examples

Code Example: Classification

# Supervised Learning: Classifying Iris flowers
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# Load labeled data
iris = load_iris()
X = iris.data  # Features (sepal length, width, etc.)
y = iris.target  # Labels (species: setosa, versicolor, virginica)

# Split into training and testing
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Train the model
model = DecisionTreeClassifier()
model.fit(X_train, y_train)

# Test accuracy
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
# Output: Accuracy: 100.00%

# Predict a new flower
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
print(f"Species: {iris.target_names[prediction[0]]}")
# Output: Species: setosa

Types of Supervised Learning

2. Unsupervised Learning

Learning from unlabeled data to discover hidden patterns.

How It Works

The algorithm explores data without guidance, finding structure and patterns on its own.

Real-World Examples

Code Example: Clustering

# Unsupervised Learning: Customer Segmentation
from sklearn.cluster import KMeans
import numpy as np

# Customer data: [annual_income, spending_score]
customers = np.array([
    [15, 39], [16, 81], [17, 6], [18, 77], [19, 40],
    [20, 76], [21, 6], [22, 94], [23, 3], [24, 72],
    [25, 14], [26, 99], [27, 15], [28, 77], [29, 13]
])

# Cluster into 3 groups (no labels needed!)
kmeans = KMeans(n_clusters=3, random_state=42)
clusters = kmeans.fit_predict(customers)

print("Customer groups:", clusters)
# Output: Customer groups: [1 2 0 2 1 2 0 2 0 2 1 2 1 2 1]

# Group meanings:
# Group 0: Low income, low spending
# Group 1: Low/Medium income, medium spending
# Group 2: High income, high spending

Types of Unsupervised Learning

3. Reinforcement Learning

Learning through trial and error by receiving rewards or penalties.

How It Works

An agent learns to make decisions by interacting with an environment. It receives rewards for good actions and penalties for bad ones, like training a dog with treats.

Real-World Examples

Code Example: Simple Game

# Reinforcement Learning: Teaching an agent to play
import random

class SimpleGame:
    def __init__(self):
        self.position = 0
        self.goal = 10
    
    def step(self, action):
        # action: 0=left, 1=right
        if action == 1:
            self.position += 1
        else:
            self.position -= 1
        
        # Reward system
        if self.position == self.goal:
            return 100  # Big reward for reaching goal
        elif self.position < 0:
            return -10  # Penalty for going too far left
        else:
            return -1  # Small penalty for each step

# Simple learning agent
game = SimpleGame()
action_values = [0, 0]  # Values for [left, right]

# Training loop
for episode in range(100):
    game.position = 0
    for step in range(20):
        # Choose action (explore vs exploit)
        if random.random() < 0.1:
            action = random.randint(0, 1)
        else:
            action = 1 if action_values[1] > action_values[0] else 0
        
        reward = game.step(action)
        action_values[action] += 0.1 * reward
        
        if game.position == game.goal:
            break

print(f"Learned values: Left={action_values[0]:.1f}, Right={action_values[1]:.1f}")
# Output: Learned values: Left=-50.0, Right=200.0
# Agent learned that moving right is better!

Key Concepts

Quick Comparison

Type Data Goal Example
Supervised Labeled Predict labels Email classification
Unsupervised Unlabeled Find patterns Customer grouping
Reinforcement Reward signals Maximize reward Game playing