Machine learning model 1 : linear regression (and SVM)

May, 2022 - François HU

Master of Science - EPITA

This lecture is available here: https://curiousml.github.io/

image.png

Table of contents

Application 1: Linear regression

Let us implement a linear regression from scratch using Python. Linear regression is one of the most basic and commonly used type of predictive analysis.

Simple linear regression

$$ \begin{bmatrix} \hat y_1\\ \hat y_2\\ \vdots\\ \hat y_n \end{bmatrix} = \begin{bmatrix} \beta_0 + x_1\beta_1\\ \beta_0 + x_2\beta_1\\ \vdots\\ \beta_0 + x_n\beta_1 \end{bmatrix} \iff \underbrace{ \quad \begin{bmatrix} \hat y_1\\ \hat y_2\\ \vdots\\ \hat y_n \end{bmatrix} = \begin{bmatrix} 1 &x_1\\ 1 &x_2\\ \vdots&\vdots\\ 1 &x_n \end{bmatrix} \times \begin{bmatrix} \beta_0\\ \beta_1 \end{bmatrix} \quad}_{\\\boxed{\hat y = X\beta^T}} $$

Multiple linear regression

In multiple linear regression, $$ X = \begin{bmatrix} \mathbb{1}, X_{\cdot 1}, X_{\cdot 2}, \cdots, X_{\cdot m} \end{bmatrix} = \begin{bmatrix} 1 & x_{1,1} & x_{1,2} & \cdots & x_{1,m}\\ 1 & x_{2,1} & x_{2,2} & \cdots & x_{2,m}\\ 1 & & & \vdots & \\ 1 & x_{n,1} & x_{n,2} & \cdots & x_{n,m}\\ \end{bmatrix} \text{ and } \hat y = \begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix} $$

the estimation becomes:

$$ \hat y = \beta_0\times \mathbb{1} + \beta_1 X_{\cdot 1} + \beta_2 X_{\cdot 2} + \cdots + \beta_m X_{\cdot m} \iff \boxed{\hat y = X\beta^T} $$

with $\beta = [\beta_0, \beta_1, \cdots, \beta_m]\in\mathbb{R}^{m+1}$ the model parameters (or coefficients).

We are trying to calibrate our parameter $\beta$ such that $\boxed{\hat y \approx \mathbf{y}}$

Diabetes dataset

We consider the diabete dataset. More specifically we choose to study three variables:

Question 1:

Plot the following three graphs:

image-4.png

Prediction

Given a parameter $\beta$ and a set of observations $x$, its predicted value is:

$$ \hat y = x \beta^T $$

Question 2:

Define the function predict(X, beta) that returns the prediction $\hat y$.

Training / optimization

We want to find the best paramater $\hat\beta = [\hat\beta_0, \hat\beta_1, \hat\beta_2] \in\mathbb{R}^3$ that minimizes a (loss) function. For instance, the mean absolute error (MAE) function,

$$ \min\limits_{\beta\in\mathbb{R}^3} \dfrac{1}{n}\sum\limits_{i=1}^{n}\left| y_i - \hat y_i \right| $$

or a mean squared error (MSE) function, $$ \min\limits_{\beta\in\mathbb{R}^3} \dfrac{1}{n}\sum\limits_{i=1}^{n}\left( y_i - \hat y_i \right)^2 \quad\text{ equivalently }\quad \min\limits_{\beta\in\mathbb{R}^3} \left\lvert\left\lvert\ \mathbf{y} - \hat y\ \right\lvert\right\lvert^2 $$

Question 3:

Define the function mse(y_hat, y) that returns the MSE of $\hat y$ and $\mathbf{y}$.

Given a dataset $(X, y)$, optimization algorithms are used to find an optimal set of parameters (e.g. $\hat\beta_0$ and $\hat\beta_1$) that minimizes a loss function (e.g. MSE).

Question 4:

Question 5:

A more direct method

The best paramater $\hat\beta \in\mathbb{R}^d$ that minimizes a mean squared error (MSE) function,

$$ \min\limits_{\beta\in\mathbb{R}^d} \left\lvert\left\lvert\ \mathbf{y} - X\beta^T\ \right\lvert\right\lvert^2 $$

Can easily be found anaytically: $$ \min\left\lvert\left\lvert\ \mathbf{y} - X\beta^T\ \right\lvert\right\lvert^2 \iff {\displaystyle ( {X} ^{\mathsf {T}} {X} ){\hat { {\beta }}}= {X} ^{\mathsf {T}}\mathbf {y}} \iff \boxed{{\widehat {\beta }}=(X^{T}X)^{-1}X^{T}\mathbf{y}} $$

Question 6:

Implement this solution and compare it with the parameter found in question 5.

Application 2: Ridge regression (lecture 4 or 5)

Soon available ...

Application 3: SVD and PCA (lecture 4)

Soon available ...

(optional) Application 4: Support Vector Machines (SVM)

SVM: Visualization and intuition

Notes:

Optimization problem (Tikhonov version)

The SVM prediction function is the solution of $$ \min\limits_{w\in\mathbb{R}^d, b\in\mathbb{R}} \left\{ \frac{1}{2}\lvert\lvert w \lvert\lvert^2 + \frac{c}{2}\sum\limits_{i=1}^{n}\max(0, 1-y_i(w^Tx_i+b)) \right\} $$

Unconstraint to constraint optimization

The SVM optimization is equivalent to: \begin{align*} \min \quad & \dfrac{1}{2}\lvert\lvert w \lvert\lvert^2 + \dfrac{c}{n}\sum\limits_{i=1}^{n} \xi \\ \text{subject to} \quad & \xi_i \geq \max(0, 1-y_i(w^Tx_i+b)) \quad \text{for all i} \end{align*}

Equivalently \begin{align*} \min \quad & \dfrac{1}{2}\lvert\lvert w \lvert\lvert^2 + \dfrac{c}{n}\sum\limits_{i=1}^{n} \xi_i \\ \text{subject to} \quad & -\xi_i \leq 0\quad \text{for all i}\\ & 1-y_i(w^Tx_i+b)-\xi_i \leq 0 \quad \text{for all i} \end{align*}

Lagrange multipliers

Therefore the primal problem:

\begin{align*} \min \quad & \dfrac{1}{2}\lvert\lvert w \lvert\lvert^2 + \dfrac{c}{n}\sum\limits_{i=1}^{n} \xi_i \\ \text{subject to} \quad & -\xi_i \leq 0\quad \text{for all i}\\ & 1-y_i(w^Tx_i+b)-\xi_i \leq 0 \quad \text{for all i} \end{align*}

and the associated Langrangian is: \begin{align*} \mathcal{L}(w, b, \xi, \alpha, \lambda) &= \frac{1}{2}\lvert\lvert w \lvert\lvert^2 + \dfrac{c}{n}\sum\limits_{i=1}^{n}\xi_i + \sum\limits_{i=1}^{n}\alpha_i (1-y_i(w^Tx_i+b)-\xi_i) - \sum\limits_{i=1}^{n} \lambda_i\xi_i\\ & = \frac{1}{2} w^Tw + \sum\limits_{i=1}^{n}\xi_i\left( \frac{c}{n}-\alpha_i-\lambda_i \right) + \sum\limits_{i=1}^{n}\alpha_i \left( 1-y_i(w^Tx_i+b) \right) \end{align*}

(Optional) Questions

Application 5: Titanic challenge

Titanic challenge