1 Lecture 2: Supervised Machine Learning
1 Lecture 2: Supervised Machine Learning
Let’s start with a simple example of a supervised learning problem: predicting diabetes risk.
Suppose we have a dataset of diabetes patients. * For each patient we have a access to measurements
from their medical record and an estimate of diabetes risk. * We are interested in understanding
how the measurements affect an individual’s diabetes risk.
At a high level, a supervised machine learning problem has the following structure:
The predictive model is chosen to model the relationship between inputs and targets. For instance,
it can predict future targets.
1
5 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. * For each patient we have a access to a measurement of their body mass index
(BMI) and a quantiative diabetes risk score (from 0-400). * We are interested in understanding
how BMI affects an individual’s diabetes risk.
[2]: import numpy as np
import pandas as pd
from sklearn import datasets
diabetes_X = diabetes_X * 30 + 25
2
6 A Supervised Learning Algorithm (Part 1)
3
7 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 θ1 , θ0 that fit the data well.
[6]: from sklearn import linear_model
from sklearn.metrics import mean_squared_error
# The coefficients
print('Slope (theta1): \t', regr.coef_[0])
print('Intercept (theta0): \t', regr.intercept_)
The supervised learning algorithm gave us a pair of parameters θ1∗ , θ0∗ . These define the predictive
model f ∗ , defined as
f (x) = θ1∗ · x + θ0∗ ,
where again x is the BMI, and y is the diabetes risk score.
We can visualize the linear model that fits our data.
[7]: plt.xlabel('Body Mass Index (BMI)')
plt.ylabel('Diabetes Risk')
plt.scatter(diabetes_X_train, diabetes_y_train)
plt.plot(diabetes_X_train, diabetes_y_train_pred, color='black', linewidth=2)
4
9 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′ ) = θ1∗ · x′ + θ0 .
Let’s start by loading more data. We will load three new patients (shown in red below) that we
haven’t seen before.
[8]: # Collect 3 data points
diabetes_X_test = diabetes_X.iloc[:3]
diabetes_y_test = diabetes_y.iloc[:3]
plt.scatter(diabetes_X_train, diabetes_y_train)
plt.scatter(diabetes_X_test, diabetes_y_test, color='red')
plt.xlabel('Body Mass Index (BMI)')
plt.ylabel('Diabetes Risk')
plt.legend(['Initial patients', 'New patients'])
5
Our linear model provides an estimate of the diabetes risk for these patients.
[9]: # generate predictions on the new patients
diabetes_y_test_pred = regr.predict(diabetes_X_test)
6
10 Why Supervised Learning?
Supervised learning can be useful in many ways. * Making predictions on new data. * Understand-
ing the mechanisms through which input variables affect targets.
Many of the most important applications of machine learning are supervised: * Classifying medical
images. * Translating between pairs of languages. * Detecting objects in a self-driving car.
# 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.
At a high level, a supervised machine learning problem has the following structure:
The predictive model is chosen to model the relationship between inputs and targets. For instance,
it can predict future targets.
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.
[10]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [12, 4]
from sklearn import datasets
7
diabetes = datasets.load_diabetes(as_frame=True)
print(diabetes.DESCR)
.. _diabetes_dataset:
Diabetes dataset
----------------
Ten baseline variables, age, sex, body mass index, average blood
pressure, and six blood serum measurements were obtained for each of n =
442 diabetes patients, as well as the response of interest, a
quantitative measure of disease progression one year after baseline.
:Attribute Information:
- age age in years
- sex
- bmi body mass index
- bp average blood pressure
- s1 tc, T-Cells (a type of white blood cells)
- s2 ldl, low-density lipoproteins
- s3 hdl, high-density lipoproteins
- s4 tch, thyroid stimulating hormone
- s5 ltg, lamotrigine
- s6 glu, blood sugar level
Note: Each of these 10 feature variables have been mean centered and scaled by
the standard deviation times `n_samples` (i.e. the sum of squares of each column
totals 1).
Source URL:
https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html
8
14 A Supervised Learning Dataset: Notation
Each x(i) denotes an input (e.g., the measurements for patient i), and each y (i) ∈ 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.
[11]: # Load the diabetes dataset
diabetes_X, diabetes_y = diabetes.data, diabetes.target
s4 s5 s6
0 -0.002592 0.019908 -0.017646
1 -0.039493 -0.068330 -0.092204
2 -0.002592 0.002864 -0.025930
3 0.034309 0.022692 -0.009362
4 -0.002592 -0.031991 -0.046641
For example, it could be the measurements the values of the d features for patient i.
The set X is called the feature space. Often, we have, X = Rd .
Let’s look at data for one patient.
9
[12]: diabetes_X.iloc[0]
We refer to the numerical variables describing the patient as attributes. Examples of attributes
include: * The age of a patient. * The patient’s gender. * The patient’s BMI.
Note that thes attributes in the above example have been mean-centered at zero and re-scaled to
have a variance of one.
Often, an input object has many attributes, and we want to use these attributes to define more
complex descriptions of the input.
• Is the patient old and a man? (Useful if old men are at risk).
• Is the BMI above the obesity threshold?
We call these custom attributes features.
Let’s create an “old man” feature.
[13]: diabetes_X['old_man'] = (diabetes_X['sex'] > 0) & (diabetes_X['age'] > 0.05)
diabetes_X.head()
s4 s5 s6 old_man
0 -0.002592 0.019908 -0.017646 False
10
1 -0.039493 -0.068330 -0.092204 False
2 -0.002592 0.002864 -0.025930 True
3 0.034309 0.022692 -0.009362 False
4 -0.002592 -0.031991 -0.046641 False
More formally, we can define a function ϕ : X → Rp that takes an input x(i) ∈ X and outputs a
p-dimensional vector
ϕ(x(i) )1
ϕ(x(i) )2
ϕ(x(i) ) = ..
.
ϕ(x(i) )p
We say that ϕ(x(i) ) is a featurized input, and each ϕ(x(i) )j is a feature.
19 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 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.
[14]: diabetes_X.loc[:, 'bmi'].hist()
[14]: <AxesSubplot:>
11
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.
[15]: print(diabetes_X.loc[:, 'sex'].unique())
diabetes_X.loc[:, 'sex'].hist()
[ 0.05068012 -0.04464164]
[15]: <AxesSubplot:>
For each patient, we are interested in predicting a quantity of interest, the target. In our example,
this is the patient’s diabetes risk.
12
Formally, when (x(i) , y (i) ) form a training example, each y (i) ∈ Y is a target. We call Y the target
space.
We plot the distirbution of risk scores below.
[16]: plt.xlabel('Diabetes risk score')
plt.ylabel('Number of patients')
diabetes_y.hist()
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.
[17]: # Discretize the targets
diabetes_y_train_discr = np.digitize(diabetes_y_train, bins=[150])
# Visualize it
plt.scatter(diabetes_X_train[diabetes_y_train_discr==0],␣
,→diabetes_y_train[diabetes_y_train_discr==0], marker='o', s=80,␣
,→facecolors='none', edgecolors='g')
13
plt.scatter(diabetes_X_train[diabetes_y_train_discr==1],␣
,→diabetes_y_train[diabetes_y_train_discr==1], marker='o', s=80,␣
,→facecolors='none', edgecolors='r')
clf = linear_model.LogisticRegression()
# Visualize it
plt.scatter(diabetes_X_train[diabetes_y_train_discr==0],␣
,→diabetes_y_train[diabetes_y_train_discr==0], marker='o', s=140,␣
,→facecolors='none', edgecolors='g')
plt.scatter(diabetes_X_train[diabetes_y_train_discr==1],␣
,→diabetes_y_train[diabetes_y_train_discr==1], marker='o', s=140,␣
,→facecolors='none', edgecolors='r')
plt.scatter(diabetes_X_train[diabetes_y_train_pred==0],␣
,→diabetes_y_train[diabetes_y_train_pred==0], color='g', s=20)
plt.scatter(diabetes_X_train[diabetes_y_train_pred==1],␣
,→diabetes_y_train[diabetes_y_train_pred==1], color='r', s=20)
14
[18]: <matplotlib.legend.Legend at 0x11847d320>
At a high level, a supervised machine learning problem has the following structure:
The predictive model is chosen to model the relationship between inputs and targets. For instance,
it can predict future targets.
We can also define the high-level structure of a supervised learning algorithm as consisting of three
components: * A model class: the set of possible models we consider. * An objective function,
which defines how good a model is. * An optimizer, which finds the best predictive model in the
model class according to the objective function
Let’s look again at our diabetes dataset for an example.
[19]: import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [12, 4]
15
# Load the diabetes dataset
diabetes = datasets.load_diabetes(as_frame=True)
diabetes_X, diabetes_y = diabetes.data, diabetes.target
s4 s5 s6
0 -0.002592 0.019908 -0.017646
1 -0.039493 -0.068330 -0.092204
2 -0.002592 0.002864 -0.025930
3 0.034309 0.022692 -0.009362
4 -0.002592 -0.031991 -0.046641
25 Model: Notation
fθ : X → Y
16
27 Model Class: Example
One simple approach is to assume that x and y are related by a linear model of the form
y = θ0 + θ1 · x1 + θ2 · x2 + ... + θd · xd
17
28 Objectives: Notation
To capture this intuition, we define an objective function (also called a loss function)
which describes the extent to which f “fits” the data D = {(x(i) , y (i) ) | i = 1, 2, ..., n}.
When f is parametrized by θ ∈ Θ, the objective becomes a function J(θ) : Θ → [0, ∞).
29 Objective: Examples
What would are some possible objective functions? We will see many, but here are a few examples:
* Mean squared error:
1 ∑( )2
n
J(θ) = fθ (x(i) ) − y (i)
2n
i=1
y1 = np.array([1, 2, 3, 4])
y2 = np.array([-1, 1, 3, 5])
18
Mean squared error: 1.50
Mean absolute error: 1.00
30 Optimizer: Notation
At a high-level an optimizer takes an objective J and a model class M and finds a model f ∈ M
with the smallest value of the objective J.
min J(f )
f ∈M
Intuitively, this is the function that bests “fits” the data on the training dataset.
When f is parametrized by θ ∈ Θ, the optimizer minimizes a function J(θ) over all θ ∈ Θ.
31 Optimizer: Example
1 ∑( )2
n
min fθ (x(i) ) − y (i)
θ∈R 2n
i=1
We can easily measure the quality of the fit on the training set and the test set.
[59]: from sklearn.metrics import mean_squared_error
At a high level, a supervised machine learning problem has the following structure:
19
Dataset + Algorithm → Predictive Model
| {z }
Model Class + Objective + Optimizer
The predictive model is chosen to model the relationship between inputs and targets. For instance,
it can predict future targets.
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 ∈ Rn×d , of the
form: (1)
(2) (n)
x1 x1 . . . x1
(1) (2) (n)
x2 x2 . . . x2
X= . .
.
.
(1) (2) (n)
xd xd . . . xd
Similarly, we can vectorize the target variables into a vector y ∈ Rn of the form
(1)
x
x(2)
y = . .
.
.
x(n)
[ ]:
20