more updates

This commit is contained in:
Morten Hjorth-Jensen
2021-09-23 09:17:15 +02:00
parent d69f43cbaf
commit ee68e6816a
8 changed files with 58 additions and 6 deletions
+2 -1
View File
@@ -423,6 +423,7 @@ MathJax.Hub.Config({
<p>
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 (<em>code example thanks to &#216;yvind Sigmundson Sch&#248;yen</em>). 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.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -502,7 +503,7 @@ intercept <span style="color: #666666">=</span> np<span style="color: #666666">.
ypredictOwn <span style="color: #666666">=</span> X <span style="color: #666666">@</span> beta
ypredictSKL <span style="color: #666666">=</span> skl<span style="color: #666666">.</span>predict(X)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE with Manual intercept&quot;</span>)
<span style="color: #008000">print</span>(MSE(y,ypredictOwn))
<span style="color: #008000">print</span>(MSE(y,ypredictOwn<span style="color: #666666">+</span>intercept))
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE with Sklearn intercept&quot;</span>)
<span style="color: #008000">print</span>(MSE(y,ypredictSKL))
+9
View File
@@ -508,6 +508,15 @@ plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39
plt<span style="color: #666666">.</span>legend()
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
We see here, when compared to the code which includes explicitely the
intercept column, that our MSE value is actually smaller. This is
because the regularization term does not include the intercept value \( \beta_0 \) in the
fitting. This applies to Lasso regularization as well.
<p>
If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
+10 -1
View File
@@ -676,6 +676,7 @@ What does this mean? And why do we insist on all this? Let us look at some examp
<p>
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 (<em>code example thanks to &#216;yvind Sigmundson Sch&#248;yen</em>). 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.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -755,7 +756,7 @@ intercept = np.mean(y_offset - X_offset @ beta)
ypredictOwn = X @ beta
ypredictSKL = skl.predict(X)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&quot;MSE with Manual intercept&quot;</span>)
<span style="color: #658b00">print</span>(MSE(y,ypredictOwn))
<span style="color: #658b00">print</span>(MSE(y,ypredictOwn+intercept))
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&quot;MSE with Sklearn intercept&quot;</span>)
<span style="color: #658b00">print</span>(MSE(y,ypredictSKL))
@@ -954,6 +955,14 @@ plt.ylabel(<span style="color: #CD5555">&#39;MSE&#39;</span>)
plt.legend()
plt.show()
</pre></div>
<p>
We see here, when compared to the code which includes explicitely the
intercept column, that our MSE value is actually smaller. This is
because the regularization term does not include the intercept value \( \beta_0 \) in the
fitting. This applies to Lasso regularization as well.
<p>
If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.
</section>
+11 -1
View File
@@ -810,6 +810,7 @@ What does this mean? And why do we insist on all this? Let us look at some examp
<p>
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 (<em>code example thanks to &#216;yvind Sigmundson Sch&#248;yen</em>). 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.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -889,7 +890,7 @@ intercept = np.mean(y_offset - X_offset @ beta)
ypredictOwn = X @ beta
ypredictSKL = skl.predict(X)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&quot;MSE with Manual intercept&quot;</span>)
<span style="color: #658b00">print</span>(MSE(y,ypredictOwn))
<span style="color: #658b00">print</span>(MSE(y,ypredictOwn+intercept))
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&quot;MSE with Sklearn intercept&quot;</span>)
<span style="color: #658b00">print</span>(MSE(y,ypredictSKL))
@@ -1088,6 +1089,15 @@ plt.ylabel(<span style="color: #CD5555">&#39;MSE&#39;</span>)
plt.legend()
plt.show()
</pre></div>
<p>
We see here, when compared to the code which includes explicitely the
intercept column, that our MSE value is actually smaller. This is
because the regularization term does not include the intercept value \( \beta_0 \) in the
fitting. This applies to Lasso regularization as well.
<p>
If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
+11 -1
View File
@@ -815,6 +815,7 @@ What does this mean? And why do we insist on all this? Let us look at some examp
<p>
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 (<em>code example thanks to &#216;yvind Sigmundson Sch&#248;yen</em>). 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.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -894,7 +895,7 @@ intercept <span style="color: #666666">=</span> np<span style="color: #666666">.
ypredictOwn <span style="color: #666666">=</span> X <span style="color: #666666">@</span> beta
ypredictSKL <span style="color: #666666">=</span> skl<span style="color: #666666">.</span>predict(X)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE with Manual intercept&quot;</span>)
<span style="color: #008000">print</span>(MSE(y,ypredictOwn))
<span style="color: #008000">print</span>(MSE(y,ypredictOwn<span style="color: #666666">+</span>intercept))
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE with Sklearn intercept&quot;</span>)
<span style="color: #008000">print</span>(MSE(y,ypredictSKL))
@@ -1093,6 +1094,15 @@ plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39
plt<span style="color: #666666">.</span>legend()
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
We see here, when compared to the code which includes explicitely the
intercept column, that our MSE value is actually smaller. This is
because the regularization term does not include the intercept value \( \beta_0 \) in the
fitting. This applies to Lasso regularization as well.
<p>
If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
Binary file not shown.
+8 -1
View File
@@ -818,7 +818,7 @@
"ypredictOwn = X @ beta\n",
"ypredictSKL = skl.predict(X)\n",
"print(f\"MSE with Manual intercept\")\n",
"print(MSE(y,ypredictOwn))\n",
"print(MSE(y,ypredictOwn+intercept))\n",
"print(f\"MSE with Sklearn intercept\")\n",
"print(MSE(y,ypredictSKL))\n",
"\n",
@@ -1039,6 +1039,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We see here, when compared to the code which includes explicitely the\n",
"intercept column, that our MSE value is actually smaller. This is\n",
"because the regularization term does not include the intercept value $\\beta_0$ in the\n",
"fitting. This applies to Lasso regularization as well.\n",
"\n",
"If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.\n",
"\n",
"## More complicated Example: The Ising model\n",
"\n",
"The one-dimensional Ising model with nearest neighbor interaction, no\n",
+7 -1
View File
@@ -457,6 +457,7 @@ What does this mean? And why do we insist on all this? Let us look at some examp
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.
!bc pycod
import numpy as np
import matplotlib.pyplot as plt
@@ -534,7 +535,7 @@ print(f"Sklearn fitted beta (without intercept): {skl.coef_}")
ypredictOwn = X @ beta
ypredictSKL = skl.predict(X)
print(f"MSE with Manual intercept")
print(MSE(y,ypredictOwn))
print(MSE(y,ypredictOwn+intercept))
print(f"MSE with Sklearn intercept")
print(MSE(y,ypredictSKL))
@@ -729,7 +730,12 @@ plt.show()
!ec
We see here, when compared to the code which includes explicitely the
intercept column, that our MSE value is actually smaller. This is
because the regularization term does not include the intercept value $\beta_0$ in the
fitting. This applies to Lasso regularization as well.
If we stay with ordinary least squares, there is no dependence on the value of the intercept when we perform the fitting.
!split
===== More complicated Example: The Ising model =====