Part 1 - Introduction

What is learning?

Defining learning is a task many people have tried to do during our time. Some examples:

  • Behaviorism (Skinner, 1900-1950)

    • Learning is a long-term change in behavior due to experience.
  • Cognitivism (Gestalt School, 1920-):

    • Learning is an internal mental process that integrates new information into established mental frameworks and updates those frameworks over time.
  • Connectionism (Hebb, 1949):

    • Learning is a physical process in which neurons join by developing the synapses between them.

This also applies to the definition of machine learning:

  • Samuel (1959):

    • Machine learning is a field of study that gives computers the ability to learn without being explicitly programmed.
  • Mitchell (1997):

    • A computer program is said to learn from experience EE with respect to some class of tasks TT and performance measure PP, if its performance at tasks in TT, as measured by PP, improves with experience EE.
  • Jordan (2015):

    • It is one of today’s rapidly growing technical fields, lying at the intersection of computer science and statistics, and at the core of artificial intelligence and data science

Topics in Machine Learning

So we’ve seen that we can define learning in many different ways, and as such, there are several ways we can approach machine learning. Here are some of the topics we will cover:

  • Supervised Learning
  • Unsupervised Learning
  • Reinforcement Learning
  • Learning Theory

Supervised Learning

We have some sort of task, be it classification or regression, but we have training data that has both the input and the corresponding output.

We try to find the function(s) that best map the input to the output.

Simply, mathematically we can define it as:

f:XYf: X \mapsto Y

Where XX is the input space and YY is the output space, and we want to find the best function ff that maps XX to YY.

Unsupervised Learning

In the case that we do not have data that has both the input and the output, we can use unsupervised learning. The problem itself does not have to differ from a supervised learning problem, but the data we have does not have the output.

In this case, we try to find the structure in the data, and we can do this in several ways:

  • Density estimation:
    • Construct a probability model over the input space.
  • Clustering:
    • Discover groups of similar examples in the data.
  • Dimensionality reduction:
    • Project high-dim data to 2 or 3-dimensions (for visualization purposes).

Reinforcement Learning

This approach differs a bit from supervised and unsupervised learning. In this case, we have an agent that interacts with an environment, and the agent receives rewards or punishments based on its actions.

We make a sequence of actions, given the current states. E.g. a robot interacting with its environment. We aim to maximize the reward function over time. At some point, receive a reward or a punishment, actions may also affect future reward!

Learning Theory

But, why does machine learning even work in the first place?

From traditional learning theory we know that we have a so-called performance guarantee. The performance guarantee is a bound on the generalization error, which is the difference between the training error and the test error.

So, the question is not if it will work, it is how well will it work. So the questions we need to figure out are:

  • Which functions can be learned/represented?
  • How much data do we need?

Math Revision

Let’s quickly revise some math concepts that we will need in this course.

Linear Algebra

The definition of a vector space can be defined as:

The real vector space RN\mathbb{R}^N is a set with elements x=[x1,x2,,xN]T\mathbf{x} = [x_1, x_2, \ldots, x_N]^T where each xjRx_j \in \mathbb{R}. The elements x\mathbf{x} are called vectors. Which must satisfy the following properties:

  • Addition: If xRN\mathbf{x} \in \mathbb{R}^N and yRN\mathbf{y} \in \mathbb{R}^N, then x+y=[x1+y1,x2+y2,,xN+yN]TRN\mathbf{x} + \mathbf{y} = [x_1 + y_1, x_2 + y_2, \ldots, x_N + y_N]^T \in \mathbb{R}^N.

  • Scalar Product: If xRN\mathbf{x} \in \mathbb{R}^N and aRa \in \mathbb{R}, then ax=[ax1,ax2,,axN]TRNa\mathbf{x} = [ax_1, ax_2, \ldots, ax_N]^T \in \mathbb{R}^N.

  • Inner Product: If xRN\mathbf{x} \in \mathbb{R}^N and yRN\mathbf{y} \in \mathbb{R}^N, then xTy=j=1Nxjyj\mathbf{x}^T\mathbf{y} = \sum_{j=1}^{N} x_jy_j.

The definition of a matrix can be defined as:

A matrix XRM×N\mathbf{X} \in \mathbb{R}^{M \times N} is rectangular array of elements xijRx_{ij} \in \mathbb{R}, 1iM1 \leq i \leq M, 1jN1 \leq j \leq N.

X=[x11x12x1Nx21x22x2NxM1xM2xMN]\mathbf{X} = \begin{bmatrix} x_{11} & x_{12} & \ldots & x_{1N} \newline x_{21} & x_{22} & \ldots & x_{2N} \newline \vdots & \vdots & \ddots & \vdots \newline x_{M1} & x_{M2} & \ldots & x_{MN} \end{bmatrix}

A matrix XRM×N\mathbf{X} \in \mathbb{R}^{M \times N} supports the following operations:

  • Addition: If XRM×N\mathbf{X} \in \mathbb{R}^{M \times N} and YRM×N\mathbf{Y} \in \mathbb{R}^{M \times N}, then Z=X+YRM×N\mathbf{Z} = \mathbf{X} + \mathbf{Y} \in \mathbb{R}^{M \times N} where zij=xij+yijz_{ij} = x_{ij} + y_{ij}.
  • Scalar Product: If XRM×N\mathbf{X} \in \mathbb{R}^{M \times N} and aRa \in \mathbb{R}, then Y=aXRM×N\mathbf{Y} = a\mathbf{X} \in \mathbb{R}^{M \times N} where yij=axijy_{ij} = ax_{ij}.
  • Matrix Product: If XRM×N\mathbf{X} \in \mathbb{R}^{M \times N} and YRN×P\mathbf{Y} \in \mathbb{R}^{N \times P}, then Z=XYRM×P\mathbf{Z} = \mathbf{X}\mathbf{Y} \in \mathbb{R}^{M \times P} where zij=k=1Nxikykjz_{ij} = \sum_{k=1}^{N} x_{ik}y_{kj}.

Probability Theory

Let’s start with the definition of a probability distribution:

A probability distribution pp over a sample space Ω\Omega is a function from elements/subsets of Ω\Omega to the real numbers, that satisfies the following conditions:

  • Non-negativity: p(ω)0p(\omega) \geq 0 for all ωΩ\omega \subseteq \Omega.
  • Normalization: ωΩp(ω)=1\sum_{\omega \in \Omega} p(\omega) = 1.
  • Additivity: For all ω\omega, ωΩ\omega^\prime \subseteq \Omega that are disjoint sets, p(ωω)=p(ω)+p(ω)p(\omega \bigcup \omega^\prime) = p(\omega) + p(\omega^\prime).

Let’s define a random variable now:

A random variable XX is defined by a function fxf_x that maps each element ω\omega of the sample space Ω\Omega to a value x=fx(ω)x = f_x(\omega) in a set χ\chi (called the range of the random variable).

For each xχx \in \chi, the event {X=x}\{X = x\} refers to the subset of the sample space {ωωΩ,fx(ω)=x}\{\omega | \omega \in \Omega, f_x(\omega) = x\}.

For each xχx \in \chi, the probability p(X=x)=p(ωωΩ,fx(ω)=x)p(X = x) = p({\omega | \omega \in \Omega, f_x(\omega) = x}).

We can also specify a probability distribution for a random variable XX with range χ\chi directly instead of via an underlying sample space Ω\Omega.

The following conditions must hold:

  • Discrete probability mass function:
    • p(X=x)0xχp(X = x) \geq 0 \quad \forall x \in \chi and xχp(X=x)=1\sum_{x \in \chi} p(X = x) = 1.
  • Continuous probability density function:
    • p(X=x)0xχp(X = x) \geq 0 \quad \forall x \in \chi and χp(X=x)dx=1\int_{\chi} p(X = x)dx = 1.

Let’s now look at classification from a mathematical perspective.

The Classification Task

Definition 1 (Classification Task)

Given a feature vector xRN\mathbf{x} \in \mathbb{R}^N that describes an object that belongs to one of CC classes from the set Y\mathcal{Y}, predict which class the object belongs to.

A classical example is iris classification, where we have 3 classes of iris flowers. We’re given two features, the petal length and the sepal width, and we want to predict the class of the iris.

x=[petal lengthsepel width]=[x1x2]R2\mathbf{x} = \begin{bmatrix} \text{petal length} \newline \text{sepel width} \end{bmatrix} = \begin{bmatrix} x_1 \newline x_2 \end{bmatrix} \in \mathbb{R}^2 Y={"versicolor","setosa","virginica"},where Y=C=3\mathcal{Y} = \{ \text{"versicolor"}, \text{"setosa"}, \text{"virginica"} \}, \text{where } |\mathcal{Y}| = C = 3

or with numbers:

Y={0,1,2}\mathcal{Y} = \{ 0, 1, 2 \}

The Classifier Learning Problem

Definition 2 (Classifier Learning Problem)

Given a data set of example pairs D={(x(i),y(i)),i=1,,M}\mathcal{D} = \{ (\mathbf{x}^{(i)}, y^{(i)}), i = 1, \ldots, M \} where x(i)RN\mathbf{x}^{(i)} \in \mathbb{R}^N is a feature vector and y(i)Y={1,,C}y^{(i)} \in \mathcal{Y} = \{1, \ldots, C\} is the class label, learn a function f:RNYf: \mathbb{R}^N \mapsto \mathcal{Y} that accurately predicts the class label yy for any feature vector x\mathbf{x}.

Let’s define the indicator function:

I[A]={1if A is true0otherwise\mathbb{I}[A] = \begin{cases} 1 & \text{if } A \text{ is true} \newline 0 & \text{otherwise} \end{cases}

Classification Error and Accuracy

Definition 3 (Classification Error)

Given a data set of example pairs D={(x(i),y(i)),i=1,,M}\mathcal{D} = \{ (\mathbf{x}^{(i)}, y^{(i)}), i = 1, \ldots, M \} and a function f:RNYf: \mathbb{R}^N \mapsto \mathcal{Y}, the classification error rate of ff on D\mathcal{D} is defined as:

Err(f,D)=1Mi=1MI[y(i)f(x(i))]Err(f, \mathcal{D}) = \frac{1}{M} \sum_{i=1}^{M} \mathbb{I}[y^{(i)} \neq f(\mathbf{x}^{(i)})]
Definition 4 (Classification Accuracy)

Given a data set of example pairs D={(x(i),y(i)),i=1,,M}\mathcal{D} = \{ (\mathbf{x}^{(i)}, y^{(i)}), i = 1, \ldots, M \} and a function f:RNYf: \mathbb{R}^N \rightarrow \mathcal{Y}, the classification accuracy of ff on D\mathcal{D} is defined as:

Acc(f,D)=1Mi=1MI[y(i)=f(x(i))]=1Err(f,D)Acc(f, \mathcal{D}) = \frac{1}{M} \sum_{i=1}^{M} \mathbb{I}[y^{(i)} = f(\mathbf{x}^{(i)})] = 1 - Err(f, \mathcal{D})

K-Nearest Neighbors (KNN) Classifier

The KNN classifier is a simple classifier that classifies a new example based on the majority class of its kk nearest neighbors.

It stores the training set D\mathcal{D} and classifies each new instance x\mathbf{x} using a majority vote of its KK nearest neighbors.

NK(x){1,,M}\mathcal{N}_K(\mathbf{x}) \subset \{1, \ldots, M\}

Use of the KNN requires choosing the distance function d:RN×RNRd : \mathbb{R}^N \times \mathbb{R}^N \mapsto \mathbb{R} and the number of neighbors KK.

Max vs. Arg Max

The max operator of a function f(x)f(\mathbf{x}) defined over xD\mathbf{x} \in \mathcal{D} returns the maximum value of the function:

maxxf(x)={f(x)xDyD,f(y)f(x)}\max_{\mathbf{x}} f(\mathbf{x}) = \{f(\mathbf{x}) | \mathbf{x} \in \mathcal{D} \land \forall \mathbf{y} \in \mathcal{D}, f(\mathbf{y}) \leq f(\mathbf{x})\}

The arg max operator of a function f(x)f(\mathbf{x}) defined over xD\mathbf{x} \in \mathcal{D} gives the set of points which f(x)f(\mathbf{x}) reaches the maximum value:

argmaxxf(x)={xxDyD,f(y)f(x)}\underset{\mathbf{x}}{\arg\max} f(\mathbf{x}) = \{\mathbf{x} | \mathbf{x} \in \mathcal{D} \land \forall \mathbf{y} \in \mathcal{D}, f(\mathbf{y}) \leq f(\mathbf{x})\}

KNN Classification

fKNN(x)=argmaxc1,,CiNK(x)I[y(i)=c]f_{KNN}(\mathbf{x}) = \underset{c \in {1, \ldots, C}}{\arg\max} \sum_{i \in \mathcal{N}_K(\mathbf{x})} \mathbb{I}[y^{(i)} = c]

In general, KNN can work with any distance function dd satisfying non-negativity d(x,x)0d(\mathbf{x}, \mathbf{x}^\prime) \geq 0 and identity of indiscernibles d(x,x)=0d(\mathbf{x}, \mathbf{x}) = 0.

Genrally, the more structure the distance function has (symmetry, triangle inequality, etc.), the more strucutre you can exploit when designing algorithms.

The Minowski distance (p\ell_p norm) is defined as:

Given two data vectors x,xRN\mathbf{x}, \mathbf{x}^\prime \in \mathbb{R}^N, the Minowski distance with parameter pp (the p\ell_p norm) is a proper metric defined as:

dp(x,x)=xxp=(j=1Nxjxjp)1pd_p(\mathbf{x}, \mathbf{x}^\prime) = \lVert \mathbf{x} - \mathbf{x}^\prime \rVert_p \newline = \left( \sum_{j=1}^{N} |x_j - x^{\prime}_j|^p \right)^{\frac{1}{p}}

Special cases include, Euclidean distance (p=2p = 2), Manhattan distance (p=1p = 1), and Chebyshev distance (p=p = \infty).

Brute Force KNN

Given any distance function dd, brute force KNN works by computing the distance di=d(x(i),x)d_i = d(\mathbf{x}^{(i)}, \mathbf{x}) from a target point x\mathbf{x} to all of the training points x(i)\mathbf{x}^{(i)}.

Sort the distances {di,i=1,,M}\{d_i, i = 1, \ldots, M\} and choose the data cases with the KK smallest distances to form the neighbor index set NK(x)\mathcal{N}_K(\mathbf{x}).

Once the KK neighbors are selected, applying the classification rule is easy.

KNN Variants

Instead of giving all of the KK neighbors equal weight in the majority vote, a distance-weighted majority can be used:

fKNN(x)=argmaxc1,,CwiI[y(i)=c]wiwi=exp(αdi)f_{KNN}(\mathbf{x}) = \underset{c \in {1, \ldots, C}}{\arg\max} \frac{\sum w_i \mathbb{I}[y^{(i)} = c]}{\sum w_i} \newline w_i = exp(-\alpha d_i)

Instead of a brute force nearest neighbor search, data structures like KK-d trees can be constructed over the training data that support nearest neighbor search with lower computational complexity.

KNN Trade-offs

  • Advantages:
    • No training period is involved (i.e., lazy learning), and new data can be added seamlessly without re-training the model
    • Converges to the correct decision surface as data goes to infinity
  • Disadvantages:
    • Does not work well with large datasets. Since KNN needs to store all training data, performing neighbor search requires a lot of memory and takes a lot of time
    • Does not work well with high dimensions. It becomes difficult for KNN to calculate the distance in each dimension. Moreover, everything is far from everything else in high dimensions (the so-called “curse of dimensionality”)

Probabilistic Classifiers

Probabilistic classifiers are classifiers that output a probability distribution over the classes.

For example, generative models are probabilistic classifiers.

Each object/pattern has a particular (and possibly unique) probability distribution over the classes.

Class Model

As we’ve defined it before, possible classes are Y\mathcal{Y}. In the real world, the frequency that class yy occurs is given by the probabalistic distribution p(y)p(y).

p(y)p(y) is called the prior distribution.

Observation Model

We measure/observe feature vector x\mathbf{x}. The value of the features depends on the class.

The observation is drawn according to the distribution p(xy)p(\mathbf{x} | y).

p(xy)p(\mathbf{x} | y) is called the class conditional distribution. It indicates the probability of observing a particular feature vector x\mathbf{x} given that the class is yy.

Gaussian Distribution

Each class is modeled as a separate Gaussian distribution of the feature value.

p(xy=c;μc,σc2)=12πσc2exp((xμc)22σc2)p(x | y = c; \mu_c, \sigma_{c}^2) = \frac{1}{\sqrt{2\pi\sigma_c^2}} \exp \left( -\frac{(x - \mu_c)^2}{2\sigma_c^2} \right)

Each class has its own mean μc\mu_c and variance σc2\sigma_c^2.

Learn the Parameters from Data

Maximum likelihood estimation (MLE) is used to estimate the parameters of the Gaussian distribution.

Set the parameters (μc,σc2)(\mu_c, \sigma_c^2) to maximize the likelihood (probability) of the samples for class cc.

Let Dc={x(i)y(i)}\mathcal{D}_c = \{x^{(i)} | y^{(i)}\} for i=1,,Mci = 1, \ldots, M_c be the data for class cc.

The likelihood of the data is:

(μ^c,σ^c2)=argmaxμc,σc2logp(x(i)y(i);μc,σc2)(\hat{\mu}_c, \hat{\sigma}_c^2) = \underset{\mu_c, \sigma_c^2}{\arg\max} \sum \log p(x^{(i)} | y^{(i)}; \mu_c, \sigma_c^2)

When we view the above objective as a function of the parameters {μc,σc2}\{\mu_c, \sigma_c^2\}, we instead call it the likelihood function of the data.

Sample mean:

μ^c=1Mx(i)\hat{\mu}_c = \frac{1}{M} \sum x^{(i)}

Sample variance:

σ^c2=1Mc(x(i)μ^c)2\hat{\sigma}_c^2 = \frac{1}{M_c} \sum (x^{(i)} - \hat{\mu}_c)^2

Bayesian Decision Rule

The Bayesian decision rule (BDR) makes the optimal decisions on problems involving probability (uncertainty). It minimizes the probability of making a prediction error.

We call this the Bayes Optimal Classifier.

Given observation x\mathbf{x}, pick the class cc with the largest posterior probability p(y=cx)p(y = c | \mathbf{x}).

fB(x)=argmaxc{1,,C}p(y=cx)f_{B}(\mathbf{x}) = \underset{c \in \{1, \ldots, C\}}{\arg\max} p(y = c | \mathbf{x})

But there is a problem, we don’t have p(yx)p(y | \mathbf{x}), we only have p(y)p(y) and p(xy)p(\mathbf{x} | y).

Bayes Rule

The posterior probability can be calculated using Bayes rule:

p(yx)=p(xy)p(y)p(x)p(y | \mathbf{x}) = \frac{p(\mathbf{x} | y) p(y)}{p(\mathbf{x})}

The denominator is the probability of (x)\mathbf(x),

p(x)=yinYp(x,y)=yYp(xy)p(y)p(\mathbf{x}) = \sum_{y in \mathcal{Y}} p(\mathbf{x}, y) = \sum_{y \in \mathcal{Y}} p(\mathbf{x} | y) p(y)

The denominator makes p(yx)p(y | \mathbf{x}) sum to 1.

So, we can write it as:

p(yx)=p(xy)p(y)yYp(xy)p(y)p(y | \mathbf{x}) = \frac{p(\mathbf{x} | y) p(y)}{\sum_{y \in \mathcal{Y}} p(\mathbf{x} | y) p(y)}

Bayes Classifier Summary

For training, we collect training data from each class. For each class cc, we estimate the class conditional densities p(xy=c)p(x | y = c):

  1. Select a form of the distribution (e.g., Gaussian)
  2. Estimate its parameters with MLE
  3. Estimate the class priors p(y)p(y) using MLE

For classification, given a new sample x\mathbf{x}^\star, calculate the likelihood p(xy=c)p(\mathbf{x}^\star | y = c) for each class cc. Pick the class cc with the largest posterior probability p(y=cx)p(y = c | \mathbf{x}^\star).

Equivalently, use p(xy=c)p(y=c)p(\mathbf{x}^\star | y = c)p(y = c) or logp(xy=c)+logp(y=c)\log p(\mathbf{x}^\star | y = c) + \log p(y = c).