changed layout
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 61 KiB |
@@ -1623,7 +1623,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
|
||||
100.106 15.0037 100.104 0.149019
|
||||
100.135 15.1329 100.136 0.151103
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1848,9 +1848,7 @@ Error: 0.10398646080125035
|
||||
Bias^2: 0.1007711427354898
|
||||
Var: 0.0032153180657605116
|
||||
0.10398646080125035 >= 0.1007711427354898 + 0.0032153180657605116 = 0.10398646080125032
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 3
|
||||
Polynomial degree: 3
|
||||
Error: 0.06547790180152355
|
||||
Bias^2: 0.06208238634231949
|
||||
Var: 0.0033955154592040936
|
||||
@@ -1889,9 +1887,7 @@ Error: 0.02660572763718093
|
||||
Bias^2: 0.010018312644137363
|
||||
Var: 0.016587414993043573
|
||||
0.02660572763718093 >= 0.010018312644137363 + 0.016587414993043573 = 0.026605727637180936
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 10
|
||||
Polynomial degree: 10
|
||||
Error: 0.021592704588025025
|
||||
Bias^2: 0.010516485576645508
|
||||
Var: 0.011076219011379514
|
||||
@@ -1901,7 +1897,9 @@ Error: 0.07160048164233104
|
||||
Bias^2: 0.014436800088904942
|
||||
Var: 0.05716368155342608
|
||||
0.07160048164233104 >= 0.014436800088904942 + 0.05716368155342608 = 0.07160048164233102
|
||||
Polynomial degree: 12
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 12
|
||||
Error: 0.11547777218872497
|
||||
Bias^2: 0.01628578269596628
|
||||
Var: 0.09919198949275869
|
||||
@@ -1913,7 +1911,7 @@ Var: 0.20867052175034223
|
||||
0.22842468702219465 >= 0.01975416527185249 + 0.20867052175034223 = 0.2284246870221947
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/week37_139_5.png" src="_images/week37_139_5.png" />
|
||||
<img alt="_images/week37_139_4.png" src="_images/week37_139_4.png" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1946,31 +1944,24 @@ flexible statistical methods have higher variance.</p>
|
||||
</div>
|
||||
<div class="section" id="another-example-from-scikit-learn-s-repository">
|
||||
<h2>Another Example from Scikit-Learn’s Repository<a class="headerlink" href="#another-example-from-scikit-learn-s-repository" title="Permalink to this headline">¶</a></h2>
|
||||
<p>This example demonstrates the problems of underfitting and overfitting and
|
||||
how we can use linear regression with polynomial features to approximate
|
||||
nonlinear functions. The plot shows the function that we want to approximate,
|
||||
which is a part of the cosine function. In addition, the samples from the
|
||||
real function and the approximations of different models are displayed. The
|
||||
models have polynomial features of different degrees. We can see that a
|
||||
linear function (polynomial with degree 1) is not sufficient to fit the
|
||||
training samples. This is called <strong>underfitting</strong>. A polynomial of degree 4
|
||||
approximates the true function almost perfectly. However, for higher degrees
|
||||
the model will <strong>overfit</strong> the training data, i.e. it learns the noise of the
|
||||
training data.
|
||||
We evaluate quantitatively overfitting and underfitting by using
|
||||
cross-validation. We calculate the mean squared error (MSE) on the validation
|
||||
set, the higher, the less likely the model generalizes correctly from the
|
||||
training data.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
|
||||
<span class="sd">============================</span>
|
||||
<span class="sd">Underfitting vs. Overfitting</span>
|
||||
<span class="sd">============================</span>
|
||||
|
||||
<span class="sd">This example demonstrates the problems of underfitting and overfitting and</span>
|
||||
<span class="sd">how we can use linear regression with polynomial features to approximate</span>
|
||||
<span class="sd">nonlinear functions. The plot shows the function that we want to approximate,</span>
|
||||
<span class="sd">which is a part of the cosine function. In addition, the samples from the</span>
|
||||
<span class="sd">real function and the approximations of different models are displayed. The</span>
|
||||
<span class="sd">models have polynomial features of different degrees. We can see that a</span>
|
||||
<span class="sd">linear function (polynomial with degree 1) is not sufficient to fit the</span>
|
||||
<span class="sd">training samples. This is called **underfitting**. A polynomial of degree 4</span>
|
||||
<span class="sd">approximates the true function almost perfectly. However, for higher degrees</span>
|
||||
<span class="sd">the model will **overfit** the training data, i.e. it learns the noise of the</span>
|
||||
<span class="sd">training data.</span>
|
||||
<span class="sd">We evaluate quantitatively **overfitting** / **underfitting** by using</span>
|
||||
<span class="sd">cross-validation. We calculate the mean squared error (MSE) on the validation</span>
|
||||
<span class="sd">set, the higher, the less likely the model generalizes correctly from the</span>
|
||||
<span class="sd">training data.</span>
|
||||
<span class="sd">"""</span>
|
||||
|
||||
<span class="nb">print</span><span class="p">(</span><span class="vm">__doc__</span><span class="p">)</span>
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#print(__doc__)</span>
|
||||
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||||
@@ -2023,28 +2014,7 @@ flexible statistical methods have higher variance.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>============================
|
||||
Underfitting vs. Overfitting
|
||||
============================
|
||||
|
||||
This example demonstrates the problems of underfitting and overfitting and
|
||||
how we can use linear regression with polynomial features to approximate
|
||||
nonlinear functions. The plot shows the function that we want to approximate,
|
||||
which is a part of the cosine function. In addition, the samples from the
|
||||
real function and the approximations of different models are displayed. The
|
||||
models have polynomial features of different degrees. We can see that a
|
||||
linear function (polynomial with degree 1) is not sufficient to fit the
|
||||
training samples. This is called **underfitting**. A polynomial of degree 4
|
||||
approximates the true function almost perfectly. However, for higher degrees
|
||||
the model will **overfit** the training data, i.e. it learns the noise of the
|
||||
training data.
|
||||
We evaluate quantitatively **overfitting** / **underfitting** by using
|
||||
cross-validation. We calculate the mean squared error (MSE) on the validation
|
||||
set, the higher, the less likely the model generalizes correctly from the
|
||||
training data.
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/week37_142_1.png" src="_images/week37_142_1.png" />
|
||||
<img alt="_images/week37_142_0.png" src="_images/week37_142_0.png" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -2372,9 +2342,9 @@ Mean squared error on training data: 0.00063866
|
||||
Mean squared error on test data: 3099.60342978
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_92606/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_96719/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_92606/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
|
||||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_96719/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
|
||||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||||
</pre></div>
|
||||
</div>
|
||||
@@ -2459,7 +2429,7 @@ Mean squared error on test data: 3099.60342978
|
||||
</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_92606/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_96719/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
|
||||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||||
</pre></div>
|
||||
</div>
|
||||
|
||||
@@ -975,33 +975,27 @@ plt.show()
|
||||
# You may also find this recent [article](https://www.pnas.org/content/116/32/15849) of interest.
|
||||
|
||||
# ## Another Example from Scikit-Learn's Repository
|
||||
#
|
||||
# This example demonstrates the problems of underfitting and overfitting and
|
||||
# how we can use linear regression with polynomial features to approximate
|
||||
# nonlinear functions. The plot shows the function that we want to approximate,
|
||||
# which is a part of the cosine function. In addition, the samples from the
|
||||
# real function and the approximations of different models are displayed. The
|
||||
# models have polynomial features of different degrees. We can see that a
|
||||
# linear function (polynomial with degree 1) is not sufficient to fit the
|
||||
# training samples. This is called **underfitting**. A polynomial of degree 4
|
||||
# approximates the true function almost perfectly. However, for higher degrees
|
||||
# the model will **overfit** the training data, i.e. it learns the noise of the
|
||||
# training data.
|
||||
# We evaluate quantitatively overfitting and underfitting by using
|
||||
# cross-validation. We calculate the mean squared error (MSE) on the validation
|
||||
# set, the higher, the less likely the model generalizes correctly from the
|
||||
# training data.
|
||||
|
||||
# In[5]:
|
||||
|
||||
|
||||
"""
|
||||
============================
|
||||
Underfitting vs. Overfitting
|
||||
============================
|
||||
|
||||
This example demonstrates the problems of underfitting and overfitting and
|
||||
how we can use linear regression with polynomial features to approximate
|
||||
nonlinear functions. The plot shows the function that we want to approximate,
|
||||
which is a part of the cosine function. In addition, the samples from the
|
||||
real function and the approximations of different models are displayed. The
|
||||
models have polynomial features of different degrees. We can see that a
|
||||
linear function (polynomial with degree 1) is not sufficient to fit the
|
||||
training samples. This is called **underfitting**. A polynomial of degree 4
|
||||
approximates the true function almost perfectly. However, for higher degrees
|
||||
the model will **overfit** the training data, i.e. it learns the noise of the
|
||||
training data.
|
||||
We evaluate quantitatively **overfitting** / **underfitting** by using
|
||||
cross-validation. We calculate the mean squared error (MSE) on the validation
|
||||
set, the higher, the less likely the model generalizes correctly from the
|
||||
training data.
|
||||
"""
|
||||
|
||||
print(__doc__)
|
||||
#print(__doc__)
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 61 KiB |