update of notes
@@ -134,7 +134,7 @@ With the OLS expressions for the parameters $\bm{\beta}$ we can evaluate the exp
|
||||
\]
|
||||
!et
|
||||
This means that the estimator of the regression parameters is unbiased.
|
||||
v
|
||||
|
||||
We can also calculate the variance
|
||||
|
||||
The variance of $\bm{\beta}$ is
|
||||
@@ -222,7 +222,7 @@ we discuss the
|
||||
o prediction error or simply the _test error_ $\mathrm{Err_{Test}}$, where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the
|
||||
o training error $\mathrm{Err_{Train}}$, which is the average loss over the training data.
|
||||
|
||||
As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
As our model becomes more and more complex, more of the training data tends to be used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
For a certain level of complexity the test error will reach minimum, before starting to increase again. The
|
||||
training error reaches a saturation.
|
||||
|
||||
@@ -362,7 +362,7 @@ estimate any statistics thereof. In principle you never draw the
|
||||
histogram of the relative frequency of $\widehat{\beta}^*$. Instead
|
||||
you use the estimators corresponding to the statistic of interest. For
|
||||
example, if you are interested in estimating the variance of $\widehat
|
||||
\beta$, apply the etsimator $\widehat \sigma^2$ to the values
|
||||
\beta$, apply the estimator $\widehat \sigma^2$ to the values
|
||||
$\widehat \beta^*$.
|
||||
|
||||
Before we proceed however, we need to remind ourselves about a central
|
||||
@@ -604,13 +604,13 @@ We can rewrite this as
|
||||
\]
|
||||
!et
|
||||
|
||||
The three terms represent the square of the bias of the learning
|
||||
The first term represents the square of the bias of the learning
|
||||
method, which can be thought of as the error caused by the simplifying
|
||||
assumptions built into the method. The second term represents the
|
||||
variance of the chosen model and finally the last terms is variance of
|
||||
the error $\bm{\epsilon}$.
|
||||
|
||||
To derive this equation, we need to recall that the variance of $\bm{y}$ and $\bm{\epsilon}$ are both equal to $\sigma^2$. The mean value of $\bm{\epsilon}$ is by definition equal to zero. Furthermore, the function $f$ is not a stochastics variable, idem for $\bm{\tilde{y}}$.
|
||||
To derive this equation, we need to recall that the variance of $\bm{y}$ and $\bm{\epsilon}$ are both equal to $\sigma^2$. The mean value of $\bm{\epsilon}$ is by definition equal to zero. Furthermore, the function $f$ is not a stochastic variable, idem for $\bm{\tilde{y}}$.
|
||||
We use a more compact notation in terms of the expectation value
|
||||
!bt
|
||||
\[
|
||||
@@ -1170,7 +1170,7 @@ plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
Note that we have kept the intercept in the first column of design matrix $\bm{X}$. When we call the corresponding _Scikit-Learn_ function we need thus to set the intercept to _False_. Libraries like _Scikit-Learn_ normally scale the design matrix and does not fit intercept. See the discussions below.
|
||||
Note that we have kept the intercept in the first column of design matrix $\bm{X}$. When we call the corresponding _Scikit-Learn_ function we need thus to set the intercept to _False_. Libraries like _Scikit-Learn_ normally scale the design matrix and do not fit intercept. See the discussions below.
|
||||
|
||||
===== More on Rescaling data =====
|
||||
|
||||
@@ -1472,7 +1472,7 @@ the way we treat the intercept may give a larger or smaller MSE,
|
||||
meaning that the MSE can be penalized by the value of the
|
||||
intercept. Not including the intercept in the fit, means that the
|
||||
regularization term does not include $\beta_0$. For different values
|
||||
of $\lambda$, this may lead to differeing MSE values.
|
||||
of $\lambda$, this may lead to different MSE values.
|
||||
|
||||
To remind the reader, the regularization term, with the intercept in Ridge regression, is given by
|
||||
!bt
|
||||
|
||||
@@ -222,7 +222,7 @@ p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
|
||||
!et
|
||||
Note that $1-p(t)= p(-t)$.
|
||||
|
||||
===== Examples of likelihood functions used in logistic regression and nueral networks =====
|
||||
===== Examples of likelihood functions used in logistic regression and neural networks =====
|
||||
|
||||
|
||||
The following code plots the logistic function, the step function and other functions we will encounter from here and on.
|
||||
|
||||
|
After Width: | Height: | Size: 27 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 294 KiB |
|
After Width: | Height: | Size: 106 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 264 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 19 KiB |
@@ -2,12 +2,32 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f63b8c1a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
|
||||
"doconce format html chapter4.do.txt -->"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a284cf5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Logistic Regression"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d78b73e2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Logistic Regression\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Logistic Regression\n",
|
||||
"\n",
|
||||
"In linear regression our main interest was centered on learning the\n",
|
||||
@@ -22,7 +42,6 @@
|
||||
"the product of the design matrices, linear regression gives then a\n",
|
||||
"simple recipe for fitting our data.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Classification problems, however, are concerned with outcomes taking\n",
|
||||
"the form of discrete variables (i.e. categories). We may for example,\n",
|
||||
"on the basis of DNA sequencing for a number of patients, like to find\n",
|
||||
@@ -39,7 +58,6 @@
|
||||
"binary outcome, true or false, positive or negative, success or\n",
|
||||
"failure etc.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Logistic regression will also serve as our stepping stone towards\n",
|
||||
"neural network algorithms and supervised deep learning. For logistic\n",
|
||||
"learning, the minimization of the cost function leads to a non-linear\n",
|
||||
@@ -52,10 +70,16 @@
|
||||
"\n",
|
||||
"We note also that many of the topics discussed here on logistic \n",
|
||||
"regression are also commonly used in modern supervised Deep Learning\n",
|
||||
"models, as we will see later.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"models, as we will see later."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d0c829b4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Basics\n",
|
||||
"\n",
|
||||
"We consider the case where the dependent variables, also called the\n",
|
||||
@@ -76,7 +100,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f1f4d5f2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n",
|
||||
@@ -85,7 +112,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "8e2f836b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Before moving to the logistic model, let us try to use our linear\n",
|
||||
"regression model to classify these two outcomes. We could for example\n",
|
||||
@@ -98,7 +128,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6b58f1ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- Equation labels as ordinary links -->\n",
|
||||
"<div id=\"_auto1\"></div>\n",
|
||||
@@ -113,12 +146,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "54e3604b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where $\\boldsymbol{y}$ is a vector representing the possible outcomes, $\\boldsymbol{X}$ is our\n",
|
||||
"$n\\times p$ design matrix and $\\boldsymbol{\\beta}$ represents our estimators/predictors.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The main problem with our function is that it takes values on the\n",
|
||||
"entire real axis. In the case of logistic regression, however, the\n",
|
||||
"labels $y_i$ are discrete variables. A typical example is the credit\n",
|
||||
@@ -134,13 +169,13 @@
|
||||
"favorable to use a ``soft\" classifier that outputs\n",
|
||||
"the probability of a given category. This leads us to the logistic function.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The following example on data for coronary heart disease (CHD) as function of age may serve as an illustration. In the code here we read and plot whether a person has had CHD (output = 1) or not (output = 0). This ouput is plotted the person's against age. Clearly, the figure shows that attempting to make a standard linear regression fit may not be very meaningful."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "cd1dd1c5",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -207,14 +242,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "2e06ee82",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"What we could attempt however is to plot the mean value for each group."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "a774f001",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -233,7 +272,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "e102fa56",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"We are now trying to find a function $f(y\\vert x)$, that is a function which gives us an expected value for the output $y$ with a given input $x$.\n",
|
||||
"In standard linear regression with a linear dependence on $x$, we would write this in terms of our model"
|
||||
@@ -241,7 +283,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "75d995ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"f(y_i\\vert x_i)=\\beta_0+\\beta_1 x_i.\n",
|
||||
@@ -250,7 +295,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "e2cacca5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"This expression implies however that $f(y_i\\vert x_i)$ could take any\n",
|
||||
"value from minus infinity to plus infinity. If we however let\n",
|
||||
@@ -261,9 +309,16 @@
|
||||
"us to a very popular model for the function $f$, namely the so-called\n",
|
||||
"Sigmoid function or logistic model. We will consider this function as\n",
|
||||
"representing the probability for finding a value of $y_i$ with a given\n",
|
||||
"$x_i$.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"$x_i$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "af9ac586",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## The logistic function\n",
|
||||
"\n",
|
||||
"Another widely studied model, is the so-called \n",
|
||||
@@ -281,7 +336,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "80aa3cbe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(t) = \\frac{1}{1+\\mathrm \\exp{-t}}=\\frac{\\exp{t}}{1+\\mathrm \\exp{t}}.\n",
|
||||
@@ -290,19 +348,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "28d49915",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Note that $1-p(t)= p(-t)$.\n",
|
||||
"\n",
|
||||
"## Examples of likelihood functions used in logistic regression and nueral networks\n",
|
||||
"\n",
|
||||
"Note that $1-p(t)= p(-t)$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eeff6cd9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Examples of likelihood functions used in logistic regression and neural networks\n",
|
||||
"\n",
|
||||
"The following code plots the logistic function, the step function and other functions we will encounter from here and on."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "5ecec05e",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -367,14 +436,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "5802efe3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"We assume now that we have two classes with $y_i$ either $0$ or $1$. Furthermore we assume also that we have only two parameters $\\beta$ in our fitting of the Sigmoid function, that is we define probabilities"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f85d14ea",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
@@ -386,7 +461,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6c4e0334",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where $\\boldsymbol{\\beta}$ are the weights we wish to extract from data, in our case $\\beta_0$ and $\\beta_1$. \n",
|
||||
"\n",
|
||||
@@ -395,7 +473,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "df7facc9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(y_i=0\\vert x_i, \\boldsymbol{\\beta}) = 1-p(y_i=1\\vert x_i, \\boldsymbol{\\beta}).\n",
|
||||
@@ -404,7 +485,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "a920e6d8",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In order to define the total likelihood for all possible outcomes from a \n",
|
||||
"dataset $\\mathcal{D}=\\{(y_i,x_i)\\}$, with the binary labels\n",
|
||||
@@ -416,7 +500,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "a780bfe1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
@@ -427,14 +514,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9f810a44",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"from which we obtain the log-likelihood and our **cost/loss** function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "3fac4ef4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left( y_i\\log{p(y_i=1|x_i,\\boldsymbol{\\beta})} + (1-y_i)\\log\\left[1-p(y_i=1|x_i,\\boldsymbol{\\beta}))\\right]\\right).\n",
|
||||
@@ -443,14 +536,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "75aa2c14",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Reordering the logarithms, we can rewrite the **cost/loss** function as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "780f2038",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
|
||||
@@ -459,7 +558,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c8c940aa",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to $\\beta$.\n",
|
||||
"Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that"
|
||||
@@ -467,7 +569,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9d4a527b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta})=-\\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
|
||||
@@ -476,23 +581,27 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c1706606",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"This equation is known in statistics as the **cross entropy**. Finally, we note that just as in linear regression, \n",
|
||||
"in practice we often supplement the cross-entropy with additional regularization terms, usually $L_1$ and $L_2$ regularization as we did for Ridge and Lasso regression.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The cross entropy is a convex function of the weights $\\boldsymbol{\\beta}$ and,\n",
|
||||
"therefore, any local minimizer is a global minimizer. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Minimizing this\n",
|
||||
"cost function with respect to the two parameters $\\beta_0$ and $\\beta_1$ we obtain"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "fe8fb387",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_0} = -\\sum_{i=1}^n \\left(y_i -\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right),\n",
|
||||
@@ -501,14 +610,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "bb564e81",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "700c5443",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_1} = -\\sum_{i=1}^n \\left(y_ix_i -x_i\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right).\n",
|
||||
@@ -517,7 +632,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f442c956",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Let us now define a vector $\\boldsymbol{y}$ with $n$ elements $y_i$, an\n",
|
||||
"$n\\times p$ matrix $\\boldsymbol{X}$ which contains the $x_i$ values and a\n",
|
||||
@@ -527,7 +645,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "83937f6f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = -\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{p}\\right).\n",
|
||||
@@ -536,7 +657,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "71819c1d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"If we in addition define a diagonal matrix $\\boldsymbol{W}$ with elements \n",
|
||||
"$p(y_i\\vert x_i,\\boldsymbol{\\beta})(1-p(y_i\\vert x_i,\\boldsymbol{\\beta})$, we can obtain a compact expression of the second derivative as"
|
||||
@@ -544,7 +668,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "23dfd975",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial^2 \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}\\partial \\boldsymbol{\\beta}^T} = \\boldsymbol{X}^T\\boldsymbol{W}\\boldsymbol{X}.\n",
|
||||
@@ -553,14 +680,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6ca9f74b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with $p$ predictors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "ddb3d93a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{ \\frac{p(\\boldsymbol{\\beta}\\boldsymbol{x})}{1-p(\\boldsymbol{\\beta}\\boldsymbol{x})}} = \\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p.\n",
|
||||
@@ -569,14 +702,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "0cfae560",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here we defined $\\boldsymbol{x}=[1,x_1,x_2,\\dots,x_p]$ and $\\boldsymbol{\\beta}=[\\beta_0, \\beta_1, \\dots, \\beta_p]$ leading to"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "7c427fc3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(\\boldsymbol{\\beta}\\boldsymbol{x})=\\frac{ \\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}{1+\\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}.\n",
|
||||
@@ -585,7 +724,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6a6814bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Till now we have mainly focused on two classes, the so-called binary\n",
|
||||
"system. Suppose we wish to extend to $K$ classes. Let us for the sake\n",
|
||||
@@ -594,7 +736,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "85282137",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=1\\vert x)}{p(K\\vert x)}} = \\beta_{10}+\\beta_{11}x_1,\n",
|
||||
@@ -603,14 +748,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "4864f76d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "390e9a55",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=2\\vert x)}{p(K\\vert x)}} = \\beta_{20}+\\beta_{21}x_1,\n",
|
||||
@@ -619,14 +770,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c3f39c44",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and so on till the class $C=K-1$ class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "92f32a03",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=K-1\\vert x)}{p(K\\vert x)}} = \\beta_{(K-1)0}+\\beta_{(K-1)1}x_1,\n",
|
||||
@@ -635,13 +792,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "677a5c4d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and the model is specified in term of $K-1$ so-called log-odds or\n",
|
||||
"**logit** transformations.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"In our discussion of neural networks we will encounter the above again\n",
|
||||
"in terms of a slightly modified function, the so-called **Softmax** function.\n",
|
||||
"\n",
|
||||
@@ -658,7 +816,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "fa70e3bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(C=k\\vert \\mathbf {x} )=\\frac{\\exp{(\\beta_{k0}+\\beta_{k1}x_1)}}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}}.\n",
|
||||
@@ -667,14 +828,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "4c59400c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"It is easy to extend to more predictors. The final class is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f4073652",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(C=K\\vert \\mathbf {x} )=\\frac{1}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}},\n",
|
||||
@@ -683,7 +850,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "810e872a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and they sum to one. Our earlier discussions were all specialized to\n",
|
||||
"the case with two classes only. It is easy to see from the above that\n",
|
||||
@@ -692,8 +862,16 @@
|
||||
"To find the optimal parameters we would typically use a gradient\n",
|
||||
"descent method. Newton's method and gradient descent methods are\n",
|
||||
"discussed in the material on [optimization\n",
|
||||
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html).\n",
|
||||
"\n",
|
||||
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f4ebc1da",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Wisconsin Cancer Data\n",
|
||||
"\n",
|
||||
"We show here how we can use a simple regression case on the breast\n",
|
||||
@@ -703,7 +881,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "b7a8cbac",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -739,7 +918,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "10f74b93",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In addition to the above scores, we could also study the covariance (and the correlation matrix).\n",
|
||||
"We use **Pandas** to compute the correlation matrix."
|
||||
@@ -747,7 +929,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "d0b8025d",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -792,7 +975,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9d84a2aa",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In the above example we note two things. In the first plot we display\n",
|
||||
"the overlap of benign and malignant tumors as functions of the various\n",
|
||||
@@ -811,7 +997,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"id": "6144ea0a",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -823,14 +1010,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "5627f5ae",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and then"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "a7c8662a",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -842,17 +1033,46 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "d03ee2a7",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Diagonalizing this matrix we can in turn say something about which\n",
|
||||
"features are of relevance and which are not. This leads us to\n",
|
||||
"the classical Principal Component Analysis (PCA) theorem with\n",
|
||||
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html))."
|
||||
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html)).\n",
|
||||
"\n",
|
||||
"Here we present a further way to present our results in terms of a so-called **confusion matrix**, the cumulative gain and the **ROC** curve.\n",
|
||||
"This way of displaying our data are based upon different ways to classify our possible outcomes. Before we proceed we need some definitions.\n",
|
||||
"1. **TP**: true positive or in other words, something equivalent with a proper classification\n",
|
||||
"\n",
|
||||
"2. **TN**: true negative, which is equivalent with a correct rejection\n",
|
||||
"\n",
|
||||
"3. **FP**: false positive, or in simpler words something that is equivalent with a false alarm\n",
|
||||
"\n",
|
||||
"4. **FN**: false negative, which is mean to be equivalent with a miss.\n",
|
||||
"\n",
|
||||
"The total data set is then the sum of the true positive and true negative targets or outputs, labeled by $n$.\n",
|
||||
"Based on this we can then define the accuracy score as the sum of correctly predicted **TP** and **TN** cases divided by the sum of true positive and treue negative events in our data set, or as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "44ce5095",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{Accuracy} = \\frac{\\sum_{i=0}^{n-1}I(y_i=\\tilde{y}_i)}{n}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 8,
|
||||
"id": "24ff3dd3",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -908,5 +1128,5 @@
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -499,7 +499,8 @@ const thebe_selector_output = ".output, .cell_output"
|
||||
|
||||
<div>
|
||||
|
||||
<div class="tex2jax_ignore mathjax_ignore section" id="resampling-methods">
|
||||
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||||
doconce format html chapter3.do.txt --><div class="tex2jax_ignore mathjax_ignore section" id="resampling-methods">
|
||||
<h1><span class="section-number">5. </span>Resampling Methods<a class="headerlink" href="#resampling-methods" title="Permalink to this headline">¶</a></h1>
|
||||
<div class="section" id="introduction">
|
||||
<h2><span class="section-number">5.1. </span>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
|
||||
@@ -625,9 +626,8 @@ mean value <span class="math notranslate nohighlight">\(\boldsymbol{X}\boldsymbo
|
||||
\[
|
||||
\mathbb{E}(\boldsymbol{\beta}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}.
|
||||
\]</div>
|
||||
<p>This means that the estimator of the regression parameters is unbiased.
|
||||
v
|
||||
We can also calculate the variance</p>
|
||||
<p>This means that the estimator of the regression parameters is unbiased.</p>
|
||||
<p>We can also calculate the variance</p>
|
||||
<p>The variance of <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> is</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[\begin{split}
|
||||
@@ -700,7 +700,7 @@ we discuss the</p>
|
||||
<li><p>prediction error or simply the <strong>test error</strong> <span class="math notranslate nohighlight">\(\mathrm{Err_{Test}}\)</span>, where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the</p></li>
|
||||
<li><p>training error <span class="math notranslate nohighlight">\(\mathrm{Err_{Train}}\)</span>, which is the average loss over the training data.</p></li>
|
||||
</ol>
|
||||
<p>As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
<p>As our model becomes more and more complex, more of the training data tends to be used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
For a certain level of complexity the test error will reach minimum, before starting to increase again. The
|
||||
training error reaches a saturation.</p>
|
||||
<p>Two famous
|
||||
@@ -760,10 +760,10 @@ number <span class="math notranslate nohighlight">\(i\)</span> is left out. Usin
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Runtime: 0.0903549 sec
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Runtime: 0.0893679 sec
|
||||
Jackknife Statistics :
|
||||
original bias std. error
|
||||
100.107 100.097 0.150184
|
||||
99.9524 99.9424 0.148854
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -828,7 +828,7 @@ estimate any statistics thereof. In principle you never draw the
|
||||
histogram of the relative frequency of <span class="math notranslate nohighlight">\(\widehat{\beta}^*\)</span>. Instead
|
||||
you use the estimators corresponding to the statistic of interest. For
|
||||
example, if you are interested in estimating the variance of <span class="math notranslate nohighlight">\(\widehat
|
||||
\beta\)</span>, apply the etsimator <span class="math notranslate nohighlight">\(\widehat \sigma^2\)</span> to the values
|
||||
\beta\)</span>, apply the estimator <span class="math notranslate nohighlight">\(\widehat \sigma^2\)</span> to the values
|
||||
<span class="math notranslate nohighlight">\(\widehat \beta^*\)</span>.</p>
|
||||
<p>Before we proceed however, we need to remind ourselves about a central
|
||||
theorem in statistics, namely the so-called <strong>central limit theorem</strong>.
|
||||
@@ -982,7 +982,7 @@ theorem.</p>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Bootstrap Statistics :
|
||||
original bias std. error
|
||||
99.9033 14.9678 99.904 0.151348
|
||||
100.188 15.1133 100.19 0.149655
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1004,7 +1004,7 @@ original bias std. error
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter3_47_0.png" src="_images/chapter3_47_0.png" />
|
||||
<img alt="_images/chapter3_51_0.png" src="_images/chapter3_51_0.png" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1036,12 +1036,12 @@ C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_
|
||||
\[
|
||||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
|
||||
\]</div>
|
||||
<p>The three terms represent the square of the bias of the learning
|
||||
<p>The first term represents the square of the bias of the learning
|
||||
method, which can be thought of as the error caused by the simplifying
|
||||
assumptions built into the method. The second term represents the
|
||||
variance of the chosen model and finally the last terms is variance of
|
||||
the error <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span>.</p>
|
||||
<p>To derive this equation, we need to recall that the variance of <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> are both equal to <span class="math notranslate nohighlight">\(\sigma^2\)</span>. The mean value of <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> is by definition equal to zero. Furthermore, the function <span class="math notranslate nohighlight">\(f\)</span> is not a stochastics variable, idem for <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span>.
|
||||
<p>To derive this equation, we need to recall that the variance of <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> are both equal to <span class="math notranslate nohighlight">\(\sigma^2\)</span>. The mean value of <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> is by definition equal to zero. Furthermore, the function <span class="math notranslate nohighlight">\(f\)</span> is not a stochastic variable, idem for <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span>.
|
||||
We use a more compact notation in terms of the expectation value</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
@@ -1124,7 +1124,7 @@ Var: 0.0010479245926411787
|
||||
0.013121574062587286 >= 0.012073649469946107 + 0.0010479245926411787 = 0.013121574062587286
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_61_1.png" src="_images/chapter3_61_1.png" />
|
||||
<img alt="_images/chapter3_65_1.png" src="_images/chapter3_65_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell docutils container">
|
||||
@@ -1216,7 +1216,9 @@ Error: 0.03781367141738902
|
||||
Bias^2: 0.03365768507152769
|
||||
Var: 0.0041559863458613296
|
||||
0.03781367141738902 >= 0.03365768507152769 + 0.0041559863458613296 = 0.03781367141738902
|
||||
Polynomial degree: 7
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 7
|
||||
Error: 0.027609773491022394
|
||||
Bias^2: 0.022999498260366198
|
||||
Var: 0.004610275230656182
|
||||
@@ -1253,7 +1255,7 @@ Var: 0.20867052175003364
|
||||
0.2284246870217162 >= 0.01975416527168255 + 0.20867052175003364 = 0.2284246870217162
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_62_2.png" src="_images/chapter3_62_2.png" />
|
||||
<img alt="_images/chapter3_66_3.png" src="_images/chapter3_66_3.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>The bias-variance tradeoff summarizes the fundamental tension in
|
||||
@@ -1378,7 +1380,7 @@ set, the higher, the less likely the model generalizes correctly from the
|
||||
training data.
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_64_1.png" src="_images/chapter3_64_1.png" />
|
||||
<img alt="_images/chapter3_68_1.png" src="_images/chapter3_68_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell docutils container">
|
||||
@@ -1466,9 +1468,7 @@ training data.
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 1
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Mean squared error on training data: 439230.69504801
|
||||
Mean squared error on training data: 439230.69504801
|
||||
Mean squared error on test data: 481979.17861098
|
||||
Degree of polynomial: 2
|
||||
Mean squared error on training data: 115822.95008046
|
||||
@@ -1508,12 +1508,12 @@ Mean squared error on test data: 0.17446471
|
||||
Degree of polynomial: 13
|
||||
Mean squared error on training data: 0.00759119
|
||||
Mean squared error on test data: 1.08131003
|
||||
Degree of polynomial: 14
|
||||
Mean squared error on training data: 0.00472199
|
||||
Mean squared error on test data: 0.81333804
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 15
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 14
|
||||
Mean squared error on training data: 0.00472199
|
||||
Mean squared error on test data: 0.81333804
|
||||
Degree of polynomial: 15
|
||||
Mean squared error on training data: 0.00410478
|
||||
Mean squared error on test data: 92.09172409
|
||||
Degree of polynomial: 16
|
||||
@@ -1528,12 +1528,12 @@ Mean squared error on test data: 108.27092910
|
||||
Degree of polynomial: 19
|
||||
Mean squared error on training data: 0.00156376
|
||||
Mean squared error on test data: 1371.99051150
|
||||
Degree of polynomial: 20
|
||||
Mean squared error on training data: 0.00137818
|
||||
Mean squared error on test data: 1887.86252988
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 21
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 20
|
||||
Mean squared error on training data: 0.00137818
|
||||
Mean squared error on test data: 1887.86252988
|
||||
Degree of polynomial: 21
|
||||
Mean squared error on training data: 0.00118508
|
||||
Mean squared error on test data: 14859.69908626
|
||||
Degree of polynomial: 22
|
||||
@@ -1548,12 +1548,12 @@ Mean squared error on test data: 1277.61702282
|
||||
Degree of polynomial: 25
|
||||
Mean squared error on training data: 0.00079129
|
||||
Mean squared error on test data: 128664.31650694
|
||||
Degree of polynomial: 26
|
||||
Mean squared error on training data: 0.00076905
|
||||
Mean squared error on test data: 19003.94822514
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 27
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 26
|
||||
Mean squared error on training data: 0.00076905
|
||||
Mean squared error on test data: 19003.94822514
|
||||
Degree of polynomial: 27
|
||||
Mean squared error on training data: 0.00068946
|
||||
Mean squared error on test data: 2379.66219404
|
||||
Degree of polynomial: 28
|
||||
@@ -1564,13 +1564,13 @@ Mean squared error on training data: 0.00060705
|
||||
Mean squared error on test data: 3250.17647619
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
|
||||
plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
|
||||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_65_7.png" src="_images/chapter3_65_7.png" />
|
||||
<img alt="_images/chapter3_69_6.png" src="_images/chapter3_69_6.png" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1723,7 +1723,7 @@ cross-validation (LOOCV).</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter3_71_0.png" src="_images/chapter3_71_0.png" />
|
||||
<img alt="_images/chapter3_75_0.png" src="_images/chapter3_75_0.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>More examples of the application of cross-validation follow here.</p>
|
||||
@@ -1800,14 +1800,14 @@ cross-validation (LOOCV).</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
|
||||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_73_1.png" src="_images/chapter3_73_1.png" />
|
||||
<img alt="_images/chapter3_77_1.png" src="_images/chapter3_77_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>Note that we have kept the intercept in the first column of design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>. When we call the corresponding <strong>Scikit-Learn</strong> function we need thus to set the intercept to <strong>False</strong>. Libraries like <strong>Scikit-Learn</strong> normally scale the design matrix and does not fit intercept. See the discussions below.</p>
|
||||
<p>Note that we have kept the intercept in the first column of design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>. When we call the corresponding <strong>Scikit-Learn</strong> function we need thus to set the intercept to <strong>False</strong>. Libraries like <strong>Scikit-Learn</strong> normally scale the design matrix and do not fit intercept. See the discussions below.</p>
|
||||
</div>
|
||||
<div class="section" id="more-on-rescaling-data">
|
||||
<h2><span class="section-number">5.6. </span>More on Rescaling data<a class="headerlink" href="#more-on-rescaling-data" title="Permalink to this headline">¶</a></h2>
|
||||
@@ -2077,7 +2077,7 @@ MSE with Sklearn intercept
|
||||
0.004113634617443131
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_107_1.png" src="_images/chapter3_107_1.png" />
|
||||
<img alt="_images/chapter3_112_1.png" src="_images/chapter3_112_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>The intercept is the value of our output/target variable
|
||||
@@ -2088,7 +2088,7 @@ the way we treat the intercept may give a larger or smaller MSE,
|
||||
meaning that the MSE can be penalized by the value of the
|
||||
intercept. Not including the intercept in the fit, means that the
|
||||
regularization term does not include <span class="math notranslate nohighlight">\(\beta_0\)</span>. For different values
|
||||
of <span class="math notranslate nohighlight">\(\lambda\)</span>, this may lead to differeing MSE values.</p>
|
||||
of <span class="math notranslate nohighlight">\(\lambda\)</span>, this may lead to different MSE values.</p>
|
||||
<p>To remind the reader, the regularization term, with the intercept in Ridge regression, is given by</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
@@ -2272,7 +2272,7 @@ MSE values for Scikit-Learn Ridge implementation
|
||||
0.26409315307910025
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_115_1.png" src="_images/chapter3_115_1.png" />
|
||||
<img alt="_images/chapter3_120_1.png" src="_images/chapter3_120_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>The results here agree when we force <strong>Scikit-Learn</strong>’s Ridge function to include the first column in our design matrix.
|
||||
@@ -2475,7 +2475,7 @@ MSE values for Scikit-Learn Ridge implementation
|
||||
0.002381316302584886
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_117_1.png" src="_images/chapter3_117_1.png" />
|
||||
<img alt="_images/chapter3_122_1.png" src="_images/chapter3_122_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>We see here, when compared to the code which includes explicitely the
|
||||
@@ -2689,13 +2689,13 @@ linear system as an equation would reduce this down to
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/4162706317.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/4162706317.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
cb = fig.colorbar(im)
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/4162706317.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/4162706317.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_148_1.png" src="_images/chapter3_148_1.png" />
|
||||
<img alt="_images/chapter3_154_1.png" src="_images/chapter3_154_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>It is interesting to note that OLS
|
||||
@@ -2835,13 +2835,13 @@ with the form utilized in linear regression, viz.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3777801602.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3777801602.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
cb = fig.colorbar(im)
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3777801602.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3777801602.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_166_1.png" src="_images/chapter3_166_1.png" />
|
||||
<img alt="_images/chapter3_172_1.png" src="_images/chapter3_172_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>The results agree perfectly with our previous discussion where we used our own code.</p>
|
||||
@@ -2850,23 +2850,15 @@ regression. In ridge regression we include a <strong>regularizer</strong>. This
|
||||
involves a new cost function which leads to a new estimate for the
|
||||
weights <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span>. This results in a penalized regression problem. The
|
||||
cost function is given by</p>
|
||||
<p>6
|
||||
0</p>
|
||||
<p><
|
||||
<
|
||||
<
|
||||
!
|
||||
!
|
||||
M
|
||||
A
|
||||
T
|
||||
H
|
||||
_
|
||||
B
|
||||
L
|
||||
O
|
||||
C
|
||||
K</p>
|
||||
<!-- Equation labels as ordinary links -->
|
||||
<div id="_auto11"></div>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
\begin{equation}
|
||||
C(\boldsymbol{X}, \boldsymbol{\beta}; \lambda) = (\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y}) + \lambda \boldsymbol{\beta}^T\boldsymbol{\beta}.
|
||||
\label{_auto11} \tag{11}
|
||||
\end{equation}
|
||||
\]</div>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">_lambda</span> <span class="o">=</span> <span class="mf">0.1</span>
|
||||
@@ -2885,13 +2877,13 @@ K</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/438060758.py:9: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/438060758.py:9: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
cb = fig.colorbar(im)
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/438060758.py:10: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/438060758.py:10: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_169_1.png" src="_images/chapter3_169_1.png" />
|
||||
<img alt="_images/chapter3_175_1.png" src="_images/chapter3_175_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>In the <strong>Least Absolute Shrinkage and Selection Operator</strong> (LASSO)-method we get a third cost function.</p>
|
||||
@@ -2922,13 +2914,13 @@ K</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3544313922.py:8: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3544313922.py:8: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
cb = fig.colorbar(im)
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3544313922.py:9: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3544313922.py:9: UserWarning: FixedFormatter should only be used together with FixedLocator
|
||||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_173_1.png" src="_images/chapter3_173_1.png" />
|
||||
<img alt="_images/chapter3_179_1.png" src="_images/chapter3_179_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>It is quite striking how LASSO breaks the symmetry of the coupling
|
||||
@@ -2977,49 +2969,49 @@ constant as opposed to ridge and OLS. We get a sparse solution with
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0%| | 0/10 [00:00<?, ?it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0%| | 0/10 [00:00<?, ?it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_coordinate_descent.py:647: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations, check the scale of the features or consider increasing regularisation. Duality gap: 3.924e+00, tolerance: 1.797e+00
|
||||
model = cd_fast.enet_coordinate_descent(
|
||||
|
||||
10%|███████████▊ | 1/10 [00:00<00:06, 1.32it/s]
|
||||
10%|███████████▉ | 1/10 [00:00<00:06, 1.50it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 20%|███████████████████████▌ | 2/10 [00:01<00:05, 1.39it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 20%|███████████████████████▊ | 2/10 [00:01<00:04, 1.83it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 30%|███████████████████████████████████▍ | 3/10 [00:01<00:03, 1.83it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 30%|███████████████████████████████████▋ | 3/10 [00:01<00:02, 2.70it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 40%|███████████████████████████████████████████████▏ | 4/10 [00:01<00:02, 2.44it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 40%|███████████████████████████████████████████████▌ | 4/10 [00:01<00:01, 3.15it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 50%|███████████████████████████████████████████████████████████ | 5/10 [00:02<00:01, 3.02it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 50%|███████████████████████████████████████████████████████████▌ | 5/10 [00:01<00:01, 3.60it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 60%|██████████████████████████████████████████████████████████████████████▊ | 6/10 [00:02<00:01, 3.70it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 60%|███████████████████████████████████████████████████████████████████████▍ | 6/10 [00:01<00:01, 3.91it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 70%|██████████████████████████████████████████████████████████████████████████████████▌ | 7/10 [00:02<00:00, 4.31it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 70%|███████████████████████████████████████████████████████████████████████████████████▎ | 7/10 [00:02<00:00, 4.65it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 80%|██████████████████████████████████████████████████████████████████████████████████████████████▍ | 8/10 [00:02<00:00, 4.48it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 80%|███████████████████████████████████████████████████████████████████████████████████████████████▏ | 8/10 [00:02<00:00, 5.13it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 90%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 9/10 [00:02<00:00, 4.92it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 90%|███████████████████████████████████████████████████████████████████████████████████████████████████████████ | 9/10 [00:02<00:00, 5.60it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02<00:00, 5.65it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02<00:00, 5.96it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02<00:00, 3.37it/s]
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02<00:00, 3.97it/s]
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_175_13.png" src="_images/chapter3_175_13.png" />
|
||||
<img alt="_images/chapter3_181_13.png" src="_images/chapter3_181_13.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>We see that LASSO reaches a good solution for low
|
||||
@@ -3068,7 +3060,7 @@ testing set that is close to the accuracy of the training set.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter3_177_0.png" src="_images/chapter3_177_0.png" />
|
||||
<img alt="_images/chapter3_183_0.png" src="_images/chapter3_183_0.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>From the above figure we can see that LASSO with <span class="math notranslate nohighlight">\(\lambda = 10^{-2}\)</span>
|
||||
@@ -3160,13 +3152,13 @@ which polynomial fits the data best.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3980313467.py:9: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3980313467.py:9: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
|
||||
ax = fig.gca(projection='3d')
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3980313467.py:37: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_41811/3980313467.py:37: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
|
||||
fig.colorbar(surf, shrink=0.5, aspect=5)
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter3_181_1.png" src="_images/chapter3_181_1.png" />
|
||||
<img alt="_images/chapter3_188_1.png" src="_images/chapter3_188_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="exercise-ordinary-least-square-ols-on-the-franke-function">
|
||||
|
||||
@@ -328,8 +328,8 @@ const thebe_selector_output = ".output, .cell_output"
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
<a class="reference internal nav-link" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks">
|
||||
6.4. Examples of likelihood functions used in logistic regression and nueral networks
|
||||
<a class="reference internal nav-link" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-neural-networks">
|
||||
6.4. Examples of likelihood functions used in logistic regression and neural networks
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
@@ -373,8 +373,8 @@ const thebe_selector_output = ".output, .cell_output"
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
<a class="reference internal nav-link" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks">
|
||||
6.4. Examples of likelihood functions used in logistic regression and nueral networks
|
||||
<a class="reference internal nav-link" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-neural-networks">
|
||||
6.4. Examples of likelihood functions used in logistic regression and neural networks
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
@@ -391,7 +391,8 @@ const thebe_selector_output = ".output, .cell_output"
|
||||
|
||||
<div>
|
||||
|
||||
<div class="tex2jax_ignore mathjax_ignore section" id="logistic-regression">
|
||||
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||||
doconce format html chapter4.do.txt --><div class="tex2jax_ignore mathjax_ignore section" id="logistic-regression">
|
||||
<h1><span class="section-number">6. </span>Logistic Regression<a class="headerlink" href="#logistic-regression" title="Permalink to this headline">¶</a></h1>
|
||||
<div class="section" id="id1">
|
||||
<h2><span class="section-number">6.1. </span>Logistic Regression<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
|
||||
@@ -647,7 +648,7 @@ the probability of a given category. This leads us to the logistic function.</p>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>100 rows × 4 columns</p>
|
||||
</div></div><img alt="_images/chapter4_5_1.png" src="_images/chapter4_5_1.png" />
|
||||
</div></div><img alt="_images/chapter4_8_1.png" src="_images/chapter4_8_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>What we could attempt however is to plot the mean value for each group.</p>
|
||||
@@ -665,7 +666,7 @@ the probability of a given category. This leads us to the logistic function.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter4_7_0.png" src="_images/chapter4_7_0.png" />
|
||||
<img alt="_images/chapter4_10_0.png" src="_images/chapter4_10_0.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>We are now trying to find a function <span class="math notranslate nohighlight">\(f(y\vert x)\)</span>, that is a function which gives us an expected value for the output <span class="math notranslate nohighlight">\(y\)</span> with a given input <span class="math notranslate nohighlight">\(x\)</span>.
|
||||
@@ -704,8 +705,8 @@ p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
|
||||
\]</div>
|
||||
<p>Note that <span class="math notranslate nohighlight">\(1-p(t)= p(-t)\)</span>.</p>
|
||||
</div>
|
||||
<div class="section" id="examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks">
|
||||
<h2><span class="section-number">6.4. </span>Examples of likelihood functions used in logistic regression and nueral networks<a class="headerlink" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="examples-of-likelihood-functions-used-in-logistic-regression-and-neural-networks">
|
||||
<h2><span class="section-number">6.4. </span>Examples of likelihood functions used in logistic regression and neural networks<a class="headerlink" href="#examples-of-likelihood-functions-used-in-logistic-regression-and-neural-networks" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The following code plots the logistic function, the step function and other functions we will encounter from here and on.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
@@ -767,9 +768,9 @@ p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter4_13_0.png" src="_images/chapter4_13_0.png" />
|
||||
<img alt="_images/chapter4_13_1.png" src="_images/chapter4_13_1.png" />
|
||||
<img alt="_images/chapter4_13_2.png" src="_images/chapter4_13_2.png" />
|
||||
<img alt="_images/chapter4_18_0.png" src="_images/chapter4_18_0.png" />
|
||||
<img alt="_images/chapter4_18_1.png" src="_images/chapter4_18_1.png" />
|
||||
<img alt="_images/chapter4_18_2.png" src="_images/chapter4_18_2.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>We assume now that we have two classes with <span class="math notranslate nohighlight">\(y_i\)</span> either <span class="math notranslate nohighlight">\(0\)</span> or <span class="math notranslate nohighlight">\(1\)</span>. Furthermore we assume also that we have only two parameters <span class="math notranslate nohighlight">\(\beta\)</span> in our fitting of the Sigmoid function, that is we define probabilities</p>
|
||||
@@ -996,8 +997,8 @@ We use <strong>Pandas</strong> to compute the correlation matrix.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter4_51_0.png" src="_images/chapter4_51_0.png" />
|
||||
<img alt="_images/chapter4_51_1.png" src="_images/chapter4_51_1.png" />
|
||||
<img alt="_images/chapter4_57_0.png" src="_images/chapter4_57_0.png" />
|
||||
<img alt="_images/chapter4_57_1.png" src="_images/chapter4_57_1.png" />
|
||||
</div>
|
||||
</div>
|
||||
<p>In the above example we note two things. In the first plot we display
|
||||
@@ -1030,6 +1031,20 @@ matrix.</p>
|
||||
features are of relevance and which are not. This leads us to
|
||||
the classical Principal Component Analysis (PCA) theorem with
|
||||
applications. This will be discussed later this semester (<a class="reference external" href="https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html">week 43</a>).</p>
|
||||
<p>Here we present a further way to present our results in terms of a so-called <strong>confusion matrix</strong>, the cumulative gain and the <strong>ROC</strong> curve.
|
||||
This way of displaying our data are based upon different ways to classify our possible outcomes. Before we proceed we need some definitions.</p>
|
||||
<ol class="simple">
|
||||
<li><p><strong>TP</strong>: true positive or in other words, something equivalent with a proper classification</p></li>
|
||||
<li><p><strong>TN</strong>: true negative, which is equivalent with a correct rejection</p></li>
|
||||
<li><p><strong>FP</strong>: false positive, or in simpler words something that is equivalent with a false alarm</p></li>
|
||||
<li><p><strong>FN</strong>: false negative, which is mean to be equivalent with a miss.</p></li>
|
||||
</ol>
|
||||
<p>The total data set is then the sum of the true positive and true negative targets or outputs, labeled by <span class="math notranslate nohighlight">\(n\)</span>.
|
||||
Based on this we can then define the accuracy score as the sum of correctly predicted <strong>TP</strong> and <strong>TN</strong> cases divided by the sum of true positive and treue negative events in our data set, or as</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
\mathrm{Accuracy} = \frac{\sum_{i=0}^{n-1}I(y_i=\tilde{y}_i)}{n}.
|
||||
\]</div>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||||
@@ -1099,18 +1114,9 @@ Please also refer to the documentation for alternative solver options:
|
||||
n_iter_i = _check_optimize_result(
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
|
||||
<span class="ne">ModuleNotFoundError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
|
||||
<span class="nn">Input In [8],</span> in <span class="ni"><cell line: 36></span><span class="nt">()</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">32</span> <span class="nb">print</span><span class="p">(</span><span class="n">accuracy</span><span class="p">)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">33</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Test set accuracy with Logistic Regression and scaled data: </span><span class="si">{:.2f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logreg</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X_test_scaled</span><span class="p">,</span><span class="n">y_test</span><span class="p">)))</span>
|
||||
<span class="ne">---> </span><span class="mi">36</span> <span class="kn">import</span> <span class="nn">scikitplot</span> <span class="k">as</span> <span class="nn">skplt</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">37</span> <span class="n">y_pred</span> <span class="o">=</span> <span class="n">logreg</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test_scaled</span><span class="p">)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">38</span> <span class="n">skplt</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">plot_confusion_matrix</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||||
|
||||
<span class="ne">ModuleNotFoundError</span>: No module named 'scikitplot'
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter4_64_2.png" src="_images/chapter4_64_2.png" />
|
||||
<img alt="_images/chapter4_64_3.png" src="_images/chapter4_64_3.png" />
|
||||
<img alt="_images/chapter4_64_4.png" src="_images/chapter4_64_4.png" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# <!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||||
# doconce format html chapter3.do.txt -->
|
||||
|
||||
# # Resampling Methods
|
||||
#
|
||||
|
||||
# ## Introduction
|
||||
#
|
||||
# Resampling methods are an indispensable tool in modern
|
||||
@@ -24,7 +27,6 @@
|
||||
# In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular
|
||||
# cross-validation and the bootstrap method.
|
||||
#
|
||||
#
|
||||
# Resampling approaches can be computationally expensive, because they
|
||||
# involve fitting the same statistical method multiple times using
|
||||
# different subsets of the training data. However, due to recent
|
||||
@@ -41,23 +43,20 @@
|
||||
# level of flexibility for a model is known as model selection. The
|
||||
# bootstrap is widely used.
|
||||
#
|
||||
#
|
||||
# * Our simulations can be treated as *computer experiments*. This is particularly the case for Monte Carlo methods
|
||||
#
|
||||
# * The results can be analysed with the same statistical tools as we would use analysing experimental data.
|
||||
#
|
||||
# * As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.
|
||||
#
|
||||
|
||||
# ## Reminder on Statistics
|
||||
#
|
||||
#
|
||||
# * As in other experiments, many numerical experiments have two classes of errors:
|
||||
#
|
||||
# * Statistical errors
|
||||
#
|
||||
# * Systematical errors
|
||||
#
|
||||
#
|
||||
# * Statistical errors can be estimated using standard tools from statistics
|
||||
#
|
||||
# * Systematical errors are method specific and must be treated differently from case to case.
|
||||
@@ -69,7 +68,6 @@
|
||||
# derive quantities like the variance and other expectation values in a
|
||||
# rather straightforward way.
|
||||
#
|
||||
#
|
||||
# It is assumed that $\varepsilon_i
|
||||
# \sim \mathcal{N}(0, \sigma^2)$ and the $\varepsilon_{i}$ are
|
||||
# independent, i.e.:
|
||||
@@ -93,7 +91,6 @@
|
||||
# notation above $\mathbf{X}_{i,\ast}$ means that we are looking at the
|
||||
# row number $i$ and perform a sum over all values $p$.
|
||||
#
|
||||
#
|
||||
# The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias-variance trade off)
|
||||
# that there exists a function $f(\boldsymbol{x})$ and a normal distributed error $\boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2)$
|
||||
# which describe our data
|
||||
@@ -140,7 +137,6 @@
|
||||
# Hence, $y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2)$, that is $\boldsymbol{y}$ follows a normal distribution with
|
||||
# mean value $\boldsymbol{X}\boldsymbol{\beta}$ and variance $\sigma^2$ (not be confused with the singular values of the SVD).
|
||||
#
|
||||
#
|
||||
# With the OLS expressions for the parameters $\boldsymbol{\beta}$ we can evaluate the expectation value
|
||||
|
||||
# $$
|
||||
@@ -148,7 +144,7 @@
|
||||
# $$
|
||||
|
||||
# This means that the estimator of the regression parameters is unbiased.
|
||||
# v
|
||||
#
|
||||
# We can also calculate the variance
|
||||
#
|
||||
# The variance of $\boldsymbol{\beta}$ is
|
||||
@@ -185,7 +181,6 @@
|
||||
# [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} }$. This may be used to
|
||||
# construct a confidence interval for the estimates.
|
||||
#
|
||||
#
|
||||
# In a similar way, we can obtain analytical expressions for say the
|
||||
# expectation values of the parameters $\boldsymbol{\beta}$ and their variance
|
||||
# when we employ Ridge regression, allowing us again to define a confidence interval.
|
||||
@@ -215,10 +210,8 @@
|
||||
|
||||
# The difference is non-negative definite since each component of the
|
||||
# matrix product is non-negative definite.
|
||||
# This means the variance we obtain with the standard OLS will always for $\lambda > 0$ be larger than the variance of $\boldsymbol{\beta}$ obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below.
|
||||
#
|
||||
#
|
||||
#
|
||||
# This means the variance we obtain with the standard OLS will always for $\lambda > 0$ be larger than the variance of $\boldsymbol{\beta}$ obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below.
|
||||
|
||||
# ## Resampling methods
|
||||
#
|
||||
# With all these analytical equations for both the OLS and Ridge
|
||||
@@ -236,12 +229,10 @@
|
||||
#
|
||||
# 2. training error $\mathrm{Err_{Train}}$, which is the average loss over the training data.
|
||||
#
|
||||
# As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
# As our model becomes more and more complex, more of the training data tends to be used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||||
# For a certain level of complexity the test error will reach minimum, before starting to increase again. The
|
||||
# training error reaches a saturation.
|
||||
#
|
||||
#
|
||||
#
|
||||
# Two famous
|
||||
# resampling methods are the **independent bootstrap** and **the jackknife**.
|
||||
#
|
||||
@@ -257,7 +248,6 @@
|
||||
# variance of $\overline{X}$ (which often is the case), then there is no
|
||||
# need for bootstrapping.
|
||||
#
|
||||
#
|
||||
# The Jackknife works by making many replicas of the estimator $\widehat{\beta}$.
|
||||
# The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values $\boldsymbol{x} = (x_1,x_2,\cdots,X_n)$.
|
||||
# Let $\boldsymbol{x}_i$ denote the vector
|
||||
@@ -328,8 +318,6 @@ t = jackknife(x, stat)
|
||||
# estimate all the interesting parameters of $p(\boldsymbol{t})$ using point
|
||||
# estimators.
|
||||
#
|
||||
#
|
||||
#
|
||||
# In the case that $\widehat{\beta}$ has
|
||||
# more than one component, and the components are independent, we use the
|
||||
# same estimator on each component separately. If the probability
|
||||
@@ -344,7 +332,6 @@ t = jackknife(x, stat)
|
||||
# idea is to use the relative frequency of $\widehat{\beta}^*$
|
||||
# (think of a histogram) as an estimate of $p(\boldsymbol{t})$.
|
||||
#
|
||||
#
|
||||
# But
|
||||
# unless there is enough information available about the process that
|
||||
# generated $X_1,X_2,\cdots,X_n$, $p(x)$ is in general
|
||||
@@ -354,13 +341,11 @@ t = jackknife(x, stat)
|
||||
# the relative frequency of the observations, will we obtain the same
|
||||
# result in some asymptotic sense? The answer is yes.
|
||||
#
|
||||
#
|
||||
# Instead of generating the histogram for the relative
|
||||
# frequency of the observation $X_i$, just draw the values
|
||||
# $(X_1^*,X_2^*,\cdots,X_n^*)$ with replacement from the vector
|
||||
# $\boldsymbol{X}$.
|
||||
#
|
||||
#
|
||||
# The independent bootstrap works like this:
|
||||
#
|
||||
# 1. Draw with replacement $n$ numbers for the observed variables $\boldsymbol{x} = (x_1,x_2,\cdots,x_n)$.
|
||||
@@ -378,7 +363,7 @@ t = jackknife(x, stat)
|
||||
# histogram of the relative frequency of $\widehat{\beta}^*$. Instead
|
||||
# you use the estimators corresponding to the statistic of interest. For
|
||||
# example, if you are interested in estimating the variance of $\widehat
|
||||
# \beta$, apply the etsimator $\widehat \sigma^2$ to the values
|
||||
# \beta$, apply the estimator $\widehat \sigma^2$ to the values
|
||||
# $\widehat \beta^*$.
|
||||
#
|
||||
# Before we proceed however, we need to remind ourselves about a central
|
||||
@@ -387,7 +372,6 @@ t = jackknife(x, stat)
|
||||
# (and other resampling methods) work so well on independent and
|
||||
# identically distributed variables.
|
||||
#
|
||||
#
|
||||
# Suppose we have a PDF $p(x)$ from which we generate a series $N$
|
||||
# of averages $\langle x_i \rangle$. Each mean value $\langle x_i \rangle$
|
||||
# is viewed as the average of a specific measurement, e.g., throwing
|
||||
@@ -404,7 +388,6 @@ t = jackknife(x, stat)
|
||||
|
||||
# the question we pose is which is the PDF of the new variable $z$.
|
||||
#
|
||||
#
|
||||
# The probability of obtaining an average value $z$ is the product of the
|
||||
# probabilities of obtaining arbitrary individual mean values $x_i$,
|
||||
# but with the constraint that the average is $z$. We can express this through
|
||||
@@ -420,8 +403,6 @@ t = jackknife(x, stat)
|
||||
# be independent, which in turn means that we can express $\tilde{p}$ as the
|
||||
# product of individual $p(x_i)$. The independence assumption is important in the derivation of the central limit theorem.
|
||||
#
|
||||
#
|
||||
#
|
||||
# If we use the integral expression for the $\delta$-function
|
||||
|
||||
# $$
|
||||
@@ -472,7 +453,6 @@ t = jackknife(x, stat)
|
||||
# $\sigma^2_m=\sigma^2/m$, where $\sigma$ is the variance of the PDF $p(x)$
|
||||
# and $\mu$ is also the mean of the PDF $p(x)$.
|
||||
#
|
||||
#
|
||||
# Thus, the central limit theorem states that the PDF $\tilde{p}(z)$ of
|
||||
# the average of $m$ random values corresponding to a PDF $p(x)$
|
||||
# is a normal distribution whose mean is the
|
||||
@@ -504,13 +484,10 @@ t = jackknife(x, stat)
|
||||
# we generate in various calculations do always exhibit some
|
||||
# correlations.
|
||||
#
|
||||
#
|
||||
#
|
||||
# The theorem is satisfied by a large class of PDFs. Note however that for a
|
||||
# finite $m$, it is not always possible to find a closed form /analytic expression for
|
||||
# $\tilde{p}(x)$.
|
||||
#
|
||||
#
|
||||
# The following code starts with a Gaussian distribution with mean value
|
||||
# $\mu =100$ and variance $\sigma=15$. We use this to generate the data
|
||||
# used in the bootstrap analysis. The bootstrap analysis returns a data
|
||||
@@ -577,7 +554,6 @@ plt.show()
|
||||
|
||||
# ## The bias-variance tradeoff
|
||||
#
|
||||
#
|
||||
# We will discuss the bias-variance tradeoff in the context of
|
||||
# continuous predictions such as regression. However, many of the
|
||||
# intuitions and ideas discussed here also carry over to classification
|
||||
@@ -609,13 +585,13 @@ plt.show()
|
||||
# \mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
|
||||
# $$
|
||||
|
||||
# The three terms represent the square of the bias of the learning
|
||||
# The first term represents the square of the bias of the learning
|
||||
# method, which can be thought of as the error caused by the simplifying
|
||||
# assumptions built into the method. The second term represents the
|
||||
# variance of the chosen model and finally the last terms is variance of
|
||||
# the error $\boldsymbol{\epsilon}$.
|
||||
#
|
||||
# To derive this equation, we need to recall that the variance of $\boldsymbol{y}$ and $\boldsymbol{\epsilon}$ are both equal to $\sigma^2$. The mean value of $\boldsymbol{\epsilon}$ is by definition equal to zero. Furthermore, the function $f$ is not a stochastics variable, idem for $\boldsymbol{\tilde{y}}$.
|
||||
# To derive this equation, we need to recall that the variance of $\boldsymbol{y}$ and $\boldsymbol{\epsilon}$ are both equal to $\sigma^2$. The mean value of $\boldsymbol{\epsilon}$ is by definition equal to zero. Furthermore, the function $f$ is not a stochastic variable, idem for $\boldsymbol{\tilde{y}}$.
|
||||
# We use a more compact notation in terms of the expectation value
|
||||
|
||||
# $$
|
||||
@@ -755,8 +731,6 @@ plt.show()
|
||||
# train and less sensitive to sampling noise arising from having a
|
||||
# finite-sized training dataset (smaller variance).
|
||||
#
|
||||
#
|
||||
#
|
||||
# The above equations tell us that in
|
||||
# order to minimize the expected test error, we need to select a
|
||||
# statistical learning method that simultaneously achieves low variance
|
||||
@@ -764,7 +738,6 @@ plt.show()
|
||||
# and squared bias is also nonnegative. Hence, we see that the expected
|
||||
# test MSE can never lie below $Var(\epsilon)$, the irreducible error.
|
||||
#
|
||||
#
|
||||
# What do we mean by the variance and bias of a statistical learning
|
||||
# method? The variance refers to the amount by which our model would change if we
|
||||
# estimated it using a different training data set. Since the training
|
||||
@@ -775,7 +748,6 @@ plt.show()
|
||||
# the training data can result in large changes in the model. In general, more
|
||||
# flexible statistical methods have higher variance.
|
||||
#
|
||||
#
|
||||
# You may also find this recent [article](https://www.pnas.org/content/116/32/15849) of interest.
|
||||
|
||||
# In[6]:
|
||||
@@ -954,7 +926,6 @@ plt.show()
|
||||
# choosing $k=n$. This particular case is referred to as leave-one-out
|
||||
# cross-validation (LOOCV).
|
||||
#
|
||||
#
|
||||
# * Define a range of interest for the penalty parameter.
|
||||
#
|
||||
# * Divide the data set into training and test set comprising samples $\{1, \ldots, n\} \setminus i$ and $\{ i \}$, respectively.
|
||||
@@ -997,7 +968,6 @@ plt.show()
|
||||
#
|
||||
# d. Retain the evaluation score and discard the model
|
||||
#
|
||||
#
|
||||
# 5. Summarize the model using the sample of model evaluation scores
|
||||
#
|
||||
# The code here uses Ridge regression with cross-validation (CV) resampling and $k$-fold CV in order to fit a specific polynomial.
|
||||
@@ -1170,8 +1140,8 @@ plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
# Note that we have kept the intercept in the first column of design matrix $\boldsymbol{X}$. When we call the corresponding **Scikit-Learn** function we need thus to set the intercept to **False**. Libraries like **Scikit-Learn** normally scale the design matrix and does not fit intercept. See the discussions below.
|
||||
#
|
||||
# Note that we have kept the intercept in the first column of design matrix $\boldsymbol{X}$. When we call the corresponding **Scikit-Learn** function we need thus to set the intercept to **False**. Libraries like **Scikit-Learn** normally scale the design matrix and do not fit intercept. See the discussions below.
|
||||
|
||||
# ## More on Rescaling data
|
||||
#
|
||||
# We end this chapter by adding some words on scaling and how to deal with the intercept for regression cases.
|
||||
@@ -1194,7 +1164,6 @@ plt.show()
|
||||
# $\boldsymbol{\beta}$, are derived under the assumption that both $\boldsymbol{y}$ and
|
||||
# $\boldsymbol{X}$ are zero centered, that is we subtract the mean values.
|
||||
#
|
||||
#
|
||||
# If our predictors represent different scales, then it is important to
|
||||
# standardize the design matrix $\boldsymbol{X}$ by subtracting the mean of each
|
||||
# column from the corresponding column and dividing the column with its
|
||||
@@ -1220,8 +1189,6 @@ plt.show()
|
||||
# coefficient term, than if measured in millimeters.
|
||||
# This can clearly lead to problems in evaluating the cost/loss functions.
|
||||
#
|
||||
#
|
||||
#
|
||||
# Keep in mind that when you transform your data set before training a model, the same transformation needs to be done
|
||||
# on your eventual new data set before making a prediction. If we translate this into a Python code, it would could be implemented as follows
|
||||
|
||||
@@ -1332,8 +1299,6 @@ y_pred = y_pred + y_train_mean
|
||||
|
||||
# the mean value for all elements of the column vector $\boldsymbol{x}_j$.
|
||||
#
|
||||
#
|
||||
#
|
||||
# Replacing $y_i$ with $y_i - y_i - \overline{\boldsymbol{y}}$ and centering also our design matrix results in a cost function (in vector-matrix disguise)
|
||||
|
||||
# $$
|
||||
@@ -1357,7 +1322,6 @@ y_pred = y_pred + y_train_mean
|
||||
|
||||
# What does this mean? And why do we insist on all this? Let us look at some examples.
|
||||
#
|
||||
#
|
||||
# This code shows a simple first-order fit to a data set using the above transformed data, where we consider the role of the intercept first, by either excluding it or including it (*code example thanks to Øyvind Sigmundson Schøyen*). Here our scaling of the data is done by subtracting the mean values only.
|
||||
# Note also that we do not split the data into training and test.
|
||||
|
||||
@@ -1461,7 +1425,7 @@ plt.show()
|
||||
# meaning that the MSE can be penalized by the value of the
|
||||
# intercept. Not including the intercept in the fit, means that the
|
||||
# regularization term does not include $\beta_0$. For different values
|
||||
# of $\lambda$, this may lead to differeing MSE values.
|
||||
# of $\lambda$, this may lead to different MSE values.
|
||||
#
|
||||
# To remind the reader, the regularization term, with the intercept in Ridge regression, is given by
|
||||
|
||||
@@ -1488,7 +1452,6 @@ plt.show()
|
||||
# back the intercept, we will get a MSE which then contains the
|
||||
# intercept.
|
||||
#
|
||||
#
|
||||
# Armed with this wisdom, we attempt first to simply set the intercept equal to **False** in our implementation of Ridge regression for our well-known vanilla data set.
|
||||
|
||||
# In[12]:
|
||||
@@ -1659,7 +1622,7 @@ plt.show()
|
||||
# of polynomial fitting problem.
|
||||
#
|
||||
# The next example is indeed an example where all these discussions about the role of intercept are not present.
|
||||
#
|
||||
|
||||
# ## More complicated Example: The Ising model
|
||||
#
|
||||
# The one-dimensional Ising model with nearest neighbor interaction, no
|
||||
@@ -1885,10 +1848,6 @@ plt.show()
|
||||
#
|
||||
# In this case our matrix inversion was actually possible. The obvious question now is what is the mathematics behind the SVD?
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# Let us now
|
||||
# focus on Ridge and Lasso regression as well. We repeat some of the
|
||||
# basic parts of the Ising model and the setup of the training and test
|
||||
@@ -2031,31 +1990,21 @@ plt.show()
|
||||
|
||||
# The results agree perfectly with our previous discussion where we used our own code.
|
||||
#
|
||||
#
|
||||
# Having explored the ordinary least squares we move on to ridge
|
||||
# regression. In ridge regression we include a **regularizer**. This
|
||||
# involves a new cost function which leads to a new estimate for the
|
||||
# weights $\boldsymbol{\beta}$. This results in a penalized regression problem. The
|
||||
# cost function is given by
|
||||
|
||||
# 6
|
||||
# 0
|
||||
#
|
||||
# <
|
||||
# <
|
||||
# <
|
||||
# !
|
||||
# !
|
||||
# M
|
||||
# A
|
||||
# T
|
||||
# H
|
||||
# _
|
||||
# B
|
||||
# L
|
||||
# O
|
||||
# C
|
||||
# K
|
||||
# <!-- Equation labels as ordinary links -->
|
||||
# <div id="_auto11"></div>
|
||||
#
|
||||
# $$
|
||||
# \begin{equation}
|
||||
# C(\boldsymbol{X}, \boldsymbol{\beta}; \lambda) = (\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y}) + \lambda \boldsymbol{\beta}^T\boldsymbol{\beta}.
|
||||
# \label{_auto11} \tag{11}
|
||||
# \end{equation}
|
||||
# $$
|
||||
|
||||
# In[26]:
|
||||
|
||||
@@ -2108,9 +2057,6 @@ plt.show()
|
||||
# constant as opposed to ridge and OLS. We get a sparse solution with
|
||||
# $J_{j, j + 1} = -1$.
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# We see how the different models perform for a different set of values for $\lambda$.
|
||||
|
||||
# In[28]:
|
||||
@@ -2159,7 +2105,6 @@ plt.show()
|
||||
# much. Ridge is more stable over a larger range of values for
|
||||
# $\lambda$, but eventually also fades away.
|
||||
#
|
||||
#
|
||||
# To determine which value of $\lambda$ is best we plot the accuracy of
|
||||
# the models when predicting the training and the testing set. We expect
|
||||
# the accuracy of the training set to be quite good, but if the accuracy
|
||||
@@ -2205,21 +2150,13 @@ plt.show()
|
||||
# From the above figure we can see that LASSO with $\lambda = 10^{-2}$
|
||||
# achieves a very good accuracy on the test set. This by far surpasses the
|
||||
# other models for all values of $\lambda$.
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
# ## Exercises and Projects
|
||||
#
|
||||
#
|
||||
#
|
||||
# The main aim of this project is to study in more detail various
|
||||
# regression methods, including the Ordinary Least Squares (OLS) method,
|
||||
# The total score is **100** points. Each subtask has its own final score.
|
||||
#
|
||||
#
|
||||
# We will first study how to fit polynomials to a specific
|
||||
# two-dimensional function called [Franke's
|
||||
# function](http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf). This
|
||||
@@ -2230,7 +2167,6 @@ plt.show()
|
||||
# proper assessment of our models. We will also study in detail the
|
||||
# so-called Bias-Variance trade off.
|
||||
#
|
||||
#
|
||||
# The Franke function, which is a weighted sum of four exponentials reads as follows
|
||||
|
||||
# $$
|
||||
@@ -2259,7 +2195,6 @@ plt.show()
|
||||
# beyond the second-order polynomials metioned above and explore
|
||||
# which polynomial fits the data best.
|
||||
#
|
||||
#
|
||||
# The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
|
||||
|
||||
# In[30]:
|
||||
@@ -2355,11 +2290,8 @@ plt.show()
|
||||
# data and say test data. An accepted rule of thumb is to use
|
||||
# approximately $2/3$ to $4/5$ of the data as training data.
|
||||
#
|
||||
#
|
||||
# You can easily reuse the solutions to your exercises from week 35 and week 36.
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
# ### Exercise: Bias-variance trade-off and resampling techniques
|
||||
#
|
||||
# Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.
|
||||
@@ -2422,8 +2354,7 @@ plt.show()
|
||||
# of data points, and possibly also your training and test data using the **bootstrap** resampling method.
|
||||
#
|
||||
# Note also that when you calculate the bias, in all applications you don't know the function values $f_i$. You would hence replace them with the actual data points $y_i$.
|
||||
#
|
||||
#
|
||||
|
||||
# ### Exercise: Cross-validation as resampling techniques, adding more complexity
|
||||
#
|
||||
# The aim here is to write your own code for another widely popular
|
||||
@@ -2440,8 +2371,7 @@ plt.show()
|
||||
# you got from your **bootstrap** code. Comment your results. Try $5-10$
|
||||
# folds. You can also compare your own cross-validation code with the
|
||||
# one provided by **Scikit-Learn**.
|
||||
#
|
||||
#
|
||||
|
||||
# ### Exercise: Ridge Regression on the Franke function with resampling
|
||||
#
|
||||
# Write your own code for the Ridge method, either using matrix
|
||||
@@ -2452,8 +2382,8 @@ plt.show()
|
||||
# dependence on $\lambda$.
|
||||
#
|
||||
# Study also the bias-variance trade-off as function of various values of
|
||||
# the parameter $\lambda$. For the bias-variance trade-off, use the **bootstrap** resampling method. Comment your results.
|
||||
#
|
||||
# the parameter $\lambda$. For the bias-variance trade-off, use the **bootstrap** resampling method. Comment your results.
|
||||
|
||||
# ### Exercise: Lasso Regression on the Franke function with resampling
|
||||
#
|
||||
# This exercise is essentially a repeat of the previous two ones, but now
|
||||
@@ -2461,8 +2391,8 @@ plt.show()
|
||||
# you can also use the functionalities of **Scikit-Learn** (recommended).
|
||||
# Give a
|
||||
# critical discussion of the three methods and a judgement of which
|
||||
# model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the **bootstrap** resampling technique and an analysis of the mean squared error using cross-validation.
|
||||
#
|
||||
# model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the **bootstrap** resampling technique and an analysis of the mean squared error using cross-validation.
|
||||
|
||||
# ### Exercise: Analysis of real data
|
||||
#
|
||||
# With our codes functioning and having been tested properly on a
|
||||
@@ -2491,7 +2421,7 @@ scipy.misc.imread
|
||||
# Here is a simple part of a Python code which reads and plots the data
|
||||
# from such files
|
||||
|
||||
# In[ ]:
|
||||
# In[32]:
|
||||
|
||||
|
||||
"""
|
||||
@@ -2519,10 +2449,8 @@ plt.show()
|
||||
# Austfjell, again in Norway.
|
||||
# Feel free to produce your own terrain data.
|
||||
#
|
||||
#
|
||||
# Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example [kaggle.com](https://www.kaggle.com/datasets) for examples.
|
||||
#
|
||||
#
|
||||
# Our final part deals with the parameterization of your digital terrain
|
||||
# data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
|
||||
# approximation and cross-validation as resampling technique to evaluate which
|
||||
|
||||
|
After Width: | Height: | Size: 27 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 294 KiB |
|
After Width: | Height: | Size: 106 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 22 KiB |
@@ -1,10 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# <!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||||
# doconce format html chapter4.do.txt -->
|
||||
|
||||
# # Logistic Regression
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
# ## Logistic Regression
|
||||
#
|
||||
# In linear regression our main interest was centered on learning the
|
||||
@@ -19,7 +20,6 @@
|
||||
# the product of the design matrices, linear regression gives then a
|
||||
# simple recipe for fitting our data.
|
||||
#
|
||||
#
|
||||
# Classification problems, however, are concerned with outcomes taking
|
||||
# the form of discrete variables (i.e. categories). We may for example,
|
||||
# on the basis of DNA sequencing for a number of patients, like to find
|
||||
@@ -36,7 +36,6 @@
|
||||
# binary outcome, true or false, positive or negative, success or
|
||||
# failure etc.
|
||||
#
|
||||
#
|
||||
# Logistic regression will also serve as our stepping stone towards
|
||||
# neural network algorithms and supervised deep learning. For logistic
|
||||
# learning, the minimization of the cost function leads to a non-linear
|
||||
@@ -50,9 +49,7 @@
|
||||
# We note also that many of the topics discussed here on logistic
|
||||
# regression are also commonly used in modern supervised Deep Learning
|
||||
# models, as we will see later.
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
# ## Basics
|
||||
#
|
||||
# We consider the case where the dependent variables, also called the
|
||||
@@ -95,7 +92,6 @@
|
||||
# where $\boldsymbol{y}$ is a vector representing the possible outcomes, $\boldsymbol{X}$ is our
|
||||
# $n\times p$ design matrix and $\boldsymbol{\beta}$ represents our estimators/predictors.
|
||||
#
|
||||
#
|
||||
# The main problem with our function is that it takes values on the
|
||||
# entire real axis. In the case of logistic regression, however, the
|
||||
# labels $y_i$ are discrete variables. A typical example is the credit
|
||||
@@ -111,7 +107,6 @@
|
||||
# favorable to use a ``soft" classifier that outputs
|
||||
# the probability of a given category. This leads us to the logistic function.
|
||||
#
|
||||
#
|
||||
# The following example on data for coronary heart disease (CHD) as function of age may serve as an illustration. In the code here we read and plot whether a person has had CHD (output = 1) or not (output = 0). This ouput is plotted the person's against age. Clearly, the figure shows that attempting to make a standard linear regression fit may not be very meaningful.
|
||||
|
||||
# In[1]:
|
||||
@@ -207,8 +202,7 @@ plt.show()
|
||||
# Sigmoid function or logistic model. We will consider this function as
|
||||
# representing the probability for finding a value of $y_i$ with a given
|
||||
# $x_i$.
|
||||
#
|
||||
#
|
||||
|
||||
# ## The logistic function
|
||||
#
|
||||
# Another widely studied model, is the so-called
|
||||
@@ -228,9 +222,8 @@ plt.show()
|
||||
# $$
|
||||
|
||||
# Note that $1-p(t)= p(-t)$.
|
||||
#
|
||||
# ## Examples of likelihood functions used in logistic regression and nueral networks
|
||||
#
|
||||
|
||||
# ## Examples of likelihood functions used in logistic regression and neural networks
|
||||
#
|
||||
# The following code plots the logistic function, the step function and other functions we will encounter from here and on.
|
||||
|
||||
@@ -345,11 +338,9 @@ plt.show()
|
||||
# This equation is known in statistics as the **cross entropy**. Finally, we note that just as in linear regression,
|
||||
# in practice we often supplement the cross-entropy with additional regularization terms, usually $L_1$ and $L_2$ regularization as we did for Ridge and Lasso regression.
|
||||
#
|
||||
#
|
||||
# The cross entropy is a convex function of the weights $\boldsymbol{\beta}$ and,
|
||||
# therefore, any local minimizer is a global minimizer.
|
||||
#
|
||||
#
|
||||
# Minimizing this
|
||||
# cost function with respect to the two parameters $\beta_0$ and $\beta_1$ we obtain
|
||||
|
||||
@@ -414,8 +405,6 @@ plt.show()
|
||||
# and the model is specified in term of $K-1$ so-called log-odds or
|
||||
# **logit** transformations.
|
||||
#
|
||||
#
|
||||
#
|
||||
# In our discussion of neural networks we will encounter the above again
|
||||
# in terms of a slightly modified function, the so-called **Softmax** function.
|
||||
#
|
||||
@@ -447,7 +436,7 @@ plt.show()
|
||||
# descent method. Newton's method and gradient descent methods are
|
||||
# discussed in the material on [optimization
|
||||
# methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html).
|
||||
#
|
||||
|
||||
# ## Wisconsin Cancer Data
|
||||
#
|
||||
# We show here how we can use a simple regression case on the breast
|
||||
@@ -558,6 +547,23 @@ correlation_matrix = cancerpd.corr().round(1)
|
||||
# features are of relevance and which are not. This leads us to
|
||||
# the classical Principal Component Analysis (PCA) theorem with
|
||||
# applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html)).
|
||||
#
|
||||
# Here we present a further way to present our results in terms of a so-called **confusion matrix**, the cumulative gain and the **ROC** curve.
|
||||
# This way of displaying our data are based upon different ways to classify our possible outcomes. Before we proceed we need some definitions.
|
||||
# 1. **TP**: true positive or in other words, something equivalent with a proper classification
|
||||
#
|
||||
# 2. **TN**: true negative, which is equivalent with a correct rejection
|
||||
#
|
||||
# 3. **FP**: false positive, or in simpler words something that is equivalent with a false alarm
|
||||
#
|
||||
# 4. **FN**: false negative, which is mean to be equivalent with a miss.
|
||||
#
|
||||
# The total data set is then the sum of the true positive and true negative targets or outputs, labeled by $n$.
|
||||
# Based on this we can then define the accuracy score as the sum of correctly predicted **TP** and **TN** cases divided by the sum of true positive and treue negative events in our data set, or as
|
||||
|
||||
# $$
|
||||
# \mathrm{Accuracy} = \frac{\sum_{i=0}^{n-1}I(y_i=\tilde{y}_i)}{n}.
|
||||
# $$
|
||||
|
||||
# In[8]:
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 264 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 19 KiB |
@@ -2,12 +2,32 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f63b8c1a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
|
||||
"doconce format html chapter4.do.txt -->"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a284cf5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Logistic Regression"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d78b73e2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Logistic Regression\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Logistic Regression\n",
|
||||
"\n",
|
||||
"In linear regression our main interest was centered on learning the\n",
|
||||
@@ -22,7 +42,6 @@
|
||||
"the product of the design matrices, linear regression gives then a\n",
|
||||
"simple recipe for fitting our data.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Classification problems, however, are concerned with outcomes taking\n",
|
||||
"the form of discrete variables (i.e. categories). We may for example,\n",
|
||||
"on the basis of DNA sequencing for a number of patients, like to find\n",
|
||||
@@ -39,7 +58,6 @@
|
||||
"binary outcome, true or false, positive or negative, success or\n",
|
||||
"failure etc.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Logistic regression will also serve as our stepping stone towards\n",
|
||||
"neural network algorithms and supervised deep learning. For logistic\n",
|
||||
"learning, the minimization of the cost function leads to a non-linear\n",
|
||||
@@ -52,10 +70,16 @@
|
||||
"\n",
|
||||
"We note also that many of the topics discussed here on logistic \n",
|
||||
"regression are also commonly used in modern supervised Deep Learning\n",
|
||||
"models, as we will see later.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"models, as we will see later."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d0c829b4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Basics\n",
|
||||
"\n",
|
||||
"We consider the case where the dependent variables, also called the\n",
|
||||
@@ -76,7 +100,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f1f4d5f2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n",
|
||||
@@ -85,7 +112,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "8e2f836b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Before moving to the logistic model, let us try to use our linear\n",
|
||||
"regression model to classify these two outcomes. We could for example\n",
|
||||
@@ -98,7 +128,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6b58f1ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- Equation labels as ordinary links -->\n",
|
||||
"<div id=\"_auto1\"></div>\n",
|
||||
@@ -113,12 +146,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "54e3604b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where $\\boldsymbol{y}$ is a vector representing the possible outcomes, $\\boldsymbol{X}$ is our\n",
|
||||
"$n\\times p$ design matrix and $\\boldsymbol{\\beta}$ represents our estimators/predictors.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The main problem with our function is that it takes values on the\n",
|
||||
"entire real axis. In the case of logistic regression, however, the\n",
|
||||
"labels $y_i$ are discrete variables. A typical example is the credit\n",
|
||||
@@ -134,13 +169,13 @@
|
||||
"favorable to use a ``soft\" classifier that outputs\n",
|
||||
"the probability of a given category. This leads us to the logistic function.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The following example on data for coronary heart disease (CHD) as function of age may serve as an illustration. In the code here we read and plot whether a person has had CHD (output = 1) or not (output = 0). This ouput is plotted the person's against age. Clearly, the figure shows that attempting to make a standard linear regression fit may not be very meaningful."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "cd1dd1c5",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -207,14 +242,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "2e06ee82",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"What we could attempt however is to plot the mean value for each group."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "a774f001",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -233,7 +272,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "e102fa56",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"We are now trying to find a function $f(y\\vert x)$, that is a function which gives us an expected value for the output $y$ with a given input $x$.\n",
|
||||
"In standard linear regression with a linear dependence on $x$, we would write this in terms of our model"
|
||||
@@ -241,7 +283,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "75d995ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"f(y_i\\vert x_i)=\\beta_0+\\beta_1 x_i.\n",
|
||||
@@ -250,7 +295,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "e2cacca5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"This expression implies however that $f(y_i\\vert x_i)$ could take any\n",
|
||||
"value from minus infinity to plus infinity. If we however let\n",
|
||||
@@ -261,9 +309,16 @@
|
||||
"us to a very popular model for the function $f$, namely the so-called\n",
|
||||
"Sigmoid function or logistic model. We will consider this function as\n",
|
||||
"representing the probability for finding a value of $y_i$ with a given\n",
|
||||
"$x_i$.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"$x_i$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "af9ac586",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## The logistic function\n",
|
||||
"\n",
|
||||
"Another widely studied model, is the so-called \n",
|
||||
@@ -281,7 +336,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "80aa3cbe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(t) = \\frac{1}{1+\\mathrm \\exp{-t}}=\\frac{\\exp{t}}{1+\\mathrm \\exp{t}}.\n",
|
||||
@@ -290,19 +348,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "28d49915",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Note that $1-p(t)= p(-t)$.\n",
|
||||
"\n",
|
||||
"## Examples of likelihood functions used in logistic regression and nueral networks\n",
|
||||
"\n",
|
||||
"Note that $1-p(t)= p(-t)$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eeff6cd9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Examples of likelihood functions used in logistic regression and neural networks\n",
|
||||
"\n",
|
||||
"The following code plots the logistic function, the step function and other functions we will encounter from here and on."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "5ecec05e",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -367,14 +436,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "5802efe3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"We assume now that we have two classes with $y_i$ either $0$ or $1$. Furthermore we assume also that we have only two parameters $\\beta$ in our fitting of the Sigmoid function, that is we define probabilities"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f85d14ea",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
@@ -386,7 +461,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6c4e0334",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where $\\boldsymbol{\\beta}$ are the weights we wish to extract from data, in our case $\\beta_0$ and $\\beta_1$. \n",
|
||||
"\n",
|
||||
@@ -395,7 +473,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "df7facc9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(y_i=0\\vert x_i, \\boldsymbol{\\beta}) = 1-p(y_i=1\\vert x_i, \\boldsymbol{\\beta}).\n",
|
||||
@@ -404,7 +485,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "a920e6d8",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In order to define the total likelihood for all possible outcomes from a \n",
|
||||
"dataset $\\mathcal{D}=\\{(y_i,x_i)\\}$, with the binary labels\n",
|
||||
@@ -416,7 +500,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "a780bfe1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
@@ -427,14 +514,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9f810a44",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"from which we obtain the log-likelihood and our **cost/loss** function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "3fac4ef4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left( y_i\\log{p(y_i=1|x_i,\\boldsymbol{\\beta})} + (1-y_i)\\log\\left[1-p(y_i=1|x_i,\\boldsymbol{\\beta}))\\right]\\right).\n",
|
||||
@@ -443,14 +536,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "75aa2c14",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Reordering the logarithms, we can rewrite the **cost/loss** function as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "780f2038",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
|
||||
@@ -459,7 +558,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c8c940aa",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to $\\beta$.\n",
|
||||
"Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that"
|
||||
@@ -467,7 +569,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9d4a527b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathcal{C}(\\boldsymbol{\\beta})=-\\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
|
||||
@@ -476,23 +581,27 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c1706606",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"This equation is known in statistics as the **cross entropy**. Finally, we note that just as in linear regression, \n",
|
||||
"in practice we often supplement the cross-entropy with additional regularization terms, usually $L_1$ and $L_2$ regularization as we did for Ridge and Lasso regression.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The cross entropy is a convex function of the weights $\\boldsymbol{\\beta}$ and,\n",
|
||||
"therefore, any local minimizer is a global minimizer. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Minimizing this\n",
|
||||
"cost function with respect to the two parameters $\\beta_0$ and $\\beta_1$ we obtain"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "fe8fb387",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_0} = -\\sum_{i=1}^n \\left(y_i -\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right),\n",
|
||||
@@ -501,14 +610,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "bb564e81",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "700c5443",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_1} = -\\sum_{i=1}^n \\left(y_ix_i -x_i\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right).\n",
|
||||
@@ -517,7 +632,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f442c956",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Let us now define a vector $\\boldsymbol{y}$ with $n$ elements $y_i$, an\n",
|
||||
"$n\\times p$ matrix $\\boldsymbol{X}$ which contains the $x_i$ values and a\n",
|
||||
@@ -527,7 +645,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "83937f6f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = -\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{p}\\right).\n",
|
||||
@@ -536,7 +657,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "71819c1d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"If we in addition define a diagonal matrix $\\boldsymbol{W}$ with elements \n",
|
||||
"$p(y_i\\vert x_i,\\boldsymbol{\\beta})(1-p(y_i\\vert x_i,\\boldsymbol{\\beta})$, we can obtain a compact expression of the second derivative as"
|
||||
@@ -544,7 +668,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "23dfd975",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial^2 \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}\\partial \\boldsymbol{\\beta}^T} = \\boldsymbol{X}^T\\boldsymbol{W}\\boldsymbol{X}.\n",
|
||||
@@ -553,14 +680,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6ca9f74b",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with $p$ predictors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "ddb3d93a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{ \\frac{p(\\boldsymbol{\\beta}\\boldsymbol{x})}{1-p(\\boldsymbol{\\beta}\\boldsymbol{x})}} = \\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p.\n",
|
||||
@@ -569,14 +702,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "0cfae560",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here we defined $\\boldsymbol{x}=[1,x_1,x_2,\\dots,x_p]$ and $\\boldsymbol{\\beta}=[\\beta_0, \\beta_1, \\dots, \\beta_p]$ leading to"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "7c427fc3",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(\\boldsymbol{\\beta}\\boldsymbol{x})=\\frac{ \\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}{1+\\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}.\n",
|
||||
@@ -585,7 +724,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "6a6814bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Till now we have mainly focused on two classes, the so-called binary\n",
|
||||
"system. Suppose we wish to extend to $K$ classes. Let us for the sake\n",
|
||||
@@ -594,7 +736,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "85282137",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=1\\vert x)}{p(K\\vert x)}} = \\beta_{10}+\\beta_{11}x_1,\n",
|
||||
@@ -603,14 +748,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "4864f76d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "390e9a55",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=2\\vert x)}{p(K\\vert x)}} = \\beta_{20}+\\beta_{21}x_1,\n",
|
||||
@@ -619,14 +770,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "c3f39c44",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and so on till the class $C=K-1$ class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "92f32a03",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=K-1\\vert x)}{p(K\\vert x)}} = \\beta_{(K-1)0}+\\beta_{(K-1)1}x_1,\n",
|
||||
@@ -635,13 +792,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "677a5c4d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and the model is specified in term of $K-1$ so-called log-odds or\n",
|
||||
"**logit** transformations.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"In our discussion of neural networks we will encounter the above again\n",
|
||||
"in terms of a slightly modified function, the so-called **Softmax** function.\n",
|
||||
"\n",
|
||||
@@ -658,7 +816,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "fa70e3bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(C=k\\vert \\mathbf {x} )=\\frac{\\exp{(\\beta_{k0}+\\beta_{k1}x_1)}}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}}.\n",
|
||||
@@ -667,14 +828,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "4c59400c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"It is easy to extend to more predictors. The final class is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "f4073652",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(C=K\\vert \\mathbf {x} )=\\frac{1}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}},\n",
|
||||
@@ -683,7 +850,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "810e872a",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and they sum to one. Our earlier discussions were all specialized to\n",
|
||||
"the case with two classes only. It is easy to see from the above that\n",
|
||||
@@ -692,8 +862,16 @@
|
||||
"To find the optimal parameters we would typically use a gradient\n",
|
||||
"descent method. Newton's method and gradient descent methods are\n",
|
||||
"discussed in the material on [optimization\n",
|
||||
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html).\n",
|
||||
"\n",
|
||||
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f4ebc1da",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Wisconsin Cancer Data\n",
|
||||
"\n",
|
||||
"We show here how we can use a simple regression case on the breast\n",
|
||||
@@ -703,7 +881,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "b7a8cbac",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -739,7 +918,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "10f74b93",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In addition to the above scores, we could also study the covariance (and the correlation matrix).\n",
|
||||
"We use **Pandas** to compute the correlation matrix."
|
||||
@@ -747,7 +929,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "d0b8025d",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -792,7 +975,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "9d84a2aa",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"In the above example we note two things. In the first plot we display\n",
|
||||
"the overlap of benign and malignant tumors as functions of the various\n",
|
||||
@@ -811,7 +997,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"id": "6144ea0a",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -823,14 +1010,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "5627f5ae",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and then"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "a7c8662a",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -842,17 +1033,46 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"id": "d03ee2a7",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Diagonalizing this matrix we can in turn say something about which\n",
|
||||
"features are of relevance and which are not. This leads us to\n",
|
||||
"the classical Principal Component Analysis (PCA) theorem with\n",
|
||||
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html))."
|
||||
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html)).\n",
|
||||
"\n",
|
||||
"Here we present a further way to present our results in terms of a so-called **confusion matrix**, the cumulative gain and the **ROC** curve.\n",
|
||||
"This way of displaying our data are based upon different ways to classify our possible outcomes. Before we proceed we need some definitions.\n",
|
||||
"1. **TP**: true positive or in other words, something equivalent with a proper classification\n",
|
||||
"\n",
|
||||
"2. **TN**: true negative, which is equivalent with a correct rejection\n",
|
||||
"\n",
|
||||
"3. **FP**: false positive, or in simpler words something that is equivalent with a false alarm\n",
|
||||
"\n",
|
||||
"4. **FN**: false negative, which is mean to be equivalent with a miss.\n",
|
||||
"\n",
|
||||
"The total data set is then the sum of the true positive and true negative targets or outputs, labeled by $n$.\n",
|
||||
"Based on this we can then define the accuracy score as the sum of correctly predicted **TP** and **TN** cases divided by the sum of true positive and treue negative events in our data set, or as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "44ce5095",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{Accuracy} = \\frac{\\sum_{i=0}^{n-1}I(y_i=\\tilde{y}_i)}{n}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 8,
|
||||
"id": "24ff3dd3",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
@@ -908,5 +1128,5 @@
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||