From ad3594d65e780ccdfdeca313e66c0c10032d4f40 Mon Sep 17 00:00:00 2001 From: Morten Hjorth-Jensen Date: Thu, 23 Sep 2021 10:45:55 +0200 Subject: [PATCH] small update on codes --- doc/pub/week38/html/._week38-bs014.html | 17 +++++ doc/pub/week38/html/._week38-bs015.html | 11 +-- doc/pub/week38/html/._week38-bs016.html | 11 +-- doc/pub/week38/html/week38-reveal.html | 45 +++++++---- doc/pub/week38/html/week38-solarized.html | 39 ++++++---- doc/pub/week38/html/week38.html | 39 ++++++---- doc/pub/week38/ipynb/ipynb-week38-src.tar.gz | Bin 192 -> 193 bytes doc/pub/week38/ipynb/week38.ipynb | 76 ++++++++++++++----- doc/src/week38/week38.do.txt | 41 ++++++---- 9 files changed, 197 insertions(+), 82 deletions(-) diff --git a/doc/pub/week38/html/._week38-bs014.html b/doc/pub/week38/html/._week38-bs014.html index affee3cad..6e43634a1 100644 --- a/doc/pub/week38/html/._week38-bs014.html +++ b/doc/pub/week38/html/._week38-bs014.html @@ -527,6 +527,23 @@ 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. +

+To remind the reader, the regularization term, with the intercept in Ridge regression is given by +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, +$$ + +but when we take out the intercept, this equation becomes +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +$$ + +

+For Lasso regression we have +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +$$ +

diff --git a/doc/pub/week38/html/._week38-bs015.html b/doc/pub/week38/html/._week38-bs015.html index 31ecfa924..d40055597 100644 --- a/doc/pub/week38/html/._week38-bs015.html +++ b/doc/pub/week38/html/._week38-bs015.html @@ -421,7 +421,7 @@ MathJax.Hub.Config({

Code Examples

-Armed with this wisdom, we attempt first simply set the intercept equal to False in our implementation of Ridge regression for yet another vanilla data set. +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.

@@ -456,10 +456,10 @@ X_train, X_test, y_train, y_test = train_tes p = Maxpolydegree I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 4, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train @@ -494,8 +494,9 @@ plt.show()

The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. -We see that the results agree very well. What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here).

diff --git a/doc/pub/week38/html/._week38-bs016.html b/doc/pub/week38/html/._week38-bs016.html index c70295620..f43f780bc 100644 --- a/doc/pub/week38/html/._week38-bs016.html +++ b/doc/pub/week38/html/._week38-bs016.html @@ -449,29 +449,24 @@ X = np.z # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - - - - #For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable X_train_mean = np.mean(X_train,axis=0) #Center by removing mean from each feature X_train_scaled = X_train - X_train_mean X_test_scaled = X_test - X_train_mean -#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered) +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) #Remove the intercept from the training data. y_scaler = np.mean(y_train) y_train_scaled = y_train - y_scaler - p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 1, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled) diff --git a/doc/pub/week38/html/week38-reveal.html b/doc/pub/week38/html/week38-reveal.html index 433918261..c25da1588 100644 --- a/doc/pub/week38/html/week38-reveal.html +++ b/doc/pub/week38/html/week38-reveal.html @@ -779,6 +779,29 @@ 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. + +

+To remind the reader, the regularization term, with the intercept in Ridge regression is given by +

 
+$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, +$$ +

 
+ +but when we take out the intercept, this equation becomes +

 
+$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +$$ +

 
+ +

+For Lasso regression we have +

 
+$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +$$ +

 
@@ -786,7 +809,7 @@ of \( \lambda \), this may lead to differeing MSE values.

Code Examples

-Armed with this wisdom, we attempt first simply set the intercept equal to False in our implementation of Ridge regression for yet another vanilla data set. +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.

@@ -821,10 +844,10 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=# Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 4, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train @@ -859,8 +882,9 @@ plt.show()

The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. -We see that the results agree very well. What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here). @@ -896,29 +920,24 @@ X = np.zeros((n,Maxpolydegree-1)) # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - - - - #For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable X_train_mean = np.mean(X_train,axis=0) #Center by removing mean from each feature X_train_scaled = X_train - X_train_mean X_test_scaled = X_test - X_train_mean -#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered) +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) #Remove the intercept from the training data. y_scaler = np.mean(y_train) y_train_scaled = y_train - y_scaler - p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 1, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled) diff --git a/doc/pub/week38/html/week38-solarized.html b/doc/pub/week38/html/week38-solarized.html index c3c51e05f..dddebe4df 100644 --- a/doc/pub/week38/html/week38-solarized.html +++ b/doc/pub/week38/html/week38-solarized.html @@ -914,13 +914,30 @@ 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. +

