Lecture 2: Supervised Machine Learning

Applied Machine Learning

Volodymyr Kuleshov
Cornell Tech

Recall: Supervised Learning

The most common approach to machine learning is supervised learning.

  1. First, we collect a dataset of labeled training examples.
  2. We train a model to output accurate predictions on this dataset.
  3. When the model sees new, similar data, it will also be accurate.

Part 1: A First Supervised Machine Learning Problem

Let’s start with a simple example of a supervised learning problem: predicting diabetes risk.

Suppose we have a dataset of diabetes patients.

Three Components of A Supervised Machine Learning Problem

At a high level, a supervised machine learning problem has the following structure:

$$ \text{Dataset} + \text{Algorithm} \to \text{Predictive Model} $$

The predictive model is chosen to model the relationship between inputs and targets. For instance, it can predict future targets.

A Supervised Learning Dataset

Let's return to our example: predicting diabates risk. What would a dataset look like?

We will use the UCI Diabetes Dataset; it's a toy dataset that's often used to demonstrate machine learning algorithms.

We can also visualize this two-dimensional dataset.

A Supervised Learning Algorithm (Part 1)

What is the relationship between BMI and diabetes risk?

We could assume that risk is a linear function of BMI. In other words, for some unknown $\theta_0, \theta_1 \in \mathbb{R}$, we have $$ y = \theta_1 \cdot x + \theta_0, $$ where $x$ is the BMI (also called the dependent variable), and $y$ is the diabetes risk score (the independent variable).

Note that $\theta_1, \theta_0$ are the slope and the intercept of the line relates $x$ to $y$. We call them parameters.

We can visualize this for a few values of $\theta_1, \theta_0$.

A Supervised Learning Algorithm (Part 2)

Assuming that $x,y$ follow the above linear relationship, the goal of the supervised learning algorithm is to find a good set of parameters consistent with the data.

We will see many algorithms for this task. For now, let's call the sklearn.linear_model library to find a $\theta_1, \theta_0$ that fit the data well.

A Supervised Learning Model

The supervised learning algorithm gave us a pair of parameters $\theta_1^*, \theta_0^*$. These define the predictive model $f^*$, defined as $$ f(x) = \theta_1^* \cdot x + \theta_0^*, $$ where again $x$ is the BMI, and $y$ is the diabetes risk score.

We can visualize the linear model that fits our data.

Predictions Using Supervised Learning

Given a new dataset of patients with a known BMI, we can use this model to estimate their diabetes risk.

Given a new $x'$, we can output a predicted $y'$ as $$ y' = f(x') = \theta_1^* \cdot x' + \theta_0. $$

Let's start by loading more data. We will load three new patients (shown in red below) that we haven't seen before.

Our linear model provides an estimate of the diabetes risk for these patients.

Why Supervised Learning?

Supervised learning can be useful in many ways.

Applications of Supervised Learning

Many of the most important applications of machine learning are supervised:

Part 2: Anatomy of a Supervised Learning Problem: Datasets

We have seen a simple example of a supervised machine learning problem and an algorithm for solving this problem.

Let's now look at what a general supervised learning problem looks like.

Recall: Three Components of A Supervised Machine Learning Problem

At a high level, a supervised machine learning problem has the following structure:

$$ \text{Dataset} + \text{Algorithm} \to \text{Predictive Model} $$

The predictive model is chosen to model the relationship between inputs and targets. For instance, it can predict future targets.

A Supervised Learning Dataset

We are going to dive deeper into what's a supervised learning dataset. As an example, consider the full version of the UCI Diabetes Dataset seen earlier.

Previsouly, we only looked at the patients' BMI, but this dataset actually records many additional measurements.

The UCI dataset contains many additional data columns besides bmi, including age, sex, and blood pressure. We can ask sklearn to give us more information about this dataset.

A Supervised Learning Dataset: Notation

We say that a training dataset of size $n$ (e.g., $n$ patients) is a set $$\mathcal{D} = \{(x^{(i)}, y^{(i)}) \mid i = 1,2,...,n\}$$

Each $x^{(i)}$ denotes an input (e.g., the measurements for patient $i$), and each $y^{(i)} \in \mathcal{Y}$ is a target (e.g., the diabetes risk).

Together, $(x^{(i)}, y^{(i)})$ form a training example.

We can look at the diabetes dataset in this form.

Training Dataset: Inputs

More precisely, an input $x^{(i)} \in \mathcal{X}$ is a $d$-dimensional vector of the form $$ x^{(i)} = \begin{bmatrix} x^{(i)}_1 \\ x^{(i)}_2 \\ \vdots \\ x^{(i)}_d \end{bmatrix}$$ For example, it could be the measurements the values of the $d$ features for patient $i$.

The set $\mathcal{X}$ is called the feature space. Often, we have, $\mathcal{X} = \mathbb{R}^d$.

Let's look at data for one patient.

Training Dataset: Attributes

We refer to the numerical variables describing the patient as attributes. Examples of attributes include:

Note that thes attributes in the above example have been mean-centered at zero and re-scaled to have a variance of one.

Training Dataset: Features

Often, an input object has many attributes, and we want to use these attributes to define more complex descriptions of the input.

We call these custom attributes features.

Let's create an "old man" feature.

Training Dataset: Features

More formally, we can define a function $\phi : \mathcal{X} \to \mathbb{R}^p$ that takes an input $x^{(i)} \in \mathcal{X}$ and outputs a $p$-dimensional vector $$ \phi(x^{(i)}) = \left[\begin{array}{@{}c@{}} \phi(x^{(i)})_1 \\ \phi(x^{(i)})_2 \\ \vdots \\ \phi(x^{(i)})_p \end{array} \right]$$ We say that $\phi(x^{(i)})$ is a featurized input, and each $\phi(x^{(i)})_j$ is a feature.

Features vs Attributes

In practice, the terms attribute and features are often used interchangeably. Most authors refer to $x^{(i)}$ as a vector of features (i.e., they've been precomputed).

We will follow this convention and use attribute only when there is ambiguity between features and attributes.

Features: Discrete vs. Continuous

Features can be either discrete or continuous. We will see later that they may be handled differently by ML algorthims.

The BMI feature that we have seen earlier is an example of a continuous feature.

We can visualize its distribution.

Other features take on one of a finite number of discrete values. The sex column is an example of a categorical feature.

In this example, the dataset has been pre-processed such that the two values happen to be 0.05068012 and -0.04464164.

Training Dataset: Targets

For each patient, we are interested in predicting a quantity of interest, the target. In our example, this is the patient's diabetes risk.

Formally, when $(x^{(i)}, y^{(i)})$ form a training example, each $y^{(i)} \in \mathcal{Y}$ is a target. We call $\mathcal{Y}$ the target space.

We plot the distirbution of risk scores below.

Targets: Regression vs. Classification

We distinguish between two broad types of supervised learning problems that differ in the form of the target variable.

  1. Regression: The target variable $y$ is continuous. We are fitting a curve in a high-dimensional feature space that approximates the shape of the dataset.
  2. Classification: The target variable $y$ is discrete. Each discrete value corresponds to a class and we are looking for a hyperplane that separates the different classes.

We can easily turn our earlier regression example into classification by discretizing the diabetes risk scores into high or low.

Let's try to generate predictions for this dataset.

Part 3: Anatomy of a Supervised Learning Problem: Learning Algorithm

Let's now look at what a general supervised learning algorithm looks like.

Recall: Three Components of A Supervised Machine Learning Problem

At a high level, a supervised machine learning problem has the following structure:

$$ \text{Dataset} + \text{Algorithm} \to \text{Predictive Model} $$

The predictive model is chosen to model the relationship between inputs and targets. For instance, it can predict future targets.

The Components of A Supervised Machine Learning Algorithm

We can also define the high-level structure of a supervised learning algorithm as consisting of three components:

Let's look again at our diabetes dataset for an example.

Model: Notation

We'll say that a model is a function $$ f : \mathcal{X} \to \mathcal{Y} $$ that maps inputs $x \in \mathcal{X}$ to targets $y \in \mathcal{Y}$.

Often, models have parameters $\theta \in \Theta$ living in a set $\Theta$. We will then write the model as $$ f_\theta : \mathcal{X} \to \mathcal{Y} $$ to denote that it's parametrized by $\theta$.

Model Class: Notation

Formally, the model class is a set $$\mathcal{M} \subseteq \{f \mid f : \mathcal{X} \to \mathcal{Y} \}$$ of possible models that map input features to targets.

When the models $f_\theta$ are paremetrized by parameters $\theta \in \Theta$ living in some set $\Theta$. Thus we can also write $$\mathcal{M} = \{f_\theta \mid f : \mathcal{X} \to \mathcal{Y}; \; \theta \in \Theta \}.$$

Model Class: Example

One simple approach is to assume that $x$ and $y$ are related by a linear model of the form \begin{align*} y & = \theta_0 + \theta_1 \cdot x_1 + \theta_2 \cdot x_2 + ... + \theta_d \cdot x_d \end{align*} where $x$ is a featurized output and $y$ is the target.

The $\theta_j$ are the parameters of the model.

Objectives: Notation

To capture this intuition, we define an objective function (also called a loss function) $$J(f) : \mathcal{M} \to [0, \infty), $$ which describes the extent to which $f$ "fits" the data $\mathcal{D} = \{(x^{(i)}, y^{(i)}) \mid i = 1,2,...,n\}$.

When $f$ is parametrized by $\theta \in \Theta$, the objective becomes a function $J(\theta) : \Theta \to [0, \infty).$

Objective: Examples

What would are some possible objective functions? We will see many, but here are a few examples:

These are defined for a dataset $\mathcal{D} = \{(x^{(i)}, y^{(i)}) \mid i = 1,2,...,n\}$.

Optimizer: Notation

At a high-level an optimizer takes an objective $J$ and a model class $\mathcal{M}$ and finds a model $f \in \mathcal{M}$ with the smallest value of the objective $J$.

\begin{align*} \min_{f \in \mathcal{M}} J(f) \end{align*}

Intuitively, this is the function that bests "fits" the data on the training dataset.

When $f$ is parametrized by $\theta \in \Theta$, the optimizer minimizes a function $J(\theta)$ over all $\theta \in \Theta$.

Optimizer: Example

We will see that behind the scenes, the sklearn.linear_models.LinearRegression algorithm optimizes the MSE loss.

\begin{align*} \min_{\theta \in \mathbb{R}} \frac{1}{2n} \sum_{i=1}^n \left( f_\theta(x^{(i)}) - y^{(i)} \right)^2 \end{align*}

We can easily measure the quality of the fit on the training set and the test set.

Let's run the above algorithm on our diabetes dataset.

The algorithm returns a predictive model. We can visualize its predictions below.

Summary: Components of A Supervised Machine Learning Problem

At a high level, a supervised machine learning problem has the following structure:

$$ \text{Dataset} + \underbrace{\text{Algorithm}}_\text{Model Class + Objective + Optimizer } \to \text{Predictive Model} $$

The predictive model is chosen to model the relationship between inputs and targets. For instance, it can predict future targets.

Notation: Feature Matrix

Suppose that we have a dataset of size $n$ (e.g., $n$ patients), indexed by $i=1,2,...,n$. Each $x^{(i)}$ is a vector of $d$ features.

Feature Matrix

Machine learning algorithms are most easily defined in the language of linear algebra. Therefore, it will be useful to represent the entire dataset as one matrix $X \in \mathbb{R}^{n \times d}$, of the form: $$ X = \begin{bmatrix} x^{(1)}_1 & x^{(2)}_1 & \ldots & x^{(n)}_1 \\ x^{(1)}_2 & x^{(2)}_2 & \ldots & x^{(n)}_2 \\ \vdots \\ x^{(1)}_d & x^{(2)}_d & \ldots & x^{(n)}_d \end{bmatrix}.$$

Similarly, we can vectorize the target variables into a vector $y \in \mathbb{R}^n$ of the form $$ y = \begin{bmatrix} x^{(1)} \\ x^{(2)} \\ \vdots \\ x^{(n)} \end{bmatrix}.$$