Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 16

Supervised Learning: Regression and Classification

This lesson is built with resources from youtube.com, aiperspectives.com and 1 more site.

Hello! Welcome to the first lesson of our third module, Fundamentals of Model Training.

In the previous module, you built a solid foundation in using Python's core data science libraries. You learned to load, manipulate, aggregate, and, most recently, visualize data using tools like Pandas, Matplotlib, and Seaborn. These skills in Exploratory Data Analysis (EDA) are crucial because they allow you to "listen" to your data. Now, we'll start teaching a computer to learn from it.

This lesson addresses the learning outcome: Explain the concept of supervised learning for regression and classification tasks. We'll explore the most common paradigm in machine learning, which forms the basis for many powerful AI systems, from predicting stock prices to powering the models behind generative AI.

1. From Explicit Rules to Learning from Data

As a software engineer, you're used to writing explicit instructions. If you were building a system to filter spam, you might write code like:

def is_spam(email):
    if "viagra" in email.subject or "free money" in email.body:
        return True
    # ... hundreds of other rules ...
    return False

This approach is brittle, hard to maintain, and will always miss new patterns. Machine learning, and specifically supervised learning, flips this on its head. Instead of writing the rules, you provide the data—thousands of emails pre-labeled as "spam" or "not spam"—and an algorithm learns the rules for you.

The core idea of supervised learning is to learn a mapping from inputs to outputs based on a set of correct examples. To get a formal definition, let's start with a short video.

Supervised vs. Unsupervised Learning

This video from IBM Technology provides a clear and concise introduction to supervised learning, contrasting it with its counterpart, unsupervised learning.

Watch the first 56 seconds of the video. Focus on the key definition: supervised learning uses labeled input and output data to train an algorithm.

The key takeaway is the concept of a labeled dataset. Each data point in our training set is a pair: (input, correct_output).

  • Input (Features): The data we use to make a prediction (e.g., the text of an email, the size of a house). Often denoted as .
  • Output (Label): The "correct answer" we want to predict (e.g., 'spam', the price of the house). Often denoted as .

The goal of the algorithm is to learn a function, let's call it , that can approximate the relationship between the inputs and outputs, such that . Once learned, this function can be used to make predictions on new, unseen data where we don't know the output.


2. Learning the Underlying Function

To make this more concrete, let's explore how an algorithm "learns" this function. We'll start with a simple, known function and then move to a more realistic example.

Supervised Learning - AI Perspectives

The article 'Supervised Learning - AI Perspectives' provides an excellent explanation of learning as function approximation. It starts with a simple temperature conversion example that illustrates the concept perfectly.

Read the following parts of the article: Start at '2.0 Overview' and read down to the Celsius/Fahrenheit example. This shows how a simple linear function can be represented by data. Continue with the house price prediction example ('Now, suppose we didn’t know the Fahrenheit function...'). Notice how the learned function is now an approximation of a more complex reality. Finally, read section '2.3 Origin of the term “supervised learning”'. This section contrasts machine learning with conventional programming and explains how the algorithm iteratively 'learns' from its errors, which is a powerful concept for someone with your background.

As the article highlights, this is a fundamental shift from traditional programming. In conventional programming, you provide the data and the rules, and the computer gives you answers. In supervised learning, you provide the data and the answers (labels), and the computer figures out the rules (the model or function). The "supervision" comes from the labeled data, which corrects the algorithm's predictions during training.


3. The Two Core Tasks: Classification and Regression

Supervised learning problems are generally broken down into two main categories, depending on the kind of output you want to predict.

  1. Classification: Predicts a discrete category or class label.
  2. Regression: Predicts a continuous numerical value.
Classification vs. Regression in Machine Learning
This image illustrates the fundamental difference. Classification is about assigning an item to a category (like 'Cat' or 'Dog'), while regression is about predicting a point on a continuous spectrum (like a house price).

Let's watch a short video that provides practical business examples for each.

Regression Vs Classification Vs Clustering Vs Time Series - Examples in Python [2022]

This video gives clear, business-oriented examples that distinguish regression from classification.

Watch the two sections on regression and classification: Regression Example (01:51 - 02:51): Notice how the goal is to predict a number (revenue). Classification Example (03:39 - 04:50): Notice how the goal is to predict a class ('good loan' or 'not good loan').

Let's formalize these concepts.

Classification: Predicting a Category

In a classification task, the model's job is to learn a decision boundary that separates the different classes in the data.

  • Binary Classification: There are only two possible outcomes. Examples:
    • Is this email spam or not spam?
    • Will this customer churn or not churn?
    • Is this transaction fraudulent or legitimate?
  • Multi-class Classification: There are more than two possible outcomes. Examples:
    • What digit is in this image (0, 1, 2, ..., 9)?
    • What is the topic of this news article (Sports, Politics, Technology)?
Classification vs. Regression Visual Comparison
The plot on the left visually represents a classification problem. The algorithm has learned a non-linear decision boundary to separate the two classes (green and teal dots).

Regression: Predicting a Number

In a regression task, the model's job is to learn a line or curve that best fits the continuous relationship between the input variables and the output.

  • Examples:
    • Predicting the price of a house based on its square footage, number of bedrooms, and location.
    • Forecasting a company's sales for the next quarter.
    • Estimating the amount of time it will take to complete a task.

The plot on the right of the image above shows a simple linear regression problem. The algorithm has learned a straight line that models the trend of the data points.

Supervised Learning - AI Perspectives

For a more detailed breakdown and a list of applications, let's refer back to 'Supervised Learning - AI Perspectives'.

Read section '2.8 Types of supervised learning'. This section formally defines regression and classification and provides a great list of real-world applications for supervised learning.

Test your understanding!

For each of the following scenarios, identify whether it is a classification or a regression problem.

  1. Predicting whether a server will fail in the next 24 hours (Yes/No).
  2. Estimating the number of bugs a new software update will introduce.
  3. Categorizing customer support tickets into 'Billing', 'Technical Issue', or 'General Inquiry'.
  4. Forecasting the daily active users for a mobile app.
  5. Identifying the sentiment of a user review (Positive, Negative, Neutral).
Show answer
  1. Classification (Binary: Yes/No).
  2. Regression (The output is a continuous number, e.g., 0, 1, 2, 3...).
  3. Classification (Multi-class: 'Billing', 'Technical Issue', 'General Inquiry').
  4. Regression (The output is a continuous number representing the count of users).
  5. Classification (Multi-class: 'Positive', 'Negative', 'Neutral').

Conclusion

In this lesson, you've taken the first crucial step into model training by understanding the paradigm of supervised learning. This conceptual framework is the bedrock for many of the models you will build throughout this course.

Key Takeaways:

  • Supervised learning is the process of training a model on a labeled dataset containing (input, output) pairs.
  • The goal is to learn a function that can generalize from the training data to make accurate predictions on new, unseen data.
  • This approach contrasts with traditional programming, where rules are hard-coded. Here, the algorithm learns the rules from data.
  • The two primary types of supervised learning are:
    • Classification: Predicting a discrete category (e.g., spam/not spam).
    • Regression: Predicting a continuous value (e.g., price, temperature).

Next up:
A model is only as good as the data it's trained on and, crucially, how it's evaluated. Simply testing a model on the same data it learned from can be misleading. In the next lesson, we will cover how to properly split a dataset into training, validation, and test sets. This is a fundamental practice to prevent data leakage and build models that perform well in the real world.