+To remind the reader, the regularization term, with the intercept in Ridge regression is given by +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, +$$ + +but when we take out the intercept, this equation becomes +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +$$ + +

+For Lasso regression we have +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +$$ +











Code Examples

-Armed with this wisdom, we attempt first simply set the intercept equal to False in our implementation of Ridge regression for yet another vanilla data set. +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.

@@ -955,10 +972,10 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=# Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 4, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train @@ -993,8 +1010,9 @@ plt.show()

The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. -We see that the results agree very well. What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here).











@@ -1030,29 +1048,24 @@ X = np.zeros((n,Maxpolydegree-1)) # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - - - - #For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable X_train_mean = np.mean(X_train,axis=0) #Center by removing mean from each feature X_train_scaled = X_train - X_train_mean X_test_scaled = X_test - X_train_mean -#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered) +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) #Remove the intercept from the training data. y_scaler = np.mean(y_train) y_train_scaled = y_train - y_scaler - p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 1, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled) diff --git a/doc/pub/week38/html/week38.html b/doc/pub/week38/html/week38.html index 0ce1a3258..7efe067f8 100644 --- a/doc/pub/week38/html/week38.html +++ b/doc/pub/week38/html/week38.html @@ -919,13 +919,30 @@ 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. +

+To remind the reader, the regularization term, with the intercept in Ridge regression is given by +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, +$$ + +but when we take out the intercept, this equation becomes +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +$$ + +

+For Lasso regression we have +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +$$ +











Code Examples

-Armed with this wisdom, we attempt first simply set the intercept equal to False in our implementation of Ridge regression for yet another vanilla data set. +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.

@@ -960,10 +977,10 @@ X_train, X_test, y_train, y_test = train_tes p = Maxpolydegree I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 4, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train @@ -998,8 +1015,9 @@ plt.show()

The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. -We see that the results agree very well. What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here).











@@ -1035,29 +1053,24 @@ X = np.z # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - - - - #For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable X_train_mean = np.mean(X_train,axis=0) #Center by removing mean from each feature X_train_scaled = X_train - X_train_mean X_test_scaled = X_test - X_train_mean -#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered) +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) #Remove the intercept from the training data. y_scaler = np.mean(y_train) y_train_scaled = y_train - y_scaler - p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 1, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled) diff --git a/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz b/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz index 2db2f457a61c9d789f3135daeaccb3daeed820cb..7aee2d84ddde531b785589000e93e220e1f4247c 100644 GIT binary patch literal 193 zcmV;y06za8iwFR=K1^W%1MSaC3c@fD2H>uHia9~aqfOlkcHu%0@d7DLZLCddlA^u6 zeSoeMH${Yeo1bBZVdjvnH@hrycNZ)MA;jVs#!Oi+Ocu`0vwc?ksHTq5ad0%?~a~i$H00;m8vancR literal 192 zcmV;x06+g9iwFSkDNJDi1MSaC3c@fD2H>uHia9|^nm*hLcHu%0@d7EG+E|;^Bt?6B z`v6@jZi)!`Hb0XL!^|ODZ+2N=@77xkAtZ?+7&A@cDM>ij6G~&C5e?b!n#L&*%3?-5 zAj_@v(izM3uu4;%Q5ux*`i8c${4i%e1)ljQj+HR5-S@Uq5~Q-2D^&jZ diff --git a/doc/pub/week38/ipynb/week38.ipynb b/doc/pub/week38/ipynb/week38.ipynb index 41daa5489..a1f1151f8 100644 --- a/doc/pub/week38/ipynb/week38.ipynb +++ b/doc/pub/week38/ipynb/week38.ipynb @@ -845,11 +845,57 @@ "regularization term does not include $\\beta_0$. For different values\n", "of $\\lambda$, this may lead to differeing MSE values.\n", "\n", - "\n", - "\n", + "To remind the reader, the regularization term, with the intercept in Ridge regression is given by" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "$$\n", + "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=0}^{p-1}\\beta_j^2,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "but when we take out the intercept, this equation becomes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "$$\n", + "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=1}^{p-1}\\beta_j^2.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For Lasso regression we have" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "$$\n", + "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_1 = \\lambda \\sum_{j=1}^{p-1}\\vert\\beta_j\\vert.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Code Examples\n", "\n", - "Armed with this wisdom, we attempt first simply set the intercept equal to **False** in our implementation of Ridge regression for yet another vanilla data set." + "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." ] }, { @@ -891,10 +937,10 @@ "p = Maxpolydegree\n", "I = np.eye(p,p)\n", "# Decide which values of lambda to use\n", - "nlambdas = 4\n", + "nlambdas = 6\n", "MSEOwnRidgePredict = np.zeros(nlambdas)\n", "MSERidgePredict = np.zeros(nlambdas)\n", - "lambdas = np.logspace(-4, 4, nlambdas)\n", + "lambdas = np.logspace(-4, 2, nlambdas)\n", "for i in range(nlambdas):\n", " lmb = lambdas[i]\n", " OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train\n", @@ -933,8 +979,9 @@ "metadata": {}, "source": [ "The results here agree when we force **Scikit-Learn**'s Ridge function to include the first column in our design matrix.\n", - "We see that the results agree very well. What happens if we do not include the intercept in our fit?\n", - "Let us see how we can change this code by zero centering.\n", + "We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix.\n", + "What happens if we do not include the intercept in our fit?\n", + "Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here).\n", "\n", "## Taking out the mean" ] @@ -975,29 +1022,24 @@ "# We split the data in test and training data\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n", "\n", - "\n", - "\n", - "\n", - "\n", "#For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable\n", "X_train_mean = np.mean(X_train,axis=0)\n", "#Center by removing mean from each feature\n", "X_train_scaled = X_train - X_train_mean \n", "X_test_scaled = X_test - X_train_mean\n", - "#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered)\n", + "#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered)\n", "#Remove the intercept from the training data.\n", "y_scaler = np.mean(y_train) \n", "y_train_scaled = y_train - y_scaler \n", "\n", - "\n", "p = Maxpolydegree-1\n", "I = np.eye(p,p)\n", "# Decide which values of lambda to use\n", - "nlambdas = 4\n", + "nlambdas = 6\n", "MSEOwnRidgePredict = np.zeros(nlambdas)\n", "MSERidgePredict = np.zeros(nlambdas)\n", "\n", - "lambdas = np.logspace(-4, 1, nlambdas)\n", + "lambdas = np.logspace(-4, 2, nlambdas)\n", "for i in range(nlambdas):\n", " lmb = lambdas[i]\n", " OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled)\n", @@ -1709,8 +1751,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "3\n", - "7\n", + "4\n", + "0\n", " \n", "<\n", "<\n", diff --git a/doc/src/week38/week38.do.txt b/doc/src/week38/week38.do.txt index 0c1e5acc2..2cad4ec81 100644 --- a/doc/src/week38/week38.do.txt +++ b/doc/src/week38/week38.do.txt @@ -559,12 +559,31 @@ 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. +To remind the reader, the regularization term, with the intercept in Ridge regression is given by +!bt +\[ +\lambda \vert\vert \bm{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, +\] +!et +but when we take out the intercept, this equation becomes +!bt +\[ +\lambda \vert\vert \bm{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +\] +!et + +For Lasso regression we have +!bt +\[ +\lambda \vert\vert \bm{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +\] +!et !split ===== Code Examples ===== -Armed with this wisdom, we attempt first simply set the intercept equal to _False_ in our implementation of Ridge regression for yet another vanilla data set. +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. !bc pycod import numpy as np @@ -597,10 +616,10 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) p = Maxpolydegree I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 4, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train @@ -636,8 +655,9 @@ plt.show() !ec The results here agree when we force _Scikit-Learn_'s Ridge function to include the first column in our design matrix. -We see that the results agree very well. What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here). !split ===== Taking out the mean ===== @@ -669,29 +689,24 @@ for degree in range(1,Maxpolydegree): #No intercept column # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - - - - #For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable X_train_mean = np.mean(X_train,axis=0) #Center by removing mean from each feature X_train_scaled = X_train - X_train_mean X_test_scaled = X_test - X_train_mean -#The model intercept (called y_scaler) is given by the mean of target variable (IF X is centered) +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) #Remove the intercept from the training data. y_scaler = np.mean(y_train) y_train_scaled = y_train - y_scaler - p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use -nlambdas = 4 +nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) -lambdas = np.logspace(-4, 1, nlambdas) +lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled)