Skip to content

Commit f88eefc

Browse files
committed
Revised first two weeks from feedback
1 parent 3425395 commit f88eefc

16 files changed

+89
-140
lines changed

doc/LectureNotes/E1.ipynb

Lines changed: 10 additions & 7 deletions
Large diffs are not rendered by default.

doc/LectureNotes/E2.ipynb

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@
224224
"source": [
225225
"With the expression for $\\boldsymbol{\\hat{\\beta}_{OLS}}$, you now have what you need to implement OLS regression with your input data and target data $\\boldsymbol{y}$. But before you can do that, you need to set up you input data as a feature matrix $\\boldsymbol{X}$.\n",
226226
"\n",
227-
"In a feature matrix, each row is a datapoint and each column is a feature of that data. If you want to predict someones spending based on their income and number of children, for instance, you would create a row for each person in your dataset, and in each column put a 1 for the intercept, the montly income and the number of children."
227+
"In a feature matrix, each row is a datapoint and each column is a feature of that data. If you want to predict someones spending based on their income and number of children, for instance, you would create a row for each person in your dataset, with the montly income and the number of children as columns.\n",
228+
"\n",
229+
"We typically also include an intercept in our models. The intercept is a value that is added to our prediction regardless of the value of the other features. The intercept tries to account for constant effects in our data that are not dependant on anything else. In our current example, the intercept could account for living expenses which are typical regardless of income or childcare expenses.\n",
230+
"\n",
231+
"We calculate the optimal intercept by including a feature with the constant value of 1 in our model, which is then multplied by some parameter $\\beta_0$ from the OLS method into the optimal intercept value (which will be $\\beta_0$). In practice, we include the intercept in our model by adding a column of ones to the start of our feature matrix."
228232
]
229233
},
230234
{
231235
"cell_type": "code",
232-
"execution_count": 59,
236+
"execution_count": null,
233237
"id": "e5ff2a69",
234238
"metadata": {},
235239
"outputs": [],
@@ -239,7 +243,7 @@
239243
},
240244
{
241245
"cell_type": "code",
242-
"execution_count": 60,
246+
"execution_count": null,
243247
"id": "a3cf2792",
244248
"metadata": {},
245249
"outputs": [],
@@ -260,7 +264,7 @@
260264
},
261265
{
262266
"cell_type": "code",
263-
"execution_count": 65,
267+
"execution_count": null,
264268
"id": "5ad87a65",
265269
"metadata": {},
266270
"outputs": [],
@@ -281,7 +285,7 @@
281285
},
282286
{
283287
"cell_type": "code",
284-
"execution_count": 37,
288+
"execution_count": null,
285289
"id": "8f3f68aa",
286290
"metadata": {},
287291
"outputs": [],
@@ -312,7 +316,7 @@
312316
},
313317
{
314318
"cell_type": "code",
315-
"execution_count": 38,
319+
"execution_count": null,
316320
"id": "d7476c84",
317321
"metadata": {},
318322
"outputs": [],
@@ -332,7 +336,7 @@
332336
},
333337
{
334338
"cell_type": "code",
335-
"execution_count": 66,
339+
"execution_count": null,
336340
"id": "91496e40",
337341
"metadata": {},
338342
"outputs": [],
@@ -358,7 +362,7 @@
358362
},
359363
{
360364
"cell_type": "code",
361-
"execution_count": 67,
365+
"execution_count": null,
362366
"id": "034f502c",
363367
"metadata": {},
364368
"outputs": [],
@@ -376,7 +380,7 @@
376380
},
377381
{
378382
"cell_type": "code",
379-
"execution_count": 68,
383+
"execution_count": null,
380384
"id": "29171358",
381385
"metadata": {},
382386
"outputs": [],
@@ -396,21 +400,10 @@
396400
},
397401
{
398402
"cell_type": "code",
399-
"execution_count": 42,
403+
"execution_count": null,
400404
"id": "1e346f4c",
401405
"metadata": {},
402-
"outputs": [
403-
{
404-
"data": {
405-
"text/plain": [
406-
"Ellipsis"
407-
]
408-
},
409-
"execution_count": 42,
410-
"metadata": {},
411-
"output_type": "execute_result"
412-
}
413-
],
406+
"outputs": [],
414407
"source": [
415408
"..."
416409
]
@@ -425,21 +418,10 @@
425418
},
426419
{
427420
"cell_type": "code",
428-
"execution_count": 43,
421+
"execution_count": null,
429422
"id": "ceb57457",
430423
"metadata": {},
431-
"outputs": [
432-
{
433-
"data": {
434-
"text/plain": [
435-
"Ellipsis"
436-
]
437-
},
438-
"execution_count": 43,
439-
"metadata": {},
440-
"output_type": "execute_result"
441-
}
442-
],
424+
"outputs": [],
443425
"source": [
444426
"..."
445427
]
320 Bytes
Binary file not shown.
973 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

doc/LectureNotes/_build/html/E1.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ <h2>Exercise 2 - Setting up a Github repository<a class="headerlink" href="#exer
446446
<h2>Exercise 3 - Setting up a Python virtual environment<a class="headerlink" href="#exercise-3-setting-up-a-python-virtual-environment" title="Link to this heading">#</a></h2>
447447
<p>Following the theme of the previous exercises, another way of improving the reproducibility of your results and shareability of your code is having a good handle on which python packages you are using.</p>
448448
<p>There are many ways to manage your packages in Python, and you are free to use any approach you want, but in this course we encourage you to use something called a virtual environment. A virtual environemnt is a folder in your project which contains a Python runtime executable as well as all the packages you are using in the current project. In this way, each of your projects has its required set of packages installed in the same folder, so that if anything goes wrong while managing your packages it only affects the one project, and if multiple projects require different versions of the same package, you don’t need to worry about messing up old projects. Also, it’s easy to just delete the folder and start over if anything goes wrong.</p>
449-
<p>Virtual environments are typically created, activated, managed and updated using terminal commands, but for now we recommend that you let VS Code handle it for you to make the coding experience much easier.</p>
449+
<p>Virtual environments are typically created, activated, managed and updated using terminal commands, but for now we recommend that you let VS Code handle it for you to make the coding experience much easier. If you are familiar with another approach for virtual environments that works for you, feel free to keep doing it that way.</p>
450450
<p><strong>a)</strong> Open this notebook in VS Code (<a class="reference external" href="https://code.visualstudio.com/Download">https://code.visualstudio.com/Download</a>). Download the Python and Jupyter extensions.</p>
451451
<p><strong>b)</strong> Press ´Cmd + Shift + P´, then search and run ´Python: Create Environment…´</p>
452452
<p><strong>c)</strong> Select ´Venv´</p>
@@ -483,10 +483,12 @@ <h2>Exercise 3 - Fitting an OLS model to data<a class="headerlink" href="#exerci
483483

484484
<span class="n">line_model</span> <span class="o">=</span> <span class="n">LinearRegression</span><span class="p">()</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
485485
<span class="n">line_predict</span> <span class="o">=</span> <span class="n">line_model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
486+
<span class="c1">#line_mse = ...</span>
486487

487488
<span class="c1">#poly_features = ...</span>
488489
<span class="c1">#poly_model = LinearRegression().fit(..., y)</span>
489490
<span class="c1">#poly_predict = ...</span>
491+
<span class="c1">#poly_mse = ...</span>
490492

491493
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="s2">&quot;Data&quot;</span><span class="p">)</span>
492494
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">line_predict</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="s2">&quot;Line model&quot;</span><span class="p">)</span>
@@ -496,7 +498,7 @@ <h2>Exercise 3 - Fitting an OLS model to data<a class="headerlink" href="#exerci
496498
</div>
497499
</div>
498500
<div class="cell_output docutils container">
499-
<img alt="_images/fbd74da3fcd21613c4c425622594644bbfe45b6a7fd50ac62da4e3136a737128.png" src="_images/fbd74da3fcd21613c4c425622594644bbfe45b6a7fd50ac62da4e3136a737128.png" />
501+
<img alt="_images/c62786c19b580c6638248aa3cfe7dea30bdbc00a922e7e45a45a5a7a053bdb38.png" src="_images/c62786c19b580c6638248aa3cfe7dea30bdbc00a922e7e45a45a5a7a053bdb38.png" />
500502
</div>
501503
</div>
502504
</section>

doc/LectureNotes/_build/html/E2.html

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ <h2>Exercise 2 - Deriving the expression for OLS<a class="headerlink" href="#exe
497497
<section id="exercise-3-creating-feature-matrix-and-implementing-ols-using-the-analytical-expression">
498498
<h2>Exercise 3 - Creating feature matrix and implementing OLS using the analytical expression<a class="headerlink" href="#exercise-3-creating-feature-matrix-and-implementing-ols-using-the-analytical-expression" title="Link to this heading">#</a></h2>
499499
<p>With the expression for <span class="math notranslate nohighlight">\(\boldsymbol{\hat{\beta}_{OLS}}\)</span>, you now have what you need to implement OLS regression with your input data and target data <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span>. But before you can do that, you need to set up you input data as a feature matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>.</p>
500-
<p>In a feature matrix, each row is a datapoint and each column is a feature of that data. If you want to predict someones spending based on their income and number of children, for instance, you would create a row for each person in your dataset, and in each column put a 1 for the intercept, the montly income and the number of children.</p>
500+
<p>In a feature matrix, each row is a datapoint and each column is a feature of that data. If you want to predict someones spending based on their income and number of children, for instance, you would create a row for each person in your dataset, with the montly income and the number of children as columns.</p>
501+
<p>We typically also include an intercept in our models. The intercept is a value that is added to our prediction regardless of the value of the other features. The intercept tries to account for constant effects in our data that are not dependant on anything else. In our current example, the intercept could account for living expenses which are typical regardless of income or childcare expenses.</p>
502+
<p>We calculate the optimal intercept by including a feature with the constant value of 1 in our model, which is then multplied by some parameter <span class="math notranslate nohighlight">\(\beta_0\)</span> from the OLS method into the optimal intercept value (which will be <span class="math notranslate nohighlight">\(\beta_0\)</span>). In practice, we include the intercept in our model by adding a column of ones to the start of our feature matrix.</p>
501503
<div class="cell docutils container">
502504
<div class="cell_input docutils container">
503505
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
@@ -592,11 +594,6 @@ <h2>Exercise 4 - Fitting a polynomial<a class="headerlink" href="#exercise-4-fit
592594
</pre></div>
593595
</div>
594596
</div>
595-
<div class="cell_output docutils container">
596-
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ellipsis
597-
</pre></div>
598-
</div>
599-
</div>
600597
</div>
601598
<p><strong>e)</strong> Do the same for each polynomial degree from 2 to 10, and plot the MSE on both the training and test data as a function of polynomial degree. The aim is to reproduce Figure 2.11 of <a class="reference external" href="https://github.com/CompPhysics/MLErasmus/blob/master/doc/Textbooks/elementsstat.pdf">Hastie et al</a>. Feel free to read the discussions leading to figure 2.11 of Hastie et al.</p>
602599
<div class="cell docutils container">
@@ -605,11 +602,6 @@ <h2>Exercise 4 - Fitting a polynomial<a class="headerlink" href="#exercise-4-fit
605602
</pre></div>
606603
</div>
607604
</div>
608-
<div class="cell_output docutils container">
609-
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ellipsis
610-
</pre></div>
611-
</div>
612-
</div>
613605
</div>
614606
<p><strong>f)</strong> Interpret the graph. Why do the lines move as they do? What does it tell us about model performance and generalizability?</p>
615607
</section>
18.5 KB
Loading

doc/LectureNotes/_build/html/_sources/E1.ipynb

Lines changed: 10 additions & 7 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)