This commit is contained in:
Morten Hjorth-Jensen
2022-10-04 17:50:07 +02:00
parent ca80391647
commit e48ba29bba
225 changed files with 6376 additions and 4975 deletions
+294 -204
View File
@@ -7,8 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>5. Resampling Methods &#8212; Applied Data Analysis and Machine Learning</title>
<link href="_static/css/theme.css" rel="stylesheet" />
<link href="_static/css/index.c5995385ac14fb8791e8eb36b4908be2.css" rel="stylesheet" />
<link href="_static/css/theme.css" rel="stylesheet">
<link href="_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
<link rel="stylesheet"
@@ -31,31 +31,37 @@
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.1c5a1a01449ed65a7b51.js">
<link rel="preload" as="script" href="_static/js/index.be7d3bbb2ef33a8344ce.js">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/togglebutton.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
<script src="_static/sphinx-book-theme.d59cb220de22ca1c485ebbdc042f0030.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="6. Logistic Regression" href="chapter4.html" />
<link rel="prev" title="4. Ridge and Lasso Regression" href="chapter2.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
<meta name="docsearch:language" content="None">
<!-- Google Analytics -->
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
@@ -94,7 +100,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
About the course
</span>
@@ -116,7 +122,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Review of Statistics with Resampling Techniques and Linear Algebra
</span>
@@ -133,7 +139,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
From Regression to Support Vector Machines
</span>
@@ -170,7 +176,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Decision Trees, Ensemble Methods and Boosting
</span>
@@ -187,7 +193,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Dimensionality Reduction
</span>
@@ -204,7 +210,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Deep Learning Methods
</span>
@@ -281,7 +287,7 @@ const thebe_selector_output = ".output, .cell_output"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
onclick="printPdf(this)" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
@@ -299,7 +305,7 @@ const thebe_selector_output = ".output, .cell_output"
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="d-none d-md-block col-md-2 bd-toc show noprint">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
@@ -393,7 +399,104 @@ const thebe_selector_output = ".output, .cell_output"
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<!-- Table of contents that is only displayed when printing the page -->
<div id="jb-print-docs-body" class="onlyprint">
<h1>Resampling Methods</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#introduction">
5.1. Introduction
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#reminder-on-statistics">
5.2. Reminder on Statistics
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#id1">
5.3. Resampling methods
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#bootstrap">
5.3.1. Bootstrap
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#the-bias-variance-tradeoff">
5.4. The bias-variance tradeoff
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#cross-validation">
5.5. Cross-validation
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#more-on-rescaling-data">
5.6. More on Rescaling data
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#more-complicated-example-the-ising-model">
5.7. More complicated Example: The Ising model
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercises-and-projects">
5.8. Exercises and Projects
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-ordinary-least-square-ols-on-the-franke-function">
5.8.1. Exercise: Ordinary Least Square (OLS) on the Franke function
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-bias-variance-trade-off-and-resampling-techniques">
5.8.2. Exercise: Bias-variance trade-off and resampling techniques
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-cross-validation-as-resampling-techniques-adding-more-complexity">
5.8.3. Exercise: Cross-validation as resampling techniques, adding more complexity
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-ridge-regression-on-the-franke-function-with-resampling">
5.8.4. Exercise: Ridge Regression on the Franke function with resampling
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-lasso-regression-on-the-franke-function-with-resampling">
5.8.5. Exercise: Lasso Regression on the Franke function with resampling
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise-analysis-of-real-data">
5.8.6. Exercise: Analysis of real data
</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div>
<div class="tex2jax_ignore mathjax_ignore section" id="resampling-methods">
@@ -657,10 +760,10 @@ number <span class="math notranslate nohighlight">\(i\)</span> is left out. Usin
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Runtime: 0.0896981 sec
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Runtime: 0.0903549 sec
Jackknife Statistics :
original bias std. error
100.213 100.203 0.148564
100.107 100.097 0.150184
</pre></div>
</div>
</div>
@@ -879,7 +982,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
99.8879 15.0782 99.8894 0.149213
99.9033 14.9678 99.904 0.151348
</pre></div>
</div>
</div>
@@ -1015,10 +1118,10 @@ We use a more compact notation in terms of the expectation value</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Error: 0.013121574061370796
Bias^2: 0.012073649472576395
Var: 0.0010479245887943952
0.013121574061370796 &gt;= 0.012073649472576395 + 0.0010479245887943952 = 0.01312157406137079
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Error: 0.013121574062587286
Bias^2: 0.012073649469946107
Var: 0.0010479245926411787
0.013121574062587286 &gt;= 0.012073649469946107 + 0.0010479245926411787 = 0.013121574062587286
</pre></div>
</div>
<img alt="_images/chapter3_61_1.png" src="_images/chapter3_61_1.png" />
@@ -1089,71 +1192,68 @@ Var: 0.004579219539673834
Polynomial degree: 2
Error: 0.10398646080125037
Bias^2: 0.10077114273548984
Var: 0.0032153180657605125
0.10398646080125037 &gt;= 0.10077114273548984 + 0.0032153180657605125 = 0.10398646080125036
Var: 0.0032153180657605116
0.10398646080125037 &gt;= 0.10077114273548984 + 0.0032153180657605116 = 0.10398646080125036
Polynomial degree: 3
Error: 0.06547790180152352
Bias^2: 0.06208238634231944
Var: 0.003395515459204093
0.06547790180152352 &gt;= 0.06208238634231944 + 0.003395515459204093 = 0.06547790180152353
Bias^2: 0.062082386342319454
Var: 0.0033955154592040923
0.06547790180152352 &gt;= 0.062082386342319454 + 0.0033955154592040923 = 0.06547790180152355
Polynomial degree: 4
Error: 0.06844519414009442
Error: 0.06844519414009445
Bias^2: 0.06453579006728322
Var: 0.003909404072811217
0.06844519414009442 &gt;= 0.06453579006728322 + 0.003909404072811217 = 0.06844519414009444
Polynomial degree: 5
Error: 0.052279218012057004
Bias^2: 0.048187277304303056
Var: 0.004091940707753948
0.052279218012057004 &gt;= 0.048187277304303056 + 0.004091940707753948 = 0.052279218012057004
Var: 0.003909404072811221
0.06844519414009445 &gt;= 0.06453579006728322 + 0.003909404072811221 = 0.06844519414009444
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 6
Error: 0.037813671417388985
Bias^2: 0.033657685071527624
Var: 0.004155986345861364
0.037813671417388985 &gt;= 0.033657685071527624 + 0.004155986345861364 = 0.03781367141738899
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 5
Error: 0.05227921801205679
Bias^2: 0.04818727730430286
Var: 0.004091940707753925
0.05227921801205679 &gt;= 0.04818727730430286 + 0.004091940707753925 = 0.05227921801205679
Polynomial degree: 6
Error: 0.03781367141738902
Bias^2: 0.03365768507152769
Var: 0.0041559863458613296
0.03781367141738902 &gt;= 0.03365768507152769 + 0.0041559863458613296 = 0.03781367141738902
Polynomial degree: 7
Error: 0.027609773491022407
Bias^2: 0.0229994982603662
Var: 0.004610275230656187
0.027609773491022407 &gt;= 0.0229994982603662 + 0.004610275230656187 = 0.027609773491022387
Error: 0.027609773491022394
Bias^2: 0.022999498260366198
Var: 0.004610275230656182
0.027609773491022394 &gt;= 0.022999498260366198 + 0.004610275230656182 = 0.02760977349102238
Polynomial degree: 8
Error: 0.017355848195593354
Bias^2: 0.010331721306655144
Var: 0.0070241268889382116
0.017355848195593354 &gt;= 0.010331721306655144 + 0.0070241268889382116 = 0.017355848195593354
Polynomial degree:
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 9
Error: 0.026605727637184613
Bias^2: 0.010018312644139205
Var: 0.016587414993045405
0.026605727637184613 &gt;= 0.010018312644139205 + 0.016587414993045405 = 0.02660572763718461
Error: 0.017355848195593312
Bias^2: 0.010331721306655165
Var: 0.007024126888938144
0.017355848195593312 &gt;= 0.010331721306655165 + 0.007024126888938144 = 0.01735584819559331
Polynomial degree: 9
Error: 0.026605727637184558
Bias^2: 0.010018312644139219
Var: 0.016587414993045335
0.026605727637184558 &gt;= 0.010018312644139219 + 0.016587414993045335 = 0.026605727637184554
Polynomial degree: 10
Error: 0.021592704588021167
Bias^2: 0.010516485576646513
Var: 0.01107621901137465
0.021592704588021167 &gt;= 0.010516485576646513 + 0.01107621901137465 = 0.021592704588021164
Error: 0.021592704588021178
Bias^2: 0.010516485576646504
Var: 0.01107621901137467
0.021592704588021178 &gt;= 0.010516485576646504 + 0.01107621901137467 = 0.021592704588021174
Polynomial degree: 11
Error: 0.07160048164232467
Bias^2: 0.014436800088896274
Var: 0.057163681553428394
0.07160048164232467 &gt;= 0.014436800088896274 + 0.057163681553428394 = 0.07160048164232467
Error: 0.07160048164232538
Bias^2: 0.014436800088896381
Var: 0.05716368155342902
0.07160048164232538 &gt;= 0.014436800088896381 + 0.05716368155342902 = 0.0716004816423254
Polynomial degree: 12
Error: 0.11547777218875695
Bias^2: 0.016285782696017055
Var: 0.0991919894927399
0.11547777218875695 &gt;= 0.016285782696017055 + 0.0991919894927399 = 0.11547777218875696
Error: 0.11547777218876518
Bias^2: 0.016285782696017142
Var: 0.09919198949274803
0.11547777218876518 &gt;= 0.016285782696017142 + 0.09919198949274803 = 0.11547777218876518
Polynomial degree: 13
Error: 0.2284246870217459
Bias^2: 0.019754165271682844
Var: 0.20867052175006306
0.2284246870217459 &gt;= 0.019754165271682844 + 0.20867052175006306 = 0.2284246870217459
Error: 0.2284246870217162
Bias^2: 0.01975416527168255
Var: 0.20867052175003364
0.2284246870217162 &gt;= 0.01975416527168255 + 0.20867052175003364 = 0.2284246870217162
</pre></div>
</div>
<img alt="_images/chapter3_62_3.png" src="_images/chapter3_62_3.png" />
<img alt="_images/chapter3_62_2.png" src="_images/chapter3_62_2.png" />
</div>
</div>
<p>The bias-variance tradeoff summarizes the fundamental tension in
@@ -1366,7 +1466,9 @@ training data.
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 1
Mean squared error on training data: 439230.69504801
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Mean squared error on training data: 439230.69504801
Mean squared error on test data: 481979.17861098
Degree of polynomial: 2
Mean squared error on training data: 115822.95008046
@@ -1383,12 +1485,12 @@ Mean squared error on test data: 5.98822371
Degree of polynomial: 6
Mean squared error on training data: 3.66204648
Mean squared error on test data: 8.14812206
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 7
Degree of polynomial: 7
Mean squared error on training data: 0.47075725
Mean squared error on test data: 2.00607783
Degree of polynomial: 8
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 8
Mean squared error on training data: 0.04912436
Mean squared error on test data: 0.21596432
Degree of polynomial: 9
@@ -1403,72 +1505,72 @@ Mean squared error on test data: 1.35533773
Degree of polynomial: 12
Mean squared error on training data: 0.00813803
Mean squared error on test data: 0.17446471
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 13
Degree of polynomial: 13
Mean squared error on training data: 0.00759119
Mean squared error on test data: 1.08131003
Degree of polynomial: 14
Mean squared error on training data: 0.00472199
Mean squared error on test data: 0.81333804
Degree of polynomial: 15
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 15
Mean squared error on training data: 0.00410478
Mean squared error on test data: 92.09172408
Mean squared error on test data: 92.09172409
Degree of polynomial: 16
Mean squared error on training data: 0.00315593
Mean squared error on test data: 234.38533184
Mean squared error on test data: 234.38533185
Degree of polynomial: 17
Mean squared error on training data: 0.00242999
Mean squared error on test data: 1271.35771842
Mean squared error on test data: 1271.35771826
Degree of polynomial: 18
Mean squared error on training data: 0.00228741
Mean squared error on test data: 108.27092897
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 19
Mean squared error on training data: 0.00156382
Mean squared error on test data: 1371.99049330
Mean squared error on training data: 0.00228742
Mean squared error on test data: 108.27092910
Degree of polynomial: 19
Mean squared error on training data: 0.00156376
Mean squared error on test data: 1371.99051150
Degree of polynomial: 20
Mean squared error on training data: 0.00137823
Mean squared error on test data: 1887.85953586
Degree of polynomial: 21
Mean squared error on training data: 0.00118504
Mean squared error on test data: 14859.70127680
Mean squared error on training data: 0.00137818
Mean squared error on test data: 1887.86252988
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 21
Mean squared error on training data: 0.00118508
Mean squared error on test data: 14859.69908626
Degree of polynomial: 22
Mean squared error on training data: 0.00092645
Mean squared error on test data: 876.51214899
Mean squared error on training data: 0.00092647
Mean squared error on test data: 876.51191552
Degree of polynomial: 23
Mean squared error on training data: 0.00085883
Mean squared error on test data: 5594.60685864
Mean squared error on training data: 0.00085889
Mean squared error on test data: 5594.60815105
Degree of polynomial: 24
Mean squared error on training data: 0.00084711
Mean squared error on test data: 1277.60619654
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 25
Mean squared error on training data: 0.00079130
Mean squared error on test data: 128664.09744272
Mean squared error on training data: 0.00084705
Mean squared error on test data: 1277.61702282
Degree of polynomial: 25
Mean squared error on training data: 0.00079129
Mean squared error on test data: 128664.31650694
Degree of polynomial: 26
Mean squared error on training data: 0.00076919
Mean squared error on test data: 19003.95079764
Degree of polynomial: 27
Mean squared error on training data: 0.00068941
Mean squared error on test data: 2379.66226149
Degree of polynomial: 28
Mean squared error on training data: 0.00062582
Mean squared error on test data: 4082.19994371
Degree of polynomial: 29
Mean squared error on training data: 0.00060708
Mean squared error on test data: 3250.24770094
Mean squared error on training data: 0.00076905
Mean squared error on test data: 19003.94822514
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-7-40a38ad763f1&gt;:73: RuntimeWarning: divide by zero encountered in log10
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 27
Mean squared error on training data: 0.00068946
Mean squared error on test data: 2379.66219404
Degree of polynomial: 28
Mean squared error on training data: 0.00062595
Mean squared error on test data: 4082.19983530
Degree of polynomial: 29
Mean squared error on training data: 0.00060705
Mean squared error on test data: 3250.17647619
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(trainingerror), label=&#39;Training Error&#39;)
&lt;ipython-input-7-40a38ad763f1&gt;:74: RuntimeWarning: divide by zero encountered in log10
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(testerror), label=&#39;Test Error&#39;)
</pre></div>
</div>
<img alt="_images/chapter3_65_6.png" src="_images/chapter3_65_6.png" />
<img alt="_images/chapter3_65_7.png" src="_images/chapter3_65_7.png" />
</div>
</div>
</div>
@@ -1698,7 +1800,7 @@ cross-validation (LOOCV).</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-9-6e75736fdab1&gt;: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_94529/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label=&#39;Test Error&#39;)
</pre></div>
</div>
@@ -1962,9 +2064,9 @@ Note also that we do not split the data into training and test.</p>
Fitted beta: [2.08376632 0.19569961 3.97898392]
Sklearn fitted beta: [2.08376632 0.19569961 3.97898392]
MSE with intercept column
0.00411363461744314
0.004113634617443139
MSE with intercept column from SKL
0.004113634617443116
0.004113634617443147
Manual intercept: 2.083766322923899
Fitted beta (wiothout intercept): [0.19569961 3.97898392]
Sklearn intercept: 2.0837663229239043
@@ -2083,9 +2185,9 @@ intercept.</p>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Beta values for own Ridge implementation
[ 1.03032441e+00 6.28336218e-02 -6.24175744e-01 5.21169159e-02
2.80847477e-01 2.12552073e-01 8.13220608e-02 -1.69634577e-02
-6.50846111e-02 -7.38962192e-02 -5.94226022e-02 -3.50227564e-02
-9.80609616e-03 1.08299273e-02 2.41882036e-02 2.93492130e-02
2.64742912e-02 1.63249532e-02 -5.01831036e-05 -2.15098090e-02]
-6.50846112e-02 -7.38962192e-02 -5.94226022e-02 -3.50227564e-02
-9.80609616e-03 1.08299273e-02 2.41882037e-02 2.93492130e-02
2.64742912e-02 1.63249532e-02 -5.01831251e-05 -2.15098090e-02]
Beta values for Scikit-Learn Ridge implementation
[ 1.03032441e+00 6.28336218e-02 -6.24175744e-01 5.21169159e-02
2.80847477e-01 2.12552073e-01 8.13220608e-02 -1.69634577e-02
@@ -2093,7 +2195,7 @@ Beta values for Scikit-Learn Ridge implementation
-9.80609615e-03 1.08299273e-02 2.41882037e-02 2.93492130e-02
2.64742912e-02 1.63249532e-02 -5.01831207e-05 -2.15098090e-02]
MSE values for own Ridge implementation
4.363295924430451e-07
4.3632959215700067e-07
MSE values for Scikit-Learn Ridge implementation
4.363295916323784e-07
Beta values for own Ridge implementation
@@ -2107,7 +2209,7 @@ Beta values for Scikit-Learn Ridge implementation
-0.05009826 -0.04389027 -0.03279636 -0.01866537 -0.00289724 0.01348565
0.02976145 0.04543942]
MSE values for own Ridge implementation
5.19404282648955e-06
5.194042827197027e-06
MSE values for Scikit-Learn Ridge implementation
5.1940428268204826e-06
Beta values for own Ridge implementation
@@ -2121,7 +2223,7 @@ Beta values for Scikit-Learn Ridge implementation
0.00107405 -0.00526348 -0.00992331 -0.01318643 -0.01531845 -0.01655318
-0.01708852 -0.01708781]
MSE values for own Ridge implementation
2.094082198966615e-05
2.0940821989643363e-05
MSE values for Scikit-Learn Ridge implementation
2.094082198961999e-05
Beta values for own Ridge implementation
@@ -2135,7 +2237,7 @@ Beta values for Scikit-Learn Ridge implementation
0.01458337 0.01223198 0.00996754 0.00784393 0.00588657 0.00410387
0.00249435 0.00105081]
MSE values for own Ridge implementation
0.0003153514830958235
0.0003153514830957865
MSE values for Scikit-Learn Ridge implementation
0.00031535148309580783
Beta values for own Ridge implementation
@@ -2151,7 +2253,7 @@ Beta values for Scikit-Learn Ridge implementation
-1.84923989e-03 -8.13661243e-04 7.46984697e-06 6.56636616e-04
1.16805821e-03 1.56912044e-03 1.88168312e-03 2.12318726e-03]
MSE values for own Ridge implementation
0.015072388895177239
0.015072388895177157
MSE values for Scikit-Learn Ridge implementation
0.0150723888951771
Beta values for own Ridge implementation
@@ -2165,7 +2267,7 @@ Beta values for Scikit-Learn Ridge implementation
0.00719176 0.00630331 0.00556826 0.0049544 0.00443743 0.0039987
0.0036237 0.003301 ]
MSE values for own Ridge implementation
0.26409315307910053
0.26409315307910036
MSE values for Scikit-Learn Ridge implementation
0.26409315307910025
</pre></div>
@@ -2266,7 +2368,7 @@ Let us see how we can change this code by zero centering.</p>
2.18613217e-01 1.02054837e-01 -4.25617658e-04 -5.90475506e-02
-7.68534263e-02 -6.68929213e-02 -4.24906604e-02 -1.40927184e-02
1.11482289e-02 2.88529063e-02 3.67047975e-02 3.38135733e-02
2.02198702e-02 -3.46383925e-03 -3.63025821e-02]
2.02198703e-02 -3.46383925e-03 -3.63025821e-02]
Beta values for Scikit-Learn Ridge implementation
[ 3.43579948e-02 -5.43330971e-01 -3.10141413e-03 2.47116868e-01
2.18613217e-01 1.02054837e-01 -4.25617654e-04 -5.90475506e-02
@@ -2274,11 +2376,11 @@ Beta values for Scikit-Learn Ridge implementation
1.11482289e-02 2.88529063e-02 3.67047975e-02 3.38135733e-02
2.02198702e-02 -3.46383925e-03 -3.63025821e-02]
Intercept from own implementation:
1.0330308045190182
1.0330308045188872
Intercept from Scikit-Learn Ridge implementation
1.0330308045183219
MSE values for own Ridge implementation
3.13925595925919e-06
3.1392559591206444e-06
MSE values for Scikit-Learn Ridge implementation
3.1392559585048734e-06
Beta values for own Ridge implementation
@@ -2292,11 +2394,11 @@ Beta values for Scikit-Learn Ridge implementation
-0.04355837 -0.03562355 -0.02348765 -0.00848904 0.00831018 0.0260906
0.04423486]
Intercept from own implementation:
1.041148729430502
1.041148729430595
Intercept from Scikit-Learn Ridge implementation
1.041148729430523
MSE values for own Ridge implementation
1.9601304850018328e-05
1.96013048502692e-05
MSE values for Scikit-Learn Ridge implementation
1.960130485007504e-05
Beta values for own Ridge implementation
@@ -2310,11 +2412,11 @@ Beta values for Scikit-Learn Ridge implementation
-0.00440346 -0.00917248 -0.01231917 -0.01405935 -0.0146081 -0.01416528
-0.01290947]
Intercept from own implementation:
1.0495569966278238
1.0495569966278295
Intercept from Scikit-Learn Ridge implementation
1.0495569966278269
MSE values for own Ridge implementation
5.4959161509356135e-05
5.4959161509377256e-05
MSE values for Scikit-Learn Ridge implementation
5.495916150936645e-05
Beta values for own Ridge implementation
@@ -2328,11 +2430,11 @@ Beta values for Scikit-Learn Ridge implementation
0.00817631 0.00472512 0.00149311 -0.00149956 -0.00424967 -0.00676387
-0.00905423]
Intercept from own implementation:
1.0399676689527968
1.0399676689527966
Intercept from Scikit-Learn Ridge implementation
1.0399676689527975
MSE values for own Ridge implementation
7.571105947979336e-05
7.571105947979352e-05
MSE values for Scikit-Learn Ridge implementation
7.571105947979394e-05
Beta values for own Ridge implementation
@@ -2346,7 +2448,7 @@ Beta values for Scikit-Learn Ridge implementation
0.00579953 0.006162 0.00642221 0.00660427 0.00672607 0.0068011
0.00683964]
Intercept from own implementation:
0.9999555851685968
0.999955585168597
Intercept from Scikit-Learn Ridge implementation
0.999955585168597
MSE values for own Ridge implementation
@@ -2587,9 +2689,9 @@ linear system as an equation would reduce this down to
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-20-6f7a6bd7d79f&gt;:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/4162706317.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
cb = fig.colorbar(im)
&lt;ipython-input-20-6f7a6bd7d79f&gt;:7: UserWarning: FixedFormatter should only be used together with FixedLocator
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/4162706317.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
</pre></div>
</div>
@@ -2733,9 +2835,9 @@ with the form utilized in linear regression, viz.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-25-5dd54edf2138&gt;:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3777801602.py:6: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
cb = fig.colorbar(im)
&lt;ipython-input-25-5dd54edf2138&gt;:7: UserWarning: FixedFormatter should only be used together with FixedLocator
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3777801602.py:7: UserWarning: FixedFormatter should only be used together with FixedLocator
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
</pre></div>
</div>
@@ -2783,9 +2885,9 @@ K</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-26-fe5b9d300cc0&gt;:9: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/438060758.py:9: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
cb = fig.colorbar(im)
&lt;ipython-input-26-fe5b9d300cc0&gt;:10: UserWarning: FixedFormatter should only be used together with FixedLocator
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/438060758.py:10: UserWarning: FixedFormatter should only be used together with FixedLocator
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
</pre></div>
</div>
@@ -2820,9 +2922,9 @@ K</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-27-25845e8df859&gt;:8: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3544313922.py:8: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
cb = fig.colorbar(im)
&lt;ipython-input-27-25845e8df859&gt;:9: UserWarning: FixedFormatter should only be used together with FixedLocator
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3544313922.py:9: UserWarning: FixedFormatter should only be used together with FixedLocator
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
</pre></div>
</div>
@@ -2875,43 +2977,43 @@ constant as opposed to ridge and OLS. We get a sparse solution with
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0%| | 0/10 [00:00&lt;?, ?it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0%| | 0/10 [00:00&lt;?, ?it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_coordinate_descent.py:647: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations, check the scale of the features or consider increasing regularisation. Duality gap: 3.924e+00, tolerance: 1.797e+00
model = cd_fast.enet_coordinate_descent(
10%|█ | 1/10 [00:00&lt;00:06, 1.50it/s]
10%|███████████▊ | 1/10 [00:00&lt;00:06, 1.32it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 20%|██ | 2/10 [00:01&lt;00:03, 2.11it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 20%|███████████████████████▌ | 2/10 [00:01&lt;00:05, 1.39it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 30%|███ | 3/10 [00:01&lt;00:02, 3.06it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 30%|███████████████████████████████████▍ | 3/10 [00:01&lt;00:03, 1.83it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 40%|████ | 4/10 [00:01&lt;00:01, 4.00it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 40%|███████████████████████████████████████████████▏ | 4/10 [00:01&lt;00:02, 2.44it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 50%|█████ | 5/10 [00:01&lt;00:01, 4.87it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 50%|███████████████████████████████████████████████████████████ | 5/10 [00:02&lt;00:01, 3.02it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 60%|██████ | 6/10 [00:01&lt;00:00, 5.73it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 60%|██████████████████████████████████████████████████████████████████████▊ | 6/10 [00:02&lt;00:01, 3.70it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 70%|███████ | 7/10 [00:01&lt;00:00, 6.34it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 70%|██████████████████████████████████████████████████████████████████████████████████▌ | 7/10 [00:02&lt;00:00, 4.31it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 80%|████████ | 8/10 [00:01&lt;00:00, 6.22it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 80%|██████████████████████████████████████████████████████████████████████████████████████████████▍ | 8/10 [00:02&lt;00:00, 4.48it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 90%|█████████ | 9/10 [00:01&lt;00:00, 6.41it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 90%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 9/10 [00:02&lt;00:00, 4.92it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|██████████| 10/10 [00:02&lt;00:00, 6.14it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02&lt;00:00, 5.65it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|██████████| 10/10 [00:02&lt;00:00, 4.66it/s]
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:02&lt;00:00, 3.37it/s]
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>
@@ -3058,9 +3160,9 @@ which polynomial fits the data best.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;ipython-input-30-bc298b802fe2&gt;:9: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3980313467.py:9: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
ax = fig.gca(projection=&#39;3d&#39;)
&lt;ipython-input-30-bc298b802fe2&gt;:37: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_94529/3980313467.py:37: MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.
fig.colorbar(surf, shrink=0.5, aspect=5)
</pre></div>
</div>
@@ -3221,7 +3323,7 @@ Python program using</p>
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">NameError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o">&lt;</span><span class="n">ipython</span><span class="o">-</span><span class="nb">input</span><span class="o">-</span><span class="mi">31</span><span class="o">-</span><span class="n">d985fb40c43d</span><span class="o">&gt;</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="nn">Input In [31],</span> in <span class="ni">&lt;cell line: 1&gt;</span><span class="nt">()</span>
<span class="ne">----&gt; </span><span class="mi">1</span> <span class="n">scipy</span><span class="o">.</span><span class="n">misc</span><span class="o">.</span><span class="n">imread</span>
<span class="ne">NameError</span>: name &#39;scipy&#39; is not defined
@@ -3293,54 +3395,42 @@ of data presented here (either the terrain data we propose or other data sets).<
</div>
<div class='prev-next-bottom'>
<div id="prev">
<a class="left-prev" href="chapter2.html" title="previous page">
<i class="prevnext-label fas fa-angle-left"></i>
<div class="prevnext-info">
<p class="prevnext-label">previous</p>
<p class="prevnext-title"><span class="section-number">4. </span>Ridge and Lasso Regression</p>
</div>
</a>
<!-- Previous / next buttons -->
<div class='prev-next-area'>
<a class='left-prev' id="prev-link" href="chapter2.html" title="previous page">
<i class="fas fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title"><span class="section-number">4. </span>Ridge and Lasso Regression</p>
</div>
</a>
<a class='right-next' id="next-link" href="chapter4.html" title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title"><span class="section-number">6. </span>Logistic Regression</p>
</div>
<div id="next">
<a class="right-next" href="chapter4.html" title="next page">
<div class="prevnext-info">
<p class="prevnext-label">next</p>
<p class="prevnext-title"><span class="section-number">6. </span>Logistic Regression</p>
</div>
<i class="prevnext-label fas fa-angle-right"></i>
</a>
</div>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p>
By Morten Hjorth-Jensen<br/>
&copy; Copyright 2021.<br/>
</p>
</div>
</footer>
<p>
By Morten Hjorth-Jensen<br/>
&copy; Copyright 2021.<br/>
</p>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.1c5a1a01449ed65a7b51.js"></script>
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
</body>
</html>