Chapter 1: Introduction to Machine Learning / Lesson 3

Types of Machine Learning

Types of Machine Learning

Machine learning can be categorized into three main types based on how the algorithm learns: Supervised Learning, Unsupervised Learning, and Reinforcement Learning. Understanding these types helps you choose the right approach for your problem.

Each type has different use cases, data requirements, and algorithms. Let's explore each one with examples.

1. Supervised Learning

Supervised learning uses labeled data—examples with known correct answers. The algorithm learns to map inputs to outputs by studying these labeled examples.

supervised_learning.py
# Supervised Learning: Learning from Labeled Data # Training data: (input, correct_output) labeled_examples = [ (1, 3), # Input 1 → Output 3 (2, 5), # Input 2 → Output 5 (3, 7), # Input 3 → Output 7 (4, 9) # Input 4 → Output 9 ] print("Supervised Learning Example:") print("Training Data (with labels):") for x, y in labeled_examples: print(f" Input: {x}, Label: {y}") print("\nThe algorithm learns: output = 2 * input + 1") print("Then can predict for new inputs!") # Common supervised learning tasks: supervised_tasks = { "Classification": "Predict categories (spam/not spam, cat/dog)", "Regression": "Predict numbers (house price, temperature)" } print("\nSupervised Learning Tasks:") for task, description in supervised_tasks.items(): print(f" {task}: {description}")

2. Unsupervised Learning

Unsupervised learning works with unlabeled data—no correct answers provided. The algorithm finds hidden patterns, groups similar items, or reduces data complexity.

unsupervised_learning.py
# Unsupervised Learning: Finding Patterns in Unlabeled Data # Unlabeled data - no correct answers! unlabeled_data = [ [1, 2], [1, 3], [5, 6], [5, 7], [10, 11], [10, 12] ] print("Unsupervised Learning Example:") print("Unlabeled Data (no correct answers):") for point in unlabeled_data: print(f" Point: {point}") print("\nAlgorithm discovers patterns:") print(" - Groups similar points together (clustering)") print(" - Finds hidden structure") print(" - Reduces dimensions") # Common unsupervised learning tasks: unsupervised_tasks = { "Clustering": "Group similar items (customer segments)", "Dimensionality Reduction": "Simplify data (visualization)", "Anomaly Detection": "Find unusual patterns (fraud detection)" } print("\nUnsupervised Learning Tasks:") for task, description in unsupervised_tasks.items(): print(f" {task}: {description}")

3. Reinforcement Learning

Reinforcement learning learns through trial and error, receiving rewards or penalties for actions. The algorithm learns to maximize rewards over time.

reinforcement_learning.py
# Reinforcement Learning: Learning from Rewards print("Reinforcement Learning Example:") print("Agent learns through trial and error") # Simplified RL scenario actions = ["move_left", "move_right", "move_up", "move_down"] rewards = { "move_right": 10, # Good action - high reward "move_up": 5, # OK action - medium reward "move_left": -5, # Bad action - negative reward "move_down": -10 # Very bad - large penalty } print("\nActions and Rewards:") for action, reward in rewards.items(): print(f" {action}: {reward:+d}") print("\nAgent learns to choose actions with highest rewards!") print("Over time, it discovers the best strategy.") # Common RL applications: rl_applications = [ "Game playing (Chess, Go, video games)", "Robotics (learning to walk, manipulate objects)", "Autonomous vehicles (learning to drive)", "Trading algorithms (learning investment strategies)" ] print("\nReinforcement Learning Applications:") for app in rl_applications: print(f" - {app}")

Comparing the Three Types

Here's a quick comparison to help you choose the right type:

comparing_types.py
# Comparing ML Types comparison = { "Supervised": { "data": "Labeled (input-output pairs)", "goal": "Learn mapping from input to output", "example": "Predict house price from features" }, "Unsupervised": { "data": "Unlabeled (just inputs)", "goal": "Find hidden patterns or structure", "example": "Group customers by behavior" }, "Reinforcement": { "data": "Actions and rewards", "goal": "Learn optimal actions to maximize reward", "example": "Game AI learning to play" } } print("Comparison of ML Types:") print("=" * 60) for ml_type, info in comparison.items(): print(f"\n{ml_type} Learning:") print(f" Data: {info['data']}") print(f" Goal: {info['goal']}") print(f" Example: {info['example']}")

Which Type Should You Use?

Choosing the right type depends on your data and problem:

  • Supervised: Use when you have labeled data and want to predict something specific
  • Unsupervised: Use when you have unlabeled data and want to discover patterns
  • Reinforcement: Use when you have an agent that can interact with an environment
choosing_type.py
# Decision Guide: Which ML Type? problems = { "Email spam detection": { "type": "Supervised", "reason": "Have labeled examples (spam/not spam)" }, "Customer segmentation": { "type": "Unsupervised", "reason": "No labels, want to find groups" }, "Chess AI": { "type": "Reinforcement", "reason": "Agent plays games, learns from wins/losses" } } print("Which ML Type for Each Problem?") for problem, info in problems.items(): print(f"\n{problem}:") print(f" Type: {info['type']}") print(f" Reason: {info['reason']}")

📝 Exercise: Practice All Three ML Types

Complete the exercises in the code editor on the right. You'll practice:

  • Exercise 1 (Supervised): Figure out the pattern from labeled examples and predict the output for input 5
  • Exercise 2 (Unsupervised): Identify groups of similar data points (clustering)
  • Exercise 3 (Reinforcement): Determine which action maximizes the reward

Hint for Exercise 1: Look at the relationship between input and output. For Exercise 2, notice which points have similar first coordinates. For Exercise 3, find the action with the highest reward value!

💡 Key Insight

Most ML projects use supervised learning because it's the most straightforward and effective for prediction tasks. Unsupervised learning is great for exploration, and reinforcement learning excels in interactive environments. Start with supervised learning—it's what we'll focus on in this course!

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...