update
This commit is contained in:
@@ -518,12 +518,13 @@ The intercept is the value of our output/target variable
|
||||
when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case).
|
||||
|
||||
<p>
|
||||
Printing the MSE, we see first that both methods give the same
|
||||
MSE. However, changing the value of the intercept gives a larger or
|
||||
smaller MSE, meaning that the MSE is penalized by the value of the
|
||||
intercept! Setting the intercept to true in the <b>Scikit-Learn</b>
|
||||
function or simply scaling our results, leads to a fit which is
|
||||
independent of the specific value of the intercept.
|
||||
Printing the MSE, we see first that both methods give the same MSE, as
|
||||
they should. However, when we move to for example Ridge regression,
|
||||
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.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
|
||||
@@ -421,7 +421,7 @@ MathJax.Hub.Config({
|
||||
<h2 id="code-examples" class="anchor">Code Examples </h2>
|
||||
|
||||
<p>
|
||||
Armed with this wisdom, we attempt first simply set the intercept eqault to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
Armed with this wisdom, we attempt first simply set the intercept equal to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -463,7 +463,6 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(nlambdas):
|
||||
lmb <span style="color: #666666">=</span> lambdas[i]
|
||||
OwnRidgeBeta <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>pinv(X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> X_train<span style="color: #666666">+</span>lmb<span style="color: #666666">*</span>I) <span style="color: #666666">@</span> X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> y_train
|
||||
<span style="color: #408080; font-style: italic"># include lasso using Scikit-Learn</span>
|
||||
<span style="color: #408080; font-style: italic"># Note: we include the intercept column and no scaling</span>
|
||||
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb,fit_intercept<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>)
|
||||
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
|
||||
@@ -478,6 +477,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(OwnRidgeBeta)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Beta values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSERidgePredict[i])
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE own Ridge Test'</span>)
|
||||
|
||||
@@ -478,16 +478,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
intercept_ <span style="color: #666666">=</span> y_scaler <span style="color: #666666">-</span> X_train_mean<span style="color: #AA22FF">@OwnRidgeBeta</span> <span style="color: #408080; font-style: italic">#The intercept can be shifted so the model can predict on uncentered data</span>
|
||||
<span style="color: #408080; font-style: italic">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge <span style="color: #666666">=</span> X_test <span style="color: #666666">@</span> OwnRidgeBeta <span style="color: #666666">+</span> intercept_
|
||||
<span style="color: #408080; font-style: italic">#EQUIVALENT PREDICTION:</span>
|
||||
<span style="color: #408080; font-style: italic">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge <span style="color: #666666">=</span> X_test_scaled <span style="color: #666666">@</span> OwnRidgeBeta <span style="color: #666666">+</span> y_scaler
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Values for own Ridge prediction"</span>)
|
||||
<span style="color: #008000">print</span>(ypredictOwnRidge)
|
||||
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb)
|
||||
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
|
||||
ypredictRidge <span style="color: #666666">=</span> RegRidge<span style="color: #666666">.</span>predict(X_test)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Values for SL Ridge prediction"</span>)
|
||||
<span style="color: #008000">print</span>(ypredictRidge)
|
||||
MSEOwnRidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictOwnRidge)
|
||||
MSERidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictRidge)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Beta values for own Ridge implementation"</span>)
|
||||
@@ -498,6 +493,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Intercept from Scikit-Learn Ridge implementation'</span>)
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSERidgePredict[i])
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
|
||||
@@ -771,12 +771,13 @@ The intercept is the value of our output/target variable
|
||||
when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case).
|
||||
|
||||
<p>
|
||||
Printing the MSE, we see first that both methods give the same
|
||||
MSE. However, changing the value of the intercept gives a larger or
|
||||
smaller MSE, meaning that the MSE is penalized by the value of the
|
||||
intercept! Setting the intercept to true in the <b>Scikit-Learn</b>
|
||||
function or simply scaling our results, leads to a fit which is
|
||||
independent of the specific value of the intercept.
|
||||
Printing the MSE, we see first that both methods give the same MSE, as
|
||||
they should. However, when we move to for example Ridge regression,
|
||||
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.
|
||||
</section>
|
||||
|
||||
|
||||
@@ -784,7 +785,7 @@ independent of the specific value of the intercept.
|
||||
<h2 id="code-examples">Code Examples </h2>
|
||||
|
||||
<p>
|
||||
Armed with this wisdom, we attempt first simply set the intercept eqault to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
Armed with this wisdom, we attempt first simply set the intercept equal to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -826,7 +827,6 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(nlambdas):
|
||||
lmb = lambdas[i]
|
||||
OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train
|
||||
<span style="color: #228B22"># include lasso using Scikit-Learn</span>
|
||||
<span style="color: #228B22"># Note: we include the intercept column and no scaling</span>
|
||||
RegRidge = linear_model.Ridge(lmb,fit_intercept=<span style="color: #8B008B; font-weight: bold">False</span>)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
@@ -841,6 +841,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(OwnRidgeBeta)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Beta values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(RegRidge.coef_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSERidgePredict[i])
|
||||
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r'</span>, label = <span style="color: #CD5555">'MSE own Ridge Test'</span>)
|
||||
@@ -919,16 +924,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
intercept_ = y_scaler - X_train_mean<span style="color: #707a7c">@OwnRidgeBeta</span> <span style="color: #228B22">#The intercept can be shifted so the model can predict on uncentered data</span>
|
||||
<span style="color: #228B22">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge = X_test @ OwnRidgeBeta + intercept_
|
||||
<span style="color: #228B22">#EQUIVALENT PREDICTION:</span>
|
||||
<span style="color: #228B22">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Values for own Ridge prediction"</span>)
|
||||
<span style="color: #658b00">print</span>(ypredictOwnRidge)
|
||||
RegRidge = linear_model.Ridge(lmb)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
ypredictRidge = RegRidge.predict(X_test)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Values for SL Ridge prediction"</span>)
|
||||
<span style="color: #658b00">print</span>(ypredictRidge)
|
||||
MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
|
||||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Beta values for own Ridge implementation"</span>)
|
||||
@@ -939,6 +939,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Intercept from Scikit-Learn Ridge implementation'</span>)
|
||||
<span style="color: #658b00">print</span>(RegRidge.intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSERidgePredict[i])
|
||||
|
||||
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
|
||||
@@ -905,12 +905,13 @@ The intercept is the value of our output/target variable
|
||||
when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case).
|
||||
|
||||
<p>
|
||||
Printing the MSE, we see first that both methods give the same
|
||||
MSE. However, changing the value of the intercept gives a larger or
|
||||
smaller MSE, meaning that the MSE is penalized by the value of the
|
||||
intercept! Setting the intercept to true in the <b>Scikit-Learn</b>
|
||||
function or simply scaling our results, leads to a fit which is
|
||||
independent of the specific value of the intercept.
|
||||
Printing the MSE, we see first that both methods give the same MSE, as
|
||||
they should. However, when we move to for example Ridge regression,
|
||||
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.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -918,7 +919,7 @@ independent of the specific value of the intercept.
|
||||
<h2 id="code-examples">Code Examples </h2>
|
||||
|
||||
<p>
|
||||
Armed with this wisdom, we attempt first simply set the intercept eqault to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
Armed with this wisdom, we attempt first simply set the intercept equal to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -960,7 +961,6 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(nlambdas):
|
||||
lmb = lambdas[i]
|
||||
OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train
|
||||
<span style="color: #228B22"># include lasso using Scikit-Learn</span>
|
||||
<span style="color: #228B22"># Note: we include the intercept column and no scaling</span>
|
||||
RegRidge = linear_model.Ridge(lmb,fit_intercept=<span style="color: #8B008B; font-weight: bold">False</span>)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
@@ -975,6 +975,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(OwnRidgeBeta)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Beta values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(RegRidge.coef_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSERidgePredict[i])
|
||||
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r'</span>, label = <span style="color: #CD5555">'MSE own Ridge Test'</span>)
|
||||
@@ -1053,16 +1058,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
intercept_ = y_scaler - X_train_mean<span style="color: #707a7c">@OwnRidgeBeta</span> <span style="color: #228B22">#The intercept can be shifted so the model can predict on uncentered data</span>
|
||||
<span style="color: #228B22">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge = X_test @ OwnRidgeBeta + intercept_
|
||||
<span style="color: #228B22">#EQUIVALENT PREDICTION:</span>
|
||||
<span style="color: #228B22">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Values for own Ridge prediction"</span>)
|
||||
<span style="color: #658b00">print</span>(ypredictOwnRidge)
|
||||
RegRidge = linear_model.Ridge(lmb)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
ypredictRidge = RegRidge.predict(X_test)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Values for SL Ridge prediction"</span>)
|
||||
<span style="color: #658b00">print</span>(ypredictRidge)
|
||||
MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
|
||||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Beta values for own Ridge implementation"</span>)
|
||||
@@ -1073,6 +1073,11 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Intercept from Scikit-Learn Ridge implementation'</span>)
|
||||
<span style="color: #658b00">print</span>(RegRidge.intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #658b00">print</span>(MSERidgePredict[i])
|
||||
|
||||
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
|
||||
@@ -910,12 +910,13 @@ The intercept is the value of our output/target variable
|
||||
when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case).
|
||||
|
||||
<p>
|
||||
Printing the MSE, we see first that both methods give the same
|
||||
MSE. However, changing the value of the intercept gives a larger or
|
||||
smaller MSE, meaning that the MSE is penalized by the value of the
|
||||
intercept! Setting the intercept to true in the <b>Scikit-Learn</b>
|
||||
function or simply scaling our results, leads to a fit which is
|
||||
independent of the specific value of the intercept.
|
||||
Printing the MSE, we see first that both methods give the same MSE, as
|
||||
they should. However, when we move to for example Ridge regression,
|
||||
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.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -923,7 +924,7 @@ independent of the specific value of the intercept.
|
||||
<h2 id="code-examples">Code Examples </h2>
|
||||
|
||||
<p>
|
||||
Armed with this wisdom, we attempt first simply set the intercept eqault to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
Armed with this wisdom, we attempt first simply set the intercept equal to <b>False</b> in our implementation of Ridge regression for yet another vanilla data set.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -965,7 +966,6 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(nlambdas):
|
||||
lmb <span style="color: #666666">=</span> lambdas[i]
|
||||
OwnRidgeBeta <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>pinv(X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> X_train<span style="color: #666666">+</span>lmb<span style="color: #666666">*</span>I) <span style="color: #666666">@</span> X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> y_train
|
||||
<span style="color: #408080; font-style: italic"># include lasso using Scikit-Learn</span>
|
||||
<span style="color: #408080; font-style: italic"># Note: we include the intercept column and no scaling</span>
|
||||
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb,fit_intercept<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>)
|
||||
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
|
||||
@@ -980,6 +980,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(OwnRidgeBeta)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Beta values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSERidgePredict[i])
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE own Ridge Test'</span>)
|
||||
@@ -1058,16 +1063,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
intercept_ <span style="color: #666666">=</span> y_scaler <span style="color: #666666">-</span> X_train_mean<span style="color: #AA22FF">@OwnRidgeBeta</span> <span style="color: #408080; font-style: italic">#The intercept can be shifted so the model can predict on uncentered data</span>
|
||||
<span style="color: #408080; font-style: italic">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge <span style="color: #666666">=</span> X_test <span style="color: #666666">@</span> OwnRidgeBeta <span style="color: #666666">+</span> intercept_
|
||||
<span style="color: #408080; font-style: italic">#EQUIVALENT PREDICTION:</span>
|
||||
<span style="color: #408080; font-style: italic">#Add intercept to prediction</span>
|
||||
ypredictOwnRidge <span style="color: #666666">=</span> X_test_scaled <span style="color: #666666">@</span> OwnRidgeBeta <span style="color: #666666">+</span> y_scaler
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Values for own Ridge prediction"</span>)
|
||||
<span style="color: #008000">print</span>(ypredictOwnRidge)
|
||||
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb)
|
||||
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
|
||||
ypredictRidge <span style="color: #666666">=</span> RegRidge<span style="color: #666666">.</span>predict(X_test)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Values for SL Ridge prediction"</span>)
|
||||
<span style="color: #008000">print</span>(ypredictRidge)
|
||||
MSEOwnRidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictOwnRidge)
|
||||
MSERidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictRidge)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Beta values for own Ridge implementation"</span>)
|
||||
@@ -1078,6 +1078,11 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Intercept from Scikit-Learn Ridge implementation'</span>)
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for own Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSEOwnRidgePredict[i])
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"MSE values for Scikit-Learn Ridge implementation"</span>)
|
||||
<span style="color: #008000">print</span>(MSERidgePredict[i])
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
|
||||
Binary file not shown.
@@ -837,17 +837,19 @@
|
||||
"The intercept is the value of our output/target variable\n",
|
||||
"when all our features are zero and our function crosses the $y$-axis (for a one-dimensional case). \n",
|
||||
"\n",
|
||||
"Printing the MSE, we see first that both methods give the same\n",
|
||||
"MSE. However, changing the value of the intercept gives a larger or\n",
|
||||
"smaller MSE, meaning that the MSE is penalized by the value of the\n",
|
||||
"intercept! Setting the intercept to true in the **Scikit-Learn**\n",
|
||||
"function or simply scaling our results, leads to a fit which is\n",
|
||||
"independent of the specific value of the intercept.\n",
|
||||
"Printing the MSE, we see first that both methods give the same MSE, as\n",
|
||||
"they should. However, when we move to for example Ridge regression,\n",
|
||||
"the way we treat the intercept may give a larger or smaller MSE,\n",
|
||||
"meaning that the MSE can be penalized by the value of the\n",
|
||||
"intercept. Not including the intercept in the fit, means that the\n",
|
||||
"regularization term does not include $\\beta_0$. For different values\n",
|
||||
"of $\\lambda$, this may lead to differeing MSE values.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Code Examples\n",
|
||||
"\n",
|
||||
"Armed with this wisdom, we attempt first simply set the intercept eqault to **False** in our implementation of Ridge regression for yet another vanilla data set."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -896,7 +898,6 @@
|
||||
"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",
|
||||
" # include lasso using Scikit-Learn\n",
|
||||
" # Note: we include the intercept column and no scaling\n",
|
||||
" RegRidge = linear_model.Ridge(lmb,fit_intercept=False)\n",
|
||||
" RegRidge.fit(X_train,y_train)\n",
|
||||
@@ -911,6 +912,11 @@
|
||||
" print(OwnRidgeBeta)\n",
|
||||
" print(\"Beta values for Scikit-Learn Ridge implementation\")\n",
|
||||
" print(RegRidge.coef_)\n",
|
||||
" print(\"MSE values for own Ridge implementation\")\n",
|
||||
" print(MSEOwnRidgePredict[i])\n",
|
||||
" print(\"MSE values for Scikit-Learn Ridge implementation\")\n",
|
||||
" print(MSERidgePredict[i])\n",
|
||||
"\n",
|
||||
"# Now plot the results\n",
|
||||
"plt.figure()\n",
|
||||
"plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE own Ridge Test')\n",
|
||||
@@ -998,16 +1004,11 @@
|
||||
" intercept_ = y_scaler - X_train_mean@OwnRidgeBeta #The intercept can be shifted so the model can predict on uncentered data\n",
|
||||
" #Add intercept to prediction\n",
|
||||
" ypredictOwnRidge = X_test @ OwnRidgeBeta + intercept_ \n",
|
||||
" #EQUIVALENT PREDICTION:\n",
|
||||
" #Add intercept to prediction\n",
|
||||
" ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler \n",
|
||||
" print(\"Values for own Ridge prediction\")\n",
|
||||
" print(ypredictOwnRidge)\n",
|
||||
" RegRidge = linear_model.Ridge(lmb)\n",
|
||||
" RegRidge.fit(X_train,y_train)\n",
|
||||
" ypredictRidge = RegRidge.predict(X_test)\n",
|
||||
" print(\"Values for SL Ridge prediction\")\n",
|
||||
" print(ypredictRidge)\n",
|
||||
" MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)\n",
|
||||
" MSERidgePredict[i] = MSE(y_test,ypredictRidge)\n",
|
||||
" print(\"Beta values for own Ridge implementation\")\n",
|
||||
@@ -1018,6 +1019,11 @@
|
||||
" print(intercept_)\n",
|
||||
" print('Intercept from Scikit-Learn Ridge implementation')\n",
|
||||
" print(RegRidge.intercept_)\n",
|
||||
" print(\"MSE values for own Ridge implementation\")\n",
|
||||
" print(MSEOwnRidgePredict[i])\n",
|
||||
" print(\"MSE values for Scikit-Learn Ridge implementation\")\n",
|
||||
" print(MSERidgePredict[i])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Now plot the results\n",
|
||||
"plt.figure()\n",
|
||||
|
||||
@@ -550,18 +550,20 @@ plt.show()
|
||||
The intercept is the value of our output/target variable
|
||||
when all our features are zero and our function crosses the $y$-axis (for a one-dimensional case).
|
||||
|
||||
Printing the MSE, we see first that both methods give the same
|
||||
MSE. However, changing the value of the intercept gives a larger or
|
||||
smaller MSE, meaning that the MSE is penalized by the value of the
|
||||
intercept! Setting the intercept to true in the _Scikit-Learn_
|
||||
function or simply scaling our results, leads to a fit which is
|
||||
independent of the specific value of the intercept.
|
||||
Printing the MSE, we see first that both methods give the same MSE, as
|
||||
they should. However, when we move to for example Ridge regression,
|
||||
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.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
===== Code Examples =====
|
||||
|
||||
Armed with this wisdom, we attempt first simply set the intercept eqault to _False_ in our implementation of Ridge regression for yet another vanilla data set.
|
||||
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.
|
||||
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
@@ -601,7 +603,6 @@ lambdas = np.logspace(-4, 4, 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
|
||||
# include lasso using Scikit-Learn
|
||||
# Note: we include the intercept column and no scaling
|
||||
RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
@@ -616,6 +617,11 @@ for i in range(nlambdas):
|
||||
print(OwnRidgeBeta)
|
||||
print("Beta values for Scikit-Learn Ridge implementation")
|
||||
print(RegRidge.coef_)
|
||||
print("MSE values for own Ridge implementation")
|
||||
print(MSEOwnRidgePredict[i])
|
||||
print("MSE values for Scikit-Learn Ridge implementation")
|
||||
print(MSERidgePredict[i])
|
||||
|
||||
# Now plot the results
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE own Ridge Test')
|
||||
@@ -691,16 +697,11 @@ for i in range(nlambdas):
|
||||
intercept_ = y_scaler - X_train_mean@OwnRidgeBeta #The intercept can be shifted so the model can predict on uncentered data
|
||||
#Add intercept to prediction
|
||||
ypredictOwnRidge = X_test @ OwnRidgeBeta + intercept_
|
||||
#EQUIVALENT PREDICTION:
|
||||
#Add intercept to prediction
|
||||
ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler
|
||||
print("Values for own Ridge prediction")
|
||||
print(ypredictOwnRidge)
|
||||
RegRidge = linear_model.Ridge(lmb)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
ypredictRidge = RegRidge.predict(X_test)
|
||||
print("Values for SL Ridge prediction")
|
||||
print(ypredictRidge)
|
||||
MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
|
||||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||||
print("Beta values for own Ridge implementation")
|
||||
@@ -711,6 +712,11 @@ for i in range(nlambdas):
|
||||
print(intercept_)
|
||||
print('Intercept from Scikit-Learn Ridge implementation')
|
||||
print(RegRidge.intercept_)
|
||||
print("MSE values for own Ridge implementation")
|
||||
print(MSEOwnRidgePredict[i])
|
||||
print("MSE values for Scikit-Learn Ridge implementation")
|
||||
print(MSERidgePredict[i])
|
||||
|
||||
|
||||
# Now plot the results
|
||||
plt.figure()
|
||||
|
||||
Reference in New Issue
Block a user