This commit is contained in:
Morten Hjorth-Jensen
2024-09-10 06:14:12 +02:00
parent 6cd669aa24
commit 6ab196695f
7 changed files with 812 additions and 804 deletions
+19 -23
View File
@@ -293,35 +293,31 @@ MathJax.Hub.Config({
<!-- !split -->
<h2 id="another-example-from-scikit-learn-s-repository" class="anchor">Another Example from Scikit-Learn's Repository </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 <b>underfitting</b>. A polynomial of degree 4
approximates the true function almost perfectly. However, for higher degrees
the model will <b>overfit</b> 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>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic">============================</span>
<span style="color: #BA2121; font-style: italic">Underfitting vs. Overfitting</span>
<span style="color: #BA2121; font-style: italic">============================</span>
<span style="color: #BA2121; font-style: italic">This example demonstrates the problems of underfitting and overfitting and</span>
<span style="color: #BA2121; font-style: italic">how we can use linear regression with polynomial features to approximate</span>
<span style="color: #BA2121; font-style: italic">nonlinear functions. The plot shows the function that we want to approximate,</span>
<span style="color: #BA2121; font-style: italic">which is a part of the cosine function. In addition, the samples from the</span>
<span style="color: #BA2121; font-style: italic">real function and the approximations of different models are displayed. The</span>
<span style="color: #BA2121; font-style: italic">models have polynomial features of different degrees. We can see that a</span>
<span style="color: #BA2121; font-style: italic">linear function (polynomial with degree 1) is not sufficient to fit the</span>
<span style="color: #BA2121; font-style: italic">training samples. This is called **underfitting**. A polynomial of degree 4</span>
<span style="color: #BA2121; font-style: italic">approximates the true function almost perfectly. However, for higher degrees</span>
<span style="color: #BA2121; font-style: italic">the model will **overfit** the training data, i.e. it learns the noise of the</span>
<span style="color: #BA2121; font-style: italic">training data.</span>
<span style="color: #BA2121; font-style: italic">We evaluate quantitatively **overfitting** / **underfitting** by using</span>
<span style="color: #BA2121; font-style: italic">cross-validation. We calculate the mean squared error (MSE) on the validation</span>
<span style="color: #BA2121; font-style: italic">set, the higher, the less likely the model generalizes correctly from the</span>
<span style="color: #BA2121; font-style: italic">training data.</span>
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #008000">print</span>(<span style="color: #19177C">__doc__</span>)
<pre style="line-height: 125%;"><span style="color: #408080; font-style: italic">#print(__doc__)</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
+19 -23
View File
@@ -1489,35 +1489,31 @@ flexible statistical methods have higher variance.
<section>
<h2 id="another-example-from-scikit-learn-s-repository">Another Example from Scikit-Learn's Repository </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 <b>underfitting</b>. A polynomial of degree 4
approximates the true function almost perfectly. However, for higher degrees
the model will <b>overfit</b> 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>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #eeeedd">
<pre style="font-size: 80%; line-height: 125%;"><span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555">============================</span>
<span style="color: #CD5555">Underfitting vs. Overfitting</span>
<span style="color: #CD5555">============================</span>
<span style="color: #CD5555">This example demonstrates the problems of underfitting and overfitting and</span>
<span style="color: #CD5555">how we can use linear regression with polynomial features to approximate</span>
<span style="color: #CD5555">nonlinear functions. The plot shows the function that we want to approximate,</span>
<span style="color: #CD5555">which is a part of the cosine function. In addition, the samples from the</span>
<span style="color: #CD5555">real function and the approximations of different models are displayed. The</span>
<span style="color: #CD5555">models have polynomial features of different degrees. We can see that a</span>
<span style="color: #CD5555">linear function (polynomial with degree 1) is not sufficient to fit the</span>
<span style="color: #CD5555">training samples. This is called **underfitting**. A polynomial of degree 4</span>
<span style="color: #CD5555">approximates the true function almost perfectly. However, for higher degrees</span>
<span style="color: #CD5555">the model will **overfit** the training data, i.e. it learns the noise of the</span>
<span style="color: #CD5555">training data.</span>
<span style="color: #CD5555">We evaluate quantitatively **overfitting** / **underfitting** by using</span>
<span style="color: #CD5555">cross-validation. We calculate the mean squared error (MSE) on the validation</span>
<span style="color: #CD5555">set, the higher, the less likely the model generalizes correctly from the</span>
<span style="color: #CD5555">training data.</span>
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #658b00">print</span>(<span style="color: #00688B">__doc__</span>)
<pre style="font-size: 80%; line-height: 125%;"><span style="color: #228B22">#print(__doc__)</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
+19 -23
View File
@@ -1407,35 +1407,31 @@ flexible statistical methods have higher variance.
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="another-example-from-scikit-learn-s-repository">Another Example from Scikit-Learn's Repository </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 <b>underfitting</b>. A polynomial of degree 4
approximates the true function almost perfectly. However, for higher degrees
the model will <b>overfit</b> 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>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #eeeedd">
<pre style="line-height: 125%;"><span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555">============================</span>
<span style="color: #CD5555">Underfitting vs. Overfitting</span>
<span style="color: #CD5555">============================</span>
<span style="color: #CD5555">This example demonstrates the problems of underfitting and overfitting and</span>
<span style="color: #CD5555">how we can use linear regression with polynomial features to approximate</span>
<span style="color: #CD5555">nonlinear functions. The plot shows the function that we want to approximate,</span>
<span style="color: #CD5555">which is a part of the cosine function. In addition, the samples from the</span>
<span style="color: #CD5555">real function and the approximations of different models are displayed. The</span>
<span style="color: #CD5555">models have polynomial features of different degrees. We can see that a</span>
<span style="color: #CD5555">linear function (polynomial with degree 1) is not sufficient to fit the</span>
<span style="color: #CD5555">training samples. This is called **underfitting**. A polynomial of degree 4</span>
<span style="color: #CD5555">approximates the true function almost perfectly. However, for higher degrees</span>
<span style="color: #CD5555">the model will **overfit** the training data, i.e. it learns the noise of the</span>
<span style="color: #CD5555">training data.</span>
<span style="color: #CD5555">We evaluate quantitatively **overfitting** / **underfitting** by using</span>
<span style="color: #CD5555">cross-validation. We calculate the mean squared error (MSE) on the validation</span>
<span style="color: #CD5555">set, the higher, the less likely the model generalizes correctly from the</span>
<span style="color: #CD5555">training data.</span>
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #658b00">print</span>(<span style="color: #00688B">__doc__</span>)
<pre style="line-height: 125%;"><span style="color: #228B22">#print(__doc__)</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
+19 -23
View File
@@ -1484,35 +1484,31 @@ flexible statistical methods have higher variance.
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="another-example-from-scikit-learn-s-repository">Another Example from Scikit-Learn's Repository </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 <b>underfitting</b>. A polynomial of degree 4
approximates the true function almost perfectly. However, for higher degrees
the model will <b>overfit</b> 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>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic">============================</span>
<span style="color: #BA2121; font-style: italic">Underfitting vs. Overfitting</span>
<span style="color: #BA2121; font-style: italic">============================</span>
<span style="color: #BA2121; font-style: italic">This example demonstrates the problems of underfitting and overfitting and</span>
<span style="color: #BA2121; font-style: italic">how we can use linear regression with polynomial features to approximate</span>
<span style="color: #BA2121; font-style: italic">nonlinear functions. The plot shows the function that we want to approximate,</span>
<span style="color: #BA2121; font-style: italic">which is a part of the cosine function. In addition, the samples from the</span>
<span style="color: #BA2121; font-style: italic">real function and the approximations of different models are displayed. The</span>
<span style="color: #BA2121; font-style: italic">models have polynomial features of different degrees. We can see that a</span>
<span style="color: #BA2121; font-style: italic">linear function (polynomial with degree 1) is not sufficient to fit the</span>
<span style="color: #BA2121; font-style: italic">training samples. This is called **underfitting**. A polynomial of degree 4</span>
<span style="color: #BA2121; font-style: italic">approximates the true function almost perfectly. However, for higher degrees</span>
<span style="color: #BA2121; font-style: italic">the model will **overfit** the training data, i.e. it learns the noise of the</span>
<span style="color: #BA2121; font-style: italic">training data.</span>
<span style="color: #BA2121; font-style: italic">We evaluate quantitatively **overfitting** / **underfitting** by using</span>
<span style="color: #BA2121; font-style: italic">cross-validation. We calculate the mean squared error (MSE) on the validation</span>
<span style="color: #BA2121; font-style: italic">set, the higher, the less likely the model generalizes correctly from the</span>
<span style="color: #BA2121; font-style: italic">training data.</span>
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #008000">print</span>(<span style="color: #19177C">__doc__</span>)
<pre style="line-height: 125%;"><span style="color: #408080; font-style: italic">#print(__doc__)</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
Binary file not shown.
File diff suppressed because one or more lines are too long
+7 -10
View File
@@ -1102,11 +1102,6 @@ You may also find this recent "article":"https://www.pnas.org/content/116/32/158
!split
===== Another Example from Scikit-Learn's Repository =====
!bc pycod
"""
============================
Underfitting vs. Overfitting
============================
This example demonstrates the problems of underfitting and overfitting and
how we can use linear regression with polynomial features to approximate
@@ -1115,17 +1110,19 @@ 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
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
the model will _overfit_ the training data, i.e. it learns the noise of the
training data.
We evaluate quantitatively **overfitting** / **underfitting** by using
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.
"""
print(__doc__)
!bc pycod
#print(__doc__)
import numpy as np
import matplotlib.pyplot as plt