small update

This commit is contained in:
Morten Hjorth-Jensen
2021-11-22 23:50:15 +01:00
parent b7ef6f0a03
commit de4efe13ca
9 changed files with 970 additions and 644 deletions
@@ -1,6 +1,7 @@
<!--
Automatically generated HTML file from DocOnce source
HTML file automatically generated from DocOnce source
(https://github.com/doconce/doconce/)
doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs
-->
<html>
<head>
@@ -8,24 +9,20 @@ Automatically generated HTML file from DocOnce source
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Project 1 on Machine Learning, deadline October 4, 2021">
<title>Project 1 on Machine Learning, deadline October 4, 2021</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
@@ -35,8 +32,6 @@ Automatically generated HTML file from DocOnce source
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
@@ -106,8 +101,6 @@ MathJax.Hub.Config({
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
@@ -118,7 +111,6 @@ MathJax.Hub.Config({
</button>
<a class="navbar-brand" href="Project1-bs.html">Project 1 on Machine Learning, deadline October 4, 2021</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
@@ -142,46 +134,39 @@ MathJax.Hub.Config({
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0000"></a>
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center><h1>Project 1 on Machine Learning, deadline October 4, 2021</h1></center> <!-- document title -->
<center>
<h1>Project 1 on Machine Learning, deadline October 4, 2021</h1>
</center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center>
<b>University of Oslo, Norway</b>
</center>
<br>
<center>
<h4>Nov 22, 2021</h4>
</center> <!-- date -->
<br>
<center><b>University of Oslo, Norway</b></center>
<br>
<p>
<center><h4>Oct 4, 2021</h4></center> <!-- date -->
<br>
<p>
</div> <!-- end jumbotron -->
<h2 id="regression-analysis-and-resampling-methods" class="anchor">Regression analysis and resampling methods </h2>
<p>
The main aim of this project is to study in more detail various
<p>The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method,
The total score is <b>100</b> points. Each subtask has its own final score.
</p>
<p>
We will first study how to fit polynomials to a specific
<p>We will first study how to fit polynomials to a specific
two-dimensional function called <a href="http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf" target="_self">Franke's
function</a>. This
is a function which has been widely used when testing various
@@ -190,9 +175,9 @@ established the model and the method, we will employ resamling
techniques such as cross-validation and/or bootstrap in order to perform a
proper assessment of our models. We will also study in detail the
so-called Bias-Variance trade off.
</p>
<p>
The Franke function, which is a weighted sum of four exponentials reads as follows
<p>The Franke function, which is a weighted sum of four exponentials reads as follows</p>
$$
\begin{align*}
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
@@ -200,8 +185,7 @@ f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}
\end{align*}
$$
<p>
The function will be defined for \( x,y\in [0,1] \). Our first step will
<p>The function will be defined for \( x,y\in [0,1] \). Our first step will
be to perform an OLS regression analysis of this function, trying out
a polynomial fit with an \( x \) and \( y \) dependence of the form \( [x, y,
x^2, y^2, xy, \dots] \). We will also include bootstrap first as
@@ -214,19 +198,23 @@ function (for example a polynomial) of \( x \) and \( y \). Thereafter we
will repeat much of the same procedure using the Ridge and Lasso
regression methods, introducing thus a dependence on the bias
(penalty) \( \lambda \).
</p>
<p>
Finally we are going to use (real) digital terrain data and try to
<p>Finally we are going to use (real) digital terrain data and try to
reproduce these data using the same methods. We will also try to go
beyond the second-order polynomials metioned above and explore
which polynomial fits the data best.
</p>
<p>
The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
<p>
<p>The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> cm
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.ticker</span> <span style="color: #008000; font-weight: bold">import</span> LinearLocator, FormatStrFormatter
@@ -265,220 +253,245 @@ ax<span style="color: #666666">.</span>zaxis<span style="color: #666666">.</span
fig<span style="color: #666666">.</span>colorbar(surf, shrink<span style="color: #666666">=0.5</span>, aspect<span style="color: #666666">=5</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<h3 id="exercise-1-ordinary-least-square-ols-on-the-franke-function-score-10-points" class="anchor">Exercise 1: Ordinary Least Square (OLS) on the Franke function (score 10 points) </h3>
<p>
We will generate our own dataset for a function
<p>We will generate our own dataset for a function
\( \mathrm{FrankeFunction}(x,y) \) with \( x,y \in [0,1] \). The function
\( f(x,y) \) is the Franke function. You should explore also the addition
of an added stochastic noise to this function using the normal
distribution \( N(0,1) \).
</p>
<p>
<em>Write your own code</em> (using either a matrix inversion or a singular
<p><em>Write your own code</em> (using either a matrix inversion or a singular
value decomposition from e.g., <b>numpy</b> ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in \( x \) and \( y \) up to fifth order. Find the
<a href="https://en.wikipedia.org/wiki/Confidence_interval" target="_self">confidence intervals</a> of the parameters (estimators) \( \beta \) by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in \( x \) and \( y \) up to fifth order.
</p>
<p>Evaluate the Mean Squared error (MSE)</p>
$$ MSE(\boldsymbol{y},\tilde{\boldsymbol{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
$$
<p>
and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
<p>and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
value of the \( i-th \) sample and \( y_i \) is the corresponding true value,
then the score \( R^2 \) is defined as
</p>
$$
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
$$
<p>
where we have defined the mean value of \( \boldsymbol{y} \) as
<p>where we have defined the mean value of \( \boldsymbol{y} \) as</p>
$$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
<p>
Your code has to include a scaling of the data (for example by
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters \( \beta \) as you increase the order of the polynomial. Comment your results.
</p>
<p>Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for
splitting training data provided by the library <b>Scikit-Learn</b> (make
sure you have installed it). This function is called
\( train\_test\_split \). <b>You should present a critical discussion of why and how you have scaled or not scaled the data</b>.
</p>
<p>
It is normal in essentially all Machine Learning studies to split the
<p>It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (eventually also an additional
validation set). There
is no explicit recipe for how much data should be included as training
data and say test data. An accepted rule of thumb is to use
approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
</p>
<p>
You can easily reuse the solutions to your exercises from week 35 and week 36.
<p>You can easily reuse the solutions to your exercises from week 35 and week 36.</p>
<h3 id="exercise-2-bias-variance-trade-off-and-resampling-techniques-score-15-points" class="anchor">Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points) </h3>
<p>
Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.</p>
<p>
With a code which does OLS and includes resampling techniques,
<p>With a code which does OLS and includes resampling techniques,
we will now discuss the bias-variance trade-off in the context of
continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
tasks and basically all Machine Learning algorithms.
</p>
<p>
Before you perform an analysis of the bias-variance trade-off on your test data, make
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
</p>
<p>
With this result we move on to the bias-variance trade-off analysis.
<p>With this result we move on to the bias-variance trade-off analysis.</p>
<p>
Consider a
<p>Consider a
dataset \( \mathcal{L} \) consisting of the data
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
</p>
<p>
Let us assume that the true data is generated from a noisy model
<p>Let us assume that the true data is generated from a noisy model</p>
$$
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
$$
<p>
Here \( \epsilon \) is normally distributed with mean zero and standard
<p>Here \( \epsilon \) is normally distributed with mean zero and standard
deviation \( \sigma^2 \).
</p>
<p>
In our derivation of the ordinary least squares method we defined then
<p>In our derivation of the ordinary least squares method we defined then
an approximation to the function \( f \) in terms of the parameters
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
</p>
<p>
The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
squared error via the so-called cost function
</p>
$$
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
$$
Here the expected value \( \mathbb{E} \) is the sample value.
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
<p>
Show that you can rewrite this as
<p>Show that you can rewrite this as</p>
$$
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
$$
<p>
Explain what the terms mean, which one is the bias and which one is
<p>Explain what the terms mean, which one is the bias and which one is
the variance and discuss their interpretations.
</p>
<p>
Perform then a bias-variance analysis of the Franke function by
<p>Perform then a bias-variance analysis of the Franke function by
studying the MSE value as function of the complexity of your model.
</p>
<p>
Discuss the bias and variance trade-off as function
<p>Discuss the bias and variance trade-off as function
of your model complexity (the degree of the polynomial) and the number
of data points, and possibly also your training and test data using the <b>bootstrap</b> resampling method.
</p>
<p>
Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).
<p>Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).</p>
<h3 id="exercise-3-cross-validation-as-resampling-techniques-adding-more-complexity-score-15-points" class="anchor">Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points) </h3>
<p>
The aim here is to write your own code for another widely popular
<p>The aim here is to write your own code for another widely popular
resampling technique, the so-called cross-validation method. Again,
before you start with cross-validation approach, you should scale your
data if you think this is needed.
</p>
<p>
Implement the \( k \)-fold cross-validation algorithm (write your own
<p>Implement the \( k \)-fold cross-validation algorithm (write your own
code) and evaluate again the MSE function resulting
from the test folds. You can compare your own code with that from
<b>Scikit-Learn</b> if needed.
<b>Scikit-Learn</b> if needed.
</p>
<p>
Compare the MSE you get from your cross-validation code with the one
<p>Compare the MSE you get from your cross-validation code with the one
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
folds. You can also compare your own cross-validation code with the
one provided by <b>Scikit-Learn</b>.
</p>
<h3 id="exercise-4-ridge-regression-on-the-franke-function-with-resampling-score-20-points" class="anchor">Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points) </h3>
<p>
Write your own code for the Ridge method, either using matrix
<p>Write your own code for the Ridge method, either using matrix
inversion or the singular value decomposition as done in the previous
exercise. Perform the same bootstrap analysis as in the
Exercise 2 (for the same polynomials) and the cross-validation in exercise 3 but now for different values of \( \lambda \). Compare and
analyze your results with those obtained in exercises 1-3. Study the
dependence on \( \lambda \).
</p>
<p>
Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
<p>Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
</p>
<h3 id="exercise-5-lasso-regression-on-the-franke-function-with-resampling-score-10-points" class="anchor">Exercise 5: Lasso Regression on the Franke function with resampling (Score 10 points)) </h3>
<p>
This exercise is essentially a repeat of the previous two ones, but now
<p>This exercise is essentially a repeat of the previous two ones, but now
with Lasso regression. Write either your own code (difficult and optional) or, in this case,
you can also use the functionalities of <b>Scikit-Learn</b> (recommended).
Give a
critical discussion of the three methods and a judgement of which
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
</p>
<h3 id="exercise-6-analysis-of-real-data-score-30-points" class="anchor">Exercise 6: Analysis of real data (score 30 points) </h3>
<p>
With our codes functioning and having been tested properly on a
<p>With our codes functioning and having been tested properly on a
simpler function we are now ready to look at real data. We will
essentially repeat in this exercise what was done in exercises 1-5. However, we
need first to download the data and prepare properly the inputs to our
codes. We are going to download digital terrain data from the website
<a href="https://earthexplorer.usgs.gov/" target="_self"><tt>https://earthexplorer.usgs.gov/</tt></a>,
</p>
<p>
Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a>
<p>Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a></p>
<p>
In order to obtain data for a specific region, you need to register as
<p>In order to obtain data for a specific region, you need to register as
a user (free) at this website and then decide upon which area you want
to fetch the digital terrain data from. In order to be able to read
the data properly, you need to specify that the format should be <b>SRTM
Arc-Second Global</b> and download the data as a <b>GeoTIF</b> file. The
files are then stored in <em>tif</em> format which can be imported into a
Python program using
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre></div>
<p>
Here is a simple part of a Python code which reads and plots the data
<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%;">scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>Here is a simple part of a Python code which reads and plots the data
from such files
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></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>
<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: #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">from</span> <span style="color: #0000FF; font-weight: bold">imageio</span> <span style="color: #008000; font-weight: bold">import</span> imread
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
@@ -493,39 +506,49 @@ plt<span style="color: #666666">.</span>imshow(terrain1, cmap<span style="color:
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;X&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Y&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
If you should have problems in downloading the digital terrain data,
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>If you should have problems in downloading the digital terrain data,
we provide two examples under the data folder of project 1. One is
from a region close to Stavanger in Norway and the other M&#248;svatn
Austfjell, again in Norway.
Feel free to produce your own terrain data.
</p>
<p>
Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_self">kaggle.com</a> for examples.
<p>Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_self">kaggle.com</a> for examples.</p>
<p>
Our final part deals with the parameterization of your digital terrain
<p>Our final part deals with the parameterization of your digital terrain
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
approximation and cross-validation as resampling technique to evaluate which
model fits the data best.
</p>
<p>
At the end, you should present a critical evaluation of your results
<p>At the end, you should present a critical evaluation of your results
and discuss the applicability of these regression methods to the type
of data presented here (either the terrain data we propose or other data sets).
</p>
<h2 id="background-literature" class="anchor">Background literature </h2>
<ol>
<li> For a discussion and derivation of the variances and mean squared errors using linear regression, see the <a href="https://arxiv.org/abs/1509.09169" target="_self">Lecture notes on ridge regression by Wessel N. van Wieringen</a></li>
<li> The textbook of <a href="https://www.springer.com/gp/book/9780387848570" target="_self">Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer</a>, chapters 3 and 7 are the most relevant ones for the analysis here.</li>
</ol>
<h2 id="introduction-to-numerical-projects" class="anchor">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.
<p>Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.</p>
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
@@ -538,63 +561,51 @@ Here follows a brief recipe and recommendation on how to answer the various ques
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="format-for-electronic-delivery-of-report-and-programs" class="anchor">Format for electronic delivery of report and programs </h2>
<p>
The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:
<p>The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:</p>
<ul>
<li> Use Canvas to hand in your projects, log in at <a href="https://www.uio.no/english/services/it/education/canvas/" target="_self"><tt>https://www.uio.no/english/services/it/education/canvas/</tt></a> with your normal UiO username and password.</li>
<li> Upload <b>only</b> the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.</li>
<li> In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.</li>
</ul>
Finally,
<p>Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.
2-3 students. You can then hand in a common report.
</p>
<h2 id="software-and-needed-installations" class="anchor">Software and needed installations </h2>
<p>
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
<p>If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via <b>pip</b> as
</p>
<ol>
<li> pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow</li>
</ol>
<p>For Python3, replace <b>pip</b> with <b>pip3</b>.</p>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>. </p>
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>.
<p>
For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
<p>For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
for a seamless installation of additional software via for example
</p>
<ol>
<li> brew install python3</li>
</ol>
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
<p>For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use <b>pip</b> as well and simply install Python as
</p>
<ol>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
<p>etc etc. </p>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<p>If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely</p>
<ol>
<li> <a href="https://docs.anaconda.com/" target="_self">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
<li> <a href="https://www.enthought.com/product/canopy/" target="_self">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
</ol>
Popular software packages written in Python for ML are
<p>Popular software packages written in Python for ML are</p>
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_self">Scikit-learn</a>,</li>
@@ -602,12 +613,10 @@ Popular software packages written in Python for ML are
<li> <a href="http://pytorch.org/" target="_self">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_self">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
<p>These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<p>
</p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -615,20 +624,15 @@ of code developers and contributors keeps increasing.
<li class="active"><a href="._Project1-bs000.html">1</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="https://..."><img width="250" align=right src="https://..."></a>
</footer>
-->
</body>
</html>
+178 -174
View File
@@ -1,6 +1,7 @@
<!--
Automatically generated HTML file from DocOnce source
HTML file automatically generated from DocOnce source
(https://github.com/doconce/doconce/)
doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs
-->
<html>
<head>
@@ -8,24 +9,20 @@ Automatically generated HTML file from DocOnce source
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Project 1 on Machine Learning, deadline October 4, 2021">
<title>Project 1 on Machine Learning, deadline October 4, 2021</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
@@ -35,8 +32,6 @@ Automatically generated HTML file from DocOnce source
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
@@ -106,8 +101,6 @@ MathJax.Hub.Config({
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
@@ -118,7 +111,6 @@ MathJax.Hub.Config({
</button>
<a class="navbar-brand" href="Project1-bs.html">Project 1 on Machine Learning, deadline October 4, 2021</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
@@ -142,46 +134,39 @@ MathJax.Hub.Config({
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0000"></a>
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center><h1>Project 1 on Machine Learning, deadline October 4, 2021</h1></center> <!-- document title -->
<center>
<h1>Project 1 on Machine Learning, deadline October 4, 2021</h1>
</center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center>
<b>University of Oslo, Norway</b>
</center>
<br>
<center>
<h4>Nov 22, 2021</h4>
</center> <!-- date -->
<br>
<center><b>University of Oslo, Norway</b></center>
<br>
<p>
<center><h4>Oct 4, 2021</h4></center> <!-- date -->
<br>
<p>
</div> <!-- end jumbotron -->
<h2 id="regression-analysis-and-resampling-methods" class="anchor">Regression analysis and resampling methods </h2>
<p>
The main aim of this project is to study in more detail various
<p>The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method,
The total score is <b>100</b> points. Each subtask has its own final score.
</p>
<p>
We will first study how to fit polynomials to a specific
<p>We will first study how to fit polynomials to a specific
two-dimensional function called <a href="http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf" target="_self">Franke's
function</a>. This
is a function which has been widely used when testing various
@@ -190,9 +175,9 @@ established the model and the method, we will employ resamling
techniques such as cross-validation and/or bootstrap in order to perform a
proper assessment of our models. We will also study in detail the
so-called Bias-Variance trade off.
</p>
<p>
The Franke function, which is a weighted sum of four exponentials reads as follows
<p>The Franke function, which is a weighted sum of four exponentials reads as follows</p>
$$
\begin{align*}
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
@@ -200,8 +185,7 @@ f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}
\end{align*}
$$
<p>
The function will be defined for \( x,y\in [0,1] \). Our first step will
<p>The function will be defined for \( x,y\in [0,1] \). Our first step will
be to perform an OLS regression analysis of this function, trying out
a polynomial fit with an \( x \) and \( y \) dependence of the form \( [x, y,
x^2, y^2, xy, \dots] \). We will also include bootstrap first as
@@ -214,19 +198,23 @@ function (for example a polynomial) of \( x \) and \( y \). Thereafter we
will repeat much of the same procedure using the Ridge and Lasso
regression methods, introducing thus a dependence on the bias
(penalty) \( \lambda \).
</p>
<p>
Finally we are going to use (real) digital terrain data and try to
<p>Finally we are going to use (real) digital terrain data and try to
reproduce these data using the same methods. We will also try to go
beyond the second-order polynomials metioned above and explore
which polynomial fits the data best.
</p>
<p>
The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
<p>
<p>The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> cm
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.ticker</span> <span style="color: #008000; font-weight: bold">import</span> LinearLocator, FormatStrFormatter
@@ -265,220 +253,245 @@ ax<span style="color: #666666">.</span>zaxis<span style="color: #666666">.</span
fig<span style="color: #666666">.</span>colorbar(surf, shrink<span style="color: #666666">=0.5</span>, aspect<span style="color: #666666">=5</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<h3 id="exercise-1-ordinary-least-square-ols-on-the-franke-function-score-10-points" class="anchor">Exercise 1: Ordinary Least Square (OLS) on the Franke function (score 10 points) </h3>
<p>
We will generate our own dataset for a function
<p>We will generate our own dataset for a function
\( \mathrm{FrankeFunction}(x,y) \) with \( x,y \in [0,1] \). The function
\( f(x,y) \) is the Franke function. You should explore also the addition
of an added stochastic noise to this function using the normal
distribution \( N(0,1) \).
</p>
<p>
<em>Write your own code</em> (using either a matrix inversion or a singular
<p><em>Write your own code</em> (using either a matrix inversion or a singular
value decomposition from e.g., <b>numpy</b> ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in \( x \) and \( y \) up to fifth order. Find the
<a href="https://en.wikipedia.org/wiki/Confidence_interval" target="_self">confidence intervals</a> of the parameters (estimators) \( \beta \) by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in \( x \) and \( y \) up to fifth order.
</p>
<p>Evaluate the Mean Squared error (MSE)</p>
$$ MSE(\boldsymbol{y},\tilde{\boldsymbol{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
$$
<p>
and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
<p>and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
value of the \( i-th \) sample and \( y_i \) is the corresponding true value,
then the score \( R^2 \) is defined as
</p>
$$
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
$$
<p>
where we have defined the mean value of \( \boldsymbol{y} \) as
<p>where we have defined the mean value of \( \boldsymbol{y} \) as</p>
$$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
<p>
Your code has to include a scaling of the data (for example by
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters \( \beta \) as you increase the order of the polynomial. Comment your results.
</p>
<p>Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for
splitting training data provided by the library <b>Scikit-Learn</b> (make
sure you have installed it). This function is called
\( train\_test\_split \). <b>You should present a critical discussion of why and how you have scaled or not scaled the data</b>.
</p>
<p>
It is normal in essentially all Machine Learning studies to split the
<p>It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (eventually also an additional
validation set). There
is no explicit recipe for how much data should be included as training
data and say test data. An accepted rule of thumb is to use
approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
</p>
<p>
You can easily reuse the solutions to your exercises from week 35 and week 36.
<p>You can easily reuse the solutions to your exercises from week 35 and week 36.</p>
<h3 id="exercise-2-bias-variance-trade-off-and-resampling-techniques-score-15-points" class="anchor">Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points) </h3>
<p>
Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.</p>
<p>
With a code which does OLS and includes resampling techniques,
<p>With a code which does OLS and includes resampling techniques,
we will now discuss the bias-variance trade-off in the context of
continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
tasks and basically all Machine Learning algorithms.
</p>
<p>
Before you perform an analysis of the bias-variance trade-off on your test data, make
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
</p>
<p>
With this result we move on to the bias-variance trade-off analysis.
<p>With this result we move on to the bias-variance trade-off analysis.</p>
<p>
Consider a
<p>Consider a
dataset \( \mathcal{L} \) consisting of the data
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
</p>
<p>
Let us assume that the true data is generated from a noisy model
<p>Let us assume that the true data is generated from a noisy model</p>
$$
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
$$
<p>
Here \( \epsilon \) is normally distributed with mean zero and standard
<p>Here \( \epsilon \) is normally distributed with mean zero and standard
deviation \( \sigma^2 \).
</p>
<p>
In our derivation of the ordinary least squares method we defined then
<p>In our derivation of the ordinary least squares method we defined then
an approximation to the function \( f \) in terms of the parameters
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
</p>
<p>
The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
squared error via the so-called cost function
</p>
$$
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
$$
Here the expected value \( \mathbb{E} \) is the sample value.
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
<p>
Show that you can rewrite this as
<p>Show that you can rewrite this as</p>
$$
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
$$
<p>
Explain what the terms mean, which one is the bias and which one is
<p>Explain what the terms mean, which one is the bias and which one is
the variance and discuss their interpretations.
</p>
<p>
Perform then a bias-variance analysis of the Franke function by
<p>Perform then a bias-variance analysis of the Franke function by
studying the MSE value as function of the complexity of your model.
</p>
<p>
Discuss the bias and variance trade-off as function
<p>Discuss the bias and variance trade-off as function
of your model complexity (the degree of the polynomial) and the number
of data points, and possibly also your training and test data using the <b>bootstrap</b> resampling method.
</p>
<p>
Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).
<p>Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).</p>
<h3 id="exercise-3-cross-validation-as-resampling-techniques-adding-more-complexity-score-15-points" class="anchor">Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points) </h3>
<p>
The aim here is to write your own code for another widely popular
<p>The aim here is to write your own code for another widely popular
resampling technique, the so-called cross-validation method. Again,
before you start with cross-validation approach, you should scale your
data if you think this is needed.
</p>
<p>
Implement the \( k \)-fold cross-validation algorithm (write your own
<p>Implement the \( k \)-fold cross-validation algorithm (write your own
code) and evaluate again the MSE function resulting
from the test folds. You can compare your own code with that from
<b>Scikit-Learn</b> if needed.
<b>Scikit-Learn</b> if needed.
</p>
<p>
Compare the MSE you get from your cross-validation code with the one
<p>Compare the MSE you get from your cross-validation code with the one
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
folds. You can also compare your own cross-validation code with the
one provided by <b>Scikit-Learn</b>.
</p>
<h3 id="exercise-4-ridge-regression-on-the-franke-function-with-resampling-score-20-points" class="anchor">Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points) </h3>
<p>
Write your own code for the Ridge method, either using matrix
<p>Write your own code for the Ridge method, either using matrix
inversion or the singular value decomposition as done in the previous
exercise. Perform the same bootstrap analysis as in the
Exercise 2 (for the same polynomials) and the cross-validation in exercise 3 but now for different values of \( \lambda \). Compare and
analyze your results with those obtained in exercises 1-3. Study the
dependence on \( \lambda \).
</p>
<p>
Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
<p>Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
</p>
<h3 id="exercise-5-lasso-regression-on-the-franke-function-with-resampling-score-10-points" class="anchor">Exercise 5: Lasso Regression on the Franke function with resampling (Score 10 points)) </h3>
<p>
This exercise is essentially a repeat of the previous two ones, but now
<p>This exercise is essentially a repeat of the previous two ones, but now
with Lasso regression. Write either your own code (difficult and optional) or, in this case,
you can also use the functionalities of <b>Scikit-Learn</b> (recommended).
Give a
critical discussion of the three methods and a judgement of which
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
</p>
<h3 id="exercise-6-analysis-of-real-data-score-30-points" class="anchor">Exercise 6: Analysis of real data (score 30 points) </h3>
<p>
With our codes functioning and having been tested properly on a
<p>With our codes functioning and having been tested properly on a
simpler function we are now ready to look at real data. We will
essentially repeat in this exercise what was done in exercises 1-5. However, we
need first to download the data and prepare properly the inputs to our
codes. We are going to download digital terrain data from the website
<a href="https://earthexplorer.usgs.gov/" target="_self"><tt>https://earthexplorer.usgs.gov/</tt></a>,
</p>
<p>
Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a>
<p>Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a></p>
<p>
In order to obtain data for a specific region, you need to register as
<p>In order to obtain data for a specific region, you need to register as
a user (free) at this website and then decide upon which area you want
to fetch the digital terrain data from. In order to be able to read
the data properly, you need to specify that the format should be <b>SRTM
Arc-Second Global</b> and download the data as a <b>GeoTIF</b> file. The
files are then stored in <em>tif</em> format which can be imported into a
Python program using
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre></div>
<p>
Here is a simple part of a Python code which reads and plots the data
<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%;">scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>Here is a simple part of a Python code which reads and plots the data
from such files
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></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>
<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: #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">from</span> <span style="color: #0000FF; font-weight: bold">imageio</span> <span style="color: #008000; font-weight: bold">import</span> imread
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
@@ -493,39 +506,49 @@ plt<span style="color: #666666">.</span>imshow(terrain1, cmap<span style="color:
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;X&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Y&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
If you should have problems in downloading the digital terrain data,
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>If you should have problems in downloading the digital terrain data,
we provide two examples under the data folder of project 1. One is
from a region close to Stavanger in Norway and the other M&#248;svatn
Austfjell, again in Norway.
Feel free to produce your own terrain data.
</p>
<p>
Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_self">kaggle.com</a> for examples.
<p>Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_self">kaggle.com</a> for examples.</p>
<p>
Our final part deals with the parameterization of your digital terrain
<p>Our final part deals with the parameterization of your digital terrain
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
approximation and cross-validation as resampling technique to evaluate which
model fits the data best.
</p>
<p>
At the end, you should present a critical evaluation of your results
<p>At the end, you should present a critical evaluation of your results
and discuss the applicability of these regression methods to the type
of data presented here (either the terrain data we propose or other data sets).
</p>
<h2 id="background-literature" class="anchor">Background literature </h2>
<ol>
<li> For a discussion and derivation of the variances and mean squared errors using linear regression, see the <a href="https://arxiv.org/abs/1509.09169" target="_self">Lecture notes on ridge regression by Wessel N. van Wieringen</a></li>
<li> The textbook of <a href="https://www.springer.com/gp/book/9780387848570" target="_self">Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer</a>, chapters 3 and 7 are the most relevant ones for the analysis here.</li>
</ol>
<h2 id="introduction-to-numerical-projects" class="anchor">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.
<p>Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.</p>
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
@@ -538,63 +561,51 @@ Here follows a brief recipe and recommendation on how to answer the various ques
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="format-for-electronic-delivery-of-report-and-programs" class="anchor">Format for electronic delivery of report and programs </h2>
<p>
The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:
<p>The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:</p>
<ul>
<li> Use Canvas to hand in your projects, log in at <a href="https://www.uio.no/english/services/it/education/canvas/" target="_self"><tt>https://www.uio.no/english/services/it/education/canvas/</tt></a> with your normal UiO username and password.</li>
<li> Upload <b>only</b> the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.</li>
<li> In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.</li>
</ul>
Finally,
<p>Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.
2-3 students. You can then hand in a common report.
</p>
<h2 id="software-and-needed-installations" class="anchor">Software and needed installations </h2>
<p>
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
<p>If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via <b>pip</b> as
</p>
<ol>
<li> pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow</li>
</ol>
<p>For Python3, replace <b>pip</b> with <b>pip3</b>.</p>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>. </p>
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>.
<p>
For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
<p>For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
for a seamless installation of additional software via for example
</p>
<ol>
<li> brew install python3</li>
</ol>
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
<p>For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use <b>pip</b> as well and simply install Python as
</p>
<ol>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
<p>etc etc. </p>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<p>If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely</p>
<ol>
<li> <a href="https://docs.anaconda.com/" target="_self">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
<li> <a href="https://www.enthought.com/product/canopy/" target="_self">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
</ol>
Popular software packages written in Python for ML are
<p>Popular software packages written in Python for ML are</p>
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_self">Scikit-learn</a>,</li>
@@ -602,12 +613,10 @@ Popular software packages written in Python for ML are
<li> <a href="http://pytorch.org/" target="_self">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_self">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
<p>These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<p>
</p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -615,20 +624,15 @@ of code developers and contributors keeps increasing.
<li class="active"><a href="._Project1-bs000.html">1</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="https://..."><img width="250" align=right src="https://..."></a>
</footer>
-->
</body>
</html>
+260 -172
View File
@@ -1,6 +1,7 @@
<!--
Automatically generated HTML file from DocOnce source
HTML file automatically generated from DocOnce source
(https://github.com/doconce/doconce/)
doconce format html Project1.do.txt --pygments_html_style=default --html_style=bloodish --html_links_in_new_window --html_output=Project1
-->
<html>
<head>
@@ -8,34 +9,107 @@ Automatically generated HTML file from DocOnce source
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Project 1 on Machine Learning, deadline October 4, 2021">
<title>Project 1 on Machine Learning, deadline October 4, 2021</title>
<style type="text/css">
/* bloodish style */
body {
font-family: Helvetica, Verdana, Arial, Sans-serif;
color: #404040;
background: #ffffff;
}
h1 { font-size: 1.8em; color: #8A0808; }
h2 { font-size: 1.6em; color: #8A0808; }
h3 { font-size: 1.4em; color: #8A0808; }
h4 { color: #8A0808; }
h1 { font-size: 1.8em; color: #8A0808; }
h2 { font-size: 1.6em; color: #8A0808; }
h3 { font-size: 1.4em; color: #8A0808; }
h4 { font-size: 1.2em; color: #8A0808; }
a { color: #8A0808; text-decoration:none; }
tt { font-family: "Courier New", Courier; }
/* pre style removed because it will interfer with pygments */
p { text-indent: 0px; }
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
p.caption { width: 80%; font-style: normal; text-align: left; }
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa}
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa; }div.highlight {
border: 1px solid #cfcfcf;
border-radius: 2px;
line-height: 1.21429em;
}
div.cell {
width: 100%;
padding: 5px 5px 5px 0;
margin: 0;
outline: none;
}
div.input {
page-break-inside: avoid;
box-orient: horizontal;
box-align: stretch;
display: flex;
flex-direction: row;
align-items: stretch;
}
div.inner_cell {
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
box-flex: 1;
flex: 1;
}
div.input_area {
border: 1px solid #cfcfcf;
border-radius: 4px;
background: #f7f7f7;
line-height: 1.21429em;
}
div.input_area > div.highlight {
margin: .4em;
border: none;
padding: 0;
background-color: transparent;
}
div.output_wrapper {
position: relative;
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
}
.output {
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
}
div.output_area {
padding: 0;
page-break-inside: avoid;
box-orient: horizontal;
box-align: stretch;
display: flex;
flex-direction: row;
align-items: stretch;
}
div.output_subarea {
padding: .4em .4em 0 .4em;
box-flex: 1;
flex: 1;
}
div.output_text {
text-align: left;
color: #000;
line-height: 1.21429em;
}
div { text-align: justify; text-justify: inter-word; }
.tab {
padding-left: 1.5em;
}
div.toc p,a {
line-height: 1.3;
margin-top: 1.1;
margin-bottom: 1.1;
}
</style>
</head>
<!-- tocinfo
@@ -105,39 +179,32 @@ MathJax.Hub.Config({
</script>
<!-- ------------------- main content ---------------------- -->
<center>
<h1>Project 1 on Machine Learning, deadline October 4, 2021</h1>
</center> <!-- document title -->
<center><h1>Project 1 on Machine Learning, deadline October 4, 2021</h1></center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_blank">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_blank">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center><b>University of Oslo, Norway</b></center>
<center>
<b>University of Oslo, Norway</b>
</center>
<br>
<p>
<center><h4>Oct 4, 2021</h4></center> <!-- date -->
<center>
<h4>Nov 22, 2021</h4>
</center> <!-- date -->
<br>
<h2 id="regression-analysis-and-resampling-methods">Regression analysis and resampling methods </h2>
<p>
The main aim of this project is to study in more detail various
<p>The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method,
The total score is <b>100</b> points. Each subtask has its own final score.
</p>
<p>
We will first study how to fit polynomials to a specific
<p>We will first study how to fit polynomials to a specific
two-dimensional function called <a href="http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf" target="_blank">Franke's
function</a>. This
is a function which has been widely used when testing various
@@ -146,9 +213,9 @@ established the model and the method, we will employ resamling
techniques such as cross-validation and/or bootstrap in order to perform a
proper assessment of our models. We will also study in detail the
so-called Bias-Variance trade off.
</p>
<p>
The Franke function, which is a weighted sum of four exponentials reads as follows
<p>The Franke function, which is a weighted sum of four exponentials reads as follows</p>
$$
\begin{align*}
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
@@ -156,8 +223,7 @@ f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}
\end{align*}
$$
<p>
The function will be defined for \( x,y\in [0,1] \). Our first step will
<p>The function will be defined for \( x,y\in [0,1] \). Our first step will
be to perform an OLS regression analysis of this function, trying out
a polynomial fit with an \( x \) and \( y \) dependence of the form \( [x, y,
x^2, y^2, xy, \dots] \). We will also include bootstrap first as
@@ -170,19 +236,23 @@ function (for example a polynomial) of \( x \) and \( y \). Thereafter we
will repeat much of the same procedure using the Ridge and Lasso
regression methods, introducing thus a dependence on the bias
(penalty) \( \lambda \).
</p>
<p>
Finally we are going to use (real) digital terrain data and try to
<p>Finally we are going to use (real) digital terrain data and try to
reproduce these data using the same methods. We will also try to go
beyond the second-order polynomials metioned above and explore
which polynomial fits the data best.
</p>
<p>
The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
<p>
<p>The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> cm
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.ticker</span> <span style="color: #008000; font-weight: bold">import</span> LinearLocator, FormatStrFormatter
@@ -221,220 +291,245 @@ ax<span style="color: #666666">.</span>zaxis<span style="color: #666666">.</span
fig<span style="color: #666666">.</span>colorbar(surf, shrink<span style="color: #666666">=0.5</span>, aspect<span style="color: #666666">=5</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<h3 id="exercise-1-ordinary-least-square-ols-on-the-franke-function-score-10-points">Exercise 1: Ordinary Least Square (OLS) on the Franke function (score 10 points) </h3>
<p>
We will generate our own dataset for a function
<p>We will generate our own dataset for a function
\( \mathrm{FrankeFunction}(x,y) \) with \( x,y \in [0,1] \). The function
\( f(x,y) \) is the Franke function. You should explore also the addition
of an added stochastic noise to this function using the normal
distribution \( N(0,1) \).
</p>
<p>
<em>Write your own code</em> (using either a matrix inversion or a singular
<p><em>Write your own code</em> (using either a matrix inversion or a singular
value decomposition from e.g., <b>numpy</b> ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in \( x \) and \( y \) up to fifth order. Find the
<a href="https://en.wikipedia.org/wiki/Confidence_interval" target="_blank">confidence intervals</a> of the parameters (estimators) \( \beta \) by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in \( x \) and \( y \) up to fifth order.
</p>
<p>Evaluate the Mean Squared error (MSE)</p>
$$ MSE(\boldsymbol{y},\tilde{\boldsymbol{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
$$
<p>
and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
<p>and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
value of the \( i-th \) sample and \( y_i \) is the corresponding true value,
then the score \( R^2 \) is defined as
</p>
$$
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
$$
<p>
where we have defined the mean value of \( \boldsymbol{y} \) as
<p>where we have defined the mean value of \( \boldsymbol{y} \) as</p>
$$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
<p>
Your code has to include a scaling of the data (for example by
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters \( \beta \) as you increase the order of the polynomial. Comment your results.
</p>
<p>Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for
splitting training data provided by the library <b>Scikit-Learn</b> (make
sure you have installed it). This function is called
\( train\_test\_split \). <b>You should present a critical discussion of why and how you have scaled or not scaled the data</b>.
</p>
<p>
It is normal in essentially all Machine Learning studies to split the
<p>It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (eventually also an additional
validation set). There
is no explicit recipe for how much data should be included as training
data and say test data. An accepted rule of thumb is to use
approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
</p>
<p>
You can easily reuse the solutions to your exercises from week 35 and week 36.
<p>You can easily reuse the solutions to your exercises from week 35 and week 36.</p>
<h3 id="exercise-2-bias-variance-trade-off-and-resampling-techniques-score-15-points">Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points) </h3>
<p>
Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.</p>
<p>
With a code which does OLS and includes resampling techniques,
<p>With a code which does OLS and includes resampling techniques,
we will now discuss the bias-variance trade-off in the context of
continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
tasks and basically all Machine Learning algorithms.
</p>
<p>
Before you perform an analysis of the bias-variance trade-off on your test data, make
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
</p>
<p>
With this result we move on to the bias-variance trade-off analysis.
<p>With this result we move on to the bias-variance trade-off analysis.</p>
<p>
Consider a
<p>Consider a
dataset \( \mathcal{L} \) consisting of the data
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
</p>
<p>
Let us assume that the true data is generated from a noisy model
<p>Let us assume that the true data is generated from a noisy model</p>
$$
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
$$
<p>
Here \( \epsilon \) is normally distributed with mean zero and standard
<p>Here \( \epsilon \) is normally distributed with mean zero and standard
deviation \( \sigma^2 \).
</p>
<p>
In our derivation of the ordinary least squares method we defined then
<p>In our derivation of the ordinary least squares method we defined then
an approximation to the function \( f \) in terms of the parameters
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
</p>
<p>
The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means
squared error via the so-called cost function
</p>
$$
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
$$
Here the expected value \( \mathbb{E} \) is the sample value.
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
<p>
Show that you can rewrite this as
<p>Show that you can rewrite this as</p>
$$
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
$$
<p>
Explain what the terms mean, which one is the bias and which one is
<p>Explain what the terms mean, which one is the bias and which one is
the variance and discuss their interpretations.
</p>
<p>
Perform then a bias-variance analysis of the Franke function by
<p>Perform then a bias-variance analysis of the Franke function by
studying the MSE value as function of the complexity of your model.
</p>
<p>
Discuss the bias and variance trade-off as function
<p>Discuss the bias and variance trade-off as function
of your model complexity (the degree of the polynomial) and the number
of data points, and possibly also your training and test data using the <b>bootstrap</b> resampling method.
</p>
<p>
Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).
<p>Note also that when you calculate the bias, in all applications you don't know the function values \( f_i \). You would hence replace them with the actual data points \( y_i \).</p>
<h3 id="exercise-3-cross-validation-as-resampling-techniques-adding-more-complexity-score-15-points">Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points) </h3>
<p>
The aim here is to write your own code for another widely popular
<p>The aim here is to write your own code for another widely popular
resampling technique, the so-called cross-validation method. Again,
before you start with cross-validation approach, you should scale your
data if you think this is needed.
</p>
<p>
Implement the \( k \)-fold cross-validation algorithm (write your own
<p>Implement the \( k \)-fold cross-validation algorithm (write your own
code) and evaluate again the MSE function resulting
from the test folds. You can compare your own code with that from
<b>Scikit-Learn</b> if needed.
<b>Scikit-Learn</b> if needed.
</p>
<p>
Compare the MSE you get from your cross-validation code with the one
<p>Compare the MSE you get from your cross-validation code with the one
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
folds. You can also compare your own cross-validation code with the
one provided by <b>Scikit-Learn</b>.
</p>
<h3 id="exercise-4-ridge-regression-on-the-franke-function-with-resampling-score-20-points">Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points) </h3>
<p>
Write your own code for the Ridge method, either using matrix
<p>Write your own code for the Ridge method, either using matrix
inversion or the singular value decomposition as done in the previous
exercise. Perform the same bootstrap analysis as in the
Exercise 2 (for the same polynomials) and the cross-validation in exercise 3 but now for different values of \( \lambda \). Compare and
analyze your results with those obtained in exercises 1-3. Study the
dependence on \( \lambda \).
</p>
<p>
Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
<p>Study also the bias-variance trade-off as function of various values of
the parameter \( \lambda \). For the bias-variance trade-off, use the <b>bootstrap</b> resampling method. Comment your results.
</p>
<h3 id="exercise-5-lasso-regression-on-the-franke-function-with-resampling-score-10-points">Exercise 5: Lasso Regression on the Franke function with resampling (Score 10 points)) </h3>
<p>
This exercise is essentially a repeat of the previous two ones, but now
<p>This exercise is essentially a repeat of the previous two ones, but now
with Lasso regression. Write either your own code (difficult and optional) or, in this case,
you can also use the functionalities of <b>Scikit-Learn</b> (recommended).
Give a
critical discussion of the three methods and a judgement of which
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <b>bootstrap</b> resampling technique and an analysis of the mean squared error using cross-validation.
</p>
<h3 id="exercise-6-analysis-of-real-data-score-30-points">Exercise 6: Analysis of real data (score 30 points) </h3>
<p>
With our codes functioning and having been tested properly on a
<p>With our codes functioning and having been tested properly on a
simpler function we are now ready to look at real data. We will
essentially repeat in this exercise what was done in exercises 1-5. However, we
need first to download the data and prepare properly the inputs to our
codes. We are going to download digital terrain data from the website
<a href="https://earthexplorer.usgs.gov/" target="_blank"><tt>https://earthexplorer.usgs.gov/</tt></a>,
</p>
<p>
Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a>
<p>Or, if you prefer, we have placed selected datafiles at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles</tt></a></p>
<p>
In order to obtain data for a specific region, you need to register as
<p>In order to obtain data for a specific region, you need to register as
a user (free) at this website and then decide upon which area you want
to fetch the digital terrain data from. In order to be able to read
the data properly, you need to specify that the format should be <b>SRTM
Arc-Second Global</b> and download the data as a <b>GeoTIF</b> file. The
files are then stored in <em>tif</em> format which can be imported into a
Python program using
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre></div>
<p>
Here is a simple part of a Python code which reads and plots the data
<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%;">scipy<span style="color: #666666">.</span>misc<span style="color: #666666">.</span>imread
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>Here is a simple part of a Python code which reads and plots the data
from such files
</p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></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>
<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: #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">from</span> <span style="color: #0000FF; font-weight: bold">imageio</span> <span style="color: #008000; font-weight: bold">import</span> imread
<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>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">mpl_toolkits.mplot3d</span> <span style="color: #008000; font-weight: bold">import</span> Axes3D
@@ -449,39 +544,49 @@ plt<span style="color: #666666">.</span>imshow(terrain1, cmap<span style="color:
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;X&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Y&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
If you should have problems in downloading the digital terrain data,
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>If you should have problems in downloading the digital terrain data,
we provide two examples under the data folder of project 1. One is
from a region close to Stavanger in Norway and the other M&#248;svatn
Austfjell, again in Norway.
Feel free to produce your own terrain data.
</p>
<p>
Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_blank">kaggle.com</a> for examples.
<p>Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a href="https://www.kaggle.com/datasets" target="_blank">kaggle.com</a> for examples.</p>
<p>
Our final part deals with the parameterization of your digital terrain
<p>Our final part deals with the parameterization of your digital terrain
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
approximation and cross-validation as resampling technique to evaluate which
model fits the data best.
</p>
<p>
At the end, you should present a critical evaluation of your results
<p>At the end, you should present a critical evaluation of your results
and discuss the applicability of these regression methods to the type
of data presented here (either the terrain data we propose or other data sets).
</p>
<h2 id="background-literature">Background literature </h2>
<ol>
<li> For a discussion and derivation of the variances and mean squared errors using linear regression, see the <a href="https://arxiv.org/abs/1509.09169" target="_blank">Lecture notes on ridge regression by Wessel N. van Wieringen</a></li>
<li> The textbook of <a href="https://www.springer.com/gp/book/9780387848570" target="_blank">Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer</a>, chapters 3 and 7 are the most relevant ones for the analysis here.</li>
</ol>
<h2 id="introduction-to-numerical-projects">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.
<p>Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.</p>
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
@@ -494,63 +599,51 @@ Here follows a brief recipe and recommendation on how to answer the various ques
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="format-for-electronic-delivery-of-report-and-programs">Format for electronic delivery of report and programs </h2>
<p>
The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:
<p>The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:</p>
<ul>
<li> Use Canvas to hand in your projects, log in at <a href="https://www.uio.no/english/services/it/education/canvas/" target="_blank"><tt>https://www.uio.no/english/services/it/education/canvas/</tt></a> with your normal UiO username and password.</li>
<li> Upload <b>only</b> the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.</li>
<li> In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.</li>
</ul>
Finally,
<p>Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.
2-3 students. You can then hand in a common report.
</p>
<h2 id="software-and-needed-installations">Software and needed installations </h2>
<p>
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
<p>If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via <b>pip</b> as
</p>
<ol>
<li> pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow</li>
</ol>
<p>For Python3, replace <b>pip</b> with <b>pip3</b>.</p>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>. </p>
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>.
<p>
For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
<p>For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
for a seamless installation of additional software via for example
</p>
<ol>
<li> brew install python3</li>
</ol>
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
<p>For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use <b>pip</b> as well and simply install Python as
</p>
<ol>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
<p>etc etc. </p>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<p>If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely</p>
<ol>
<li> <a href="https://docs.anaconda.com/" target="_blank">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
<li> <a href="https://www.enthought.com/product/canopy/" target="_blank">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
</ol>
Popular software packages written in Python for ML are
<p>Popular software packages written in Python for ML are</p>
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>,</li>
@@ -558,17 +651,12 @@ Popular software packages written in Python for ML are
<li> <a href="http://pytorch.org/" target="_blank">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_blank">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
<p>These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<p>
</p>
<!-- ------------------- end of main content --------------- -->
</body>
</html>
+198 -66
View File
@@ -2,23 +2,42 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"id": "93d44361",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html Project1.do.txt -->\n",
"<!-- dom:TITLE: Project 1 on Machine Learning, deadline October 4, 2021 -->"
]
},
{
"cell_type": "markdown",
"id": "272bee31",
"metadata": {
"editable": true
},
"source": [
"<!-- dom:TITLE: Project 1 on Machine Learning, deadline October 4, 2021 -->\n",
"# Project 1 on Machine Learning, deadline October 4, 2021\n",
"<!-- dom:AUTHOR: [Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html) at University of Oslo, Norway -->\n",
"<!-- Author: --> \n",
"**[Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html)**, University of Oslo, Norway\n",
"\n",
"Date: **Oct 4, 2021**\n",
"\n",
"Date: **Nov 22, 2021**"
]
},
{
"cell_type": "markdown",
"id": "e6dd2dcb",
"metadata": {
"editable": true
},
"source": [
"## Regression analysis and resampling methods\n",
"\n",
"The main aim of this project is to study in more detail various\n",
"regression methods, including the Ordinary Least Squares (OLS) method,\n",
"The total score is **100** points. Each subtask has its own final score.\n",
"\n",
"\n",
"We will first study how to fit polynomials to a specific\n",
"two-dimensional function called [Franke's\n",
"function](http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf). This\n",
@@ -29,13 +48,15 @@
"proper assessment of our models. We will also study in detail the\n",
"so-called Bias-Variance trade off.\n",
"\n",
"\n",
"The Franke function, which is a weighted sum of four exponentials reads as follows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "2cab7d59",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\begin{align*}\n",
@@ -47,7 +68,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "9b1c11ab",
"metadata": {
"editable": true
},
"source": [
"The function will be defined for $x,y\\in [0,1]$. Our first step will\n",
"be to perform an OLS regression analysis of this function, trying out\n",
@@ -68,13 +92,13 @@
"beyond the second-order polynomials metioned above and explore \n",
"which polynomial fits the data best.\n",
"\n",
"\n",
"The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "ce58ef26",
"metadata": {
"collapsed": false,
"editable": true
@@ -126,7 +150,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "af3d048d",
"metadata": {
"editable": true
},
"source": [
"### Exercise 1: Ordinary Least Square (OLS) on the Franke function (score 10 points)\n",
"\n",
@@ -139,14 +166,17 @@
"*Write your own code* (using either a matrix inversion or a singular\n",
"value decomposition from e.g., **numpy** ) or use your code from\n",
"homeworks 1 and 2 and perform a standard least square regression\n",
"analysis using polynomials in $x$ and $y$ up to fifth order. Find the\n",
"[confidence intervals](https://en.wikipedia.org/wiki/Confidence_interval) of the parameters (estimators) $\\beta$ by computing their\n",
"variances, evaluate the Mean Squared error (MSE)"
"analysis using polynomials in $x$ and $y$ up to fifth order.\n",
"\n",
"Evaluate the Mean Squared error (MSE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "c87a566e",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"MSE(\\boldsymbol{y},\\tilde{\\boldsymbol{y}}) = \\frac{1}{n}\n",
@@ -156,7 +186,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "728f699c",
"metadata": {
"editable": true
},
"source": [
"and the $R^2$ score function. If $\\tilde{\\boldsymbol{y}}_i$ is the predicted\n",
"value of the $i-th$ sample and $y_i$ is the corresponding true value,\n",
@@ -165,7 +198,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "603545f4",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
@@ -174,14 +210,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "b4eef382",
"metadata": {
"editable": true
},
"source": [
"where we have defined the mean value of $\\boldsymbol{y}$ as"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "ccf144a4",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\bar{y} = \\frac{1}{n} \\sum_{i=0}^{n - 1} y_i.\n",
@@ -190,9 +232,15 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "da524531",
"metadata": {
"editable": true
},
"source": [
"Your code has to include a scaling of the data (for example by\n",
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).\n",
"Plot also the parameters $\\beta$ as you increase the order of the polynomial. Comment your results.\n",
"\n",
"Your code has to include a scaling/centering of the data (for example by\n",
"subtracting the mean value), and\n",
"a split of the data in training and test data. For this exercise you can\n",
"either write your own code or use for example the function for\n",
@@ -200,7 +248,6 @@
"sure you have installed it). This function is called\n",
"$train\\_test\\_split$. **You should present a critical discussion of why and how you have scaled or not scaled the data**.\n",
"\n",
"\n",
"It is normal in essentially all Machine Learning studies to split the\n",
"data in a training set and a test set (eventually also an additional\n",
"validation set). There\n",
@@ -208,11 +255,16 @@
"data and say test data. An accepted rule of thumb is to use\n",
"approximately $2/3$ to $4/5$ of the data as training data.\n",
"\n",
"\n",
"You can easily reuse the solutions to your exercises from week 35 and week 36.\n",
"\n",
"\n",
"\n",
"You can easily reuse the solutions to your exercises from week 35 and week 36."
]
},
{
"cell_type": "markdown",
"id": "62fa548f",
"metadata": {
"editable": true
},
"source": [
"### Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points)\n",
"\n",
"Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.\n",
@@ -240,7 +292,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "a5b3ef67",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
@@ -249,7 +304,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "34fe636c",
"metadata": {
"editable": true
},
"source": [
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
"deviation $\\sigma^2$.\n",
@@ -265,7 +323,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "3ab7ad05",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"C(\\boldsymbol{X},\\boldsymbol{\\beta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
@@ -274,7 +335,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "7a5d9046",
"metadata": {
"editable": true
},
"source": [
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
"\n",
@@ -283,7 +347,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "41b867a2",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\frac{1}{n}\\sum_i(f_i-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right])^2+\\frac{1}{n}\\sum_i(\\tilde{y}_i-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right])^2+\\sigma^2.\n",
@@ -292,7 +359,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "46c67e41",
"metadata": {
"editable": true
},
"source": [
"Explain what the terms mean, which one is the bias and which one is\n",
"the variance and discuss their interpretations.\n",
@@ -304,9 +374,16 @@
"of your model complexity (the degree of the polynomial) and the number\n",
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
"\n",
"Note also that when you calculate the bias, in all applications you don't know the function values $f_i$. You would hence replace them with the actual data points $y_i$.\n",
"\n",
"\n",
"Note also that when you calculate the bias, in all applications you don't know the function values $f_i$. You would hence replace them with the actual data points $y_i$."
]
},
{
"cell_type": "markdown",
"id": "9e7e141f",
"metadata": {
"editable": true
},
"source": [
"### Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points)\n",
"\n",
"The aim here is to write your own code for another widely popular\n",
@@ -322,9 +399,16 @@
"Compare the MSE you get from your cross-validation code with the one\n",
"you got from your **bootstrap** code. Comment your results. Try $5-10$\n",
"folds. You can also compare your own cross-validation code with the\n",
"one provided by **Scikit-Learn**.\n",
"\n",
"\n",
"one provided by **Scikit-Learn**."
]
},
{
"cell_type": "markdown",
"id": "2f5f9e1c",
"metadata": {
"editable": true
},
"source": [
"### Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points)\n",
"\n",
"Write your own code for the Ridge method, either using matrix\n",
@@ -335,8 +419,16 @@
"dependence on $\\lambda$.\n",
"\n",
"Study also the bias-variance trade-off as function of various values of\n",
"the parameter $\\lambda$. For the bias-variance trade-off, use the **bootstrap** resampling method. Comment your results. \n",
"\n",
"the parameter $\\lambda$. For the bias-variance trade-off, use the **bootstrap** resampling method. Comment your results."
]
},
{
"cell_type": "markdown",
"id": "7f98c28e",
"metadata": {
"editable": true
},
"source": [
"### Exercise 5: Lasso Regression on the Franke function with resampling (Score 10 points))\n",
"\n",
"This exercise is essentially a repeat of the previous two ones, but now\n",
@@ -344,8 +436,16 @@
"you can also use the functionalities of **Scikit-Learn** (recommended). \n",
"Give a\n",
"critical discussion of the three methods and a judgement of which\n",
"model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the **bootstrap** resampling technique and an analysis of the mean squared error using cross-validation. \n",
"\n",
"model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the **bootstrap** resampling technique and an analysis of the mean squared error using cross-validation."
]
},
{
"cell_type": "markdown",
"id": "a0c27fc5",
"metadata": {
"editable": true
},
"source": [
"### Exercise 6: Analysis of real data (score 30 points)\n",
"\n",
"With our codes functioning and having been tested properly on a\n",
@@ -368,7 +468,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "95eaae34",
"metadata": {
"collapsed": false,
"editable": true
@@ -380,7 +481,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "9928c479",
"metadata": {
"editable": true
},
"source": [
"Here is a simple part of a Python code which reads and plots the data\n",
"from such files"
@@ -388,7 +492,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "5625cc03",
"metadata": {
"collapsed": false,
"editable": true
@@ -414,7 +519,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "0190e320",
"metadata": {
"editable": true
},
"source": [
"If you should have problems in downloading the digital terrain data,\n",
"we provide two examples under the data folder of project 1. One is\n",
@@ -422,10 +530,8 @@
"Austfjell, again in Norway.\n",
"Feel free to produce your own terrain data.\n",
"\n",
"\n",
"Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example [kaggle.com](https://www.kaggle.com/datasets) for examples.\n",
"\n",
"\n",
"Our final part deals with the parameterization of your digital terrain\n",
"data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial\n",
"approximation and cross-validation as resampling technique to evaluate which\n",
@@ -433,22 +539,34 @@
"\n",
"At the end, you should present a critical evaluation of your results\n",
"and discuss the applicability of these regression methods to the type\n",
"of data presented here (either the terrain data we propose or other data sets).\n",
"\n",
"\n",
"\n",
"\n",
"of data presented here (either the terrain data we propose or other data sets)."
]
},
{
"cell_type": "markdown",
"id": "c0014ba8",
"metadata": {
"editable": true
},
"source": [
"## Background literature\n",
"\n",
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
"\n",
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here. \n",
"\n",
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
]
},
{
"cell_type": "markdown",
"id": "69ffae21",
"metadata": {
"editable": true
},
"source": [
"## Introduction to numerical projects\n",
"\n",
"Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.\n",
"\n",
"\n",
" * Give a short description of the nature of the problem and the eventual numerical methods you have used.\n",
"\n",
" * Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.\n",
@@ -465,8 +583,16 @@
"\n",
" * Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.\n",
"\n",
" * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.\n",
"\n",
" * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning."
]
},
{
"cell_type": "markdown",
"id": "a6bccf3d",
"metadata": {
"editable": true
},
"source": [
"## Format for electronic delivery of report and programs\n",
"\n",
"The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:\n",
@@ -479,10 +605,16 @@
"\n",
"Finally, \n",
"we encourage you to collaborate. Optimal working groups consist of \n",
"2-3 students. You can then hand in a common report. \n",
"\n",
"\n",
"\n",
"2-3 students. You can then hand in a common report."
]
},
{
"cell_type": "markdown",
"id": "20f309ed",
"metadata": {
"editable": true
},
"source": [
"## Software and needed installations\n",
"\n",
"If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, \n",
@@ -526,5 +658,5 @@
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 5
}
+74 -27
View File
@@ -1,7 +1,7 @@
%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%% (https://github.com/doconce/doconce/)
%% doconce format latex Project1.do.txt --print_latex_style=trac --latex_admon=paragraph
%%
% #ifdef PTEX2TEX_EXPLANATION
%%
@@ -132,7 +132,7 @@ Project 1 on Machine Learning, deadline October 4, 2021
% --- begin date ---
\begin{center}
Oct 4, 2021
Nov 22, 2021
\end{center}
% --- end date ---
@@ -145,7 +145,6 @@ The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method,
The total score is \textbf{100} points. Each subtask has its own final score.
We will first study how to fit polynomials to a specific
two-dimensional function called \href{{http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf}}{Franke's
function}. This
@@ -156,7 +155,6 @@ techniques such as cross-validation and/or bootstrap in order to perform a
proper assessment of our models. We will also study in detail the
so-called Bias-Variance trade off.
The Franke function, which is a weighted sum of four exponentials reads as follows
\begin{align*}
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
@@ -182,8 +180,48 @@ reproduce these data using the same methods. We will also try to go
beyond the second-order polynomials metioned above and explore
which polynomial fits the data best.
The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
\bpycod
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
@@ -225,6 +263,7 @@ fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
\epycod
@@ -238,9 +277,9 @@ distribution $N(0,1)$.
\emph{Write your own code} (using either a matrix inversion or a singular
value decomposition from e.g., \textbf{numpy} ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in $x$ and $y$ up to fifth order. Find the
\href{{https://en.wikipedia.org/wiki/Confidence_interval}}{confidence intervals} of the parameters (estimators) $\beta$ by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in $x$ and $y$ up to fifth order.
Evaluate the Mean Squared error (MSE)
\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
@@ -260,7 +299,10 @@ where we have defined the mean value of $\bm{y}$ as
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
\]
Your code has to include a scaling of the data (for example by
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters $\beta$ as you increase the order of the polynomial. Comment your results.
Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for
@@ -268,7 +310,6 @@ splitting training data provided by the library \textbf{Scikit-Learn} (make
sure you have installed it). This function is called
$train\_test\_split$. \textbf{You should present a critical discussion of why and how you have scaled or not scaled the data}.
It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (eventually also an additional
validation set). There
@@ -276,11 +317,8 @@ is no explicit recipe for how much data should be included as training
data and say test data. An accepted rule of thumb is to use
approximately $2/3$ to $4/5$ of the data as training data.
You can easily reuse the solutions to your exercises from week 35 and week 36.
\paragraph{Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points).}
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
@@ -341,7 +379,6 @@ of data points, and possibly also your training and test data using the \textbf{
Note also that when you calculate the bias, in all applications you don't know the function values $f_i$. You would hence replace them with the actual data points $y_i$.
\paragraph{Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points).}
The aim here is to write your own code for another widely popular
resampling technique, the so-called cross-validation method. Again,
@@ -358,7 +395,6 @@ you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
folds. You can also compare your own cross-validation code with the
one provided by \textbf{Scikit-Learn}.
\paragraph{Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points).}
Write your own code for the Ridge method, either using matrix
inversion or the singular value decomposition as done in the previous
@@ -396,13 +432,33 @@ Arc-Second Global} and download the data as a \textbf{GeoTIF} file. The
files are then stored in \emph{tif} format which can be imported into a
Python program using
\bpycod
scipy.misc.imread
\epycod
Here is a simple part of a Python code which reads and plots the data
from such files
\bpycod
import numpy as np
from imageio import imread
@@ -419,18 +475,18 @@ plt.imshow(terrain1, cmap='gray')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
\epycod
If you should have problems in downloading the digital terrain data,
we provide two examples under the data folder of project 1. One is
from a region close to Stavanger in Norway and the other Møsvatn
Austfjell, again in Norway.
Feel free to produce your own terrain data.
Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example \href{{https://www.kaggle.com/datasets}}{kaggle.com} for examples.
Our final part deals with the parameterization of your digital terrain
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
approximation and cross-validation as resampling technique to evaluate which
@@ -440,9 +496,6 @@ At the end, you should present a critical evaluation of your results
and discuss the applicability of these regression methods to the type
of data presented here (either the terrain data we propose or other data sets).
\subsection{Background literature}
\begin{enumerate}
@@ -456,7 +509,6 @@ of data presented here (either the terrain data we propose or other data sets).
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
@@ -495,8 +547,6 @@ Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.
\subsection{Software and needed installations}
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
@@ -552,9 +602,6 @@ encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
% ------------------- end of main content ---------------
% #ifdef PREAMBLE
Binary file not shown.
+74 -27
View File
@@ -1,7 +1,7 @@
%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%% (https://github.com/doconce/doconce/)
%% doconce format latex Project1.do.txt --print_latex_style=trac --latex_admon=paragraph
%%
@@ -102,7 +102,7 @@ Project 1 on Machine Learning, deadline October 4, 2021
% --- begin date ---
\begin{center}
Oct 4, 2021
Nov 22, 2021
\end{center}
% --- end date ---
@@ -115,7 +115,6 @@ The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method,
The total score is \textbf{100} points. Each subtask has its own final score.
We will first study how to fit polynomials to a specific
two-dimensional function called \href{{http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf}}{Franke's
function}. This
@@ -126,7 +125,6 @@ techniques such as cross-validation and/or bootstrap in order to perform a
proper assessment of our models. We will also study in detail the
so-called Bias-Variance trade off.
The Franke function, which is a weighted sum of four exponentials reads as follows
\begin{align*}
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
@@ -152,8 +150,48 @@ reproduce these data using the same methods. We will also try to go
beyond the second-order polynomials metioned above and explore
which polynomial fits the data best.
The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)
\begin{verbatim}
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
@@ -195,6 +233,7 @@ fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
\end{verbatim}
@@ -208,9 +247,9 @@ distribution $N(0,1)$.
\emph{Write your own code} (using either a matrix inversion or a singular
value decomposition from e.g., \textbf{numpy} ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in $x$ and $y$ up to fifth order. Find the
\href{{https://en.wikipedia.org/wiki/Confidence_interval}}{confidence intervals} of the parameters (estimators) $\beta$ by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in $x$ and $y$ up to fifth order.
Evaluate the Mean Squared error (MSE)
\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
@@ -230,7 +269,10 @@ where we have defined the mean value of $\bm{y}$ as
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
\]
Your code has to include a scaling of the data (for example by
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters $\beta$ as you increase the order of the polynomial. Comment your results.
Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for
@@ -238,7 +280,6 @@ splitting training data provided by the library \textbf{Scikit-Learn} (make
sure you have installed it). This function is called
$train\_test\_split$. \textbf{You should present a critical discussion of why and how you have scaled or not scaled the data}.
It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (eventually also an additional
validation set). There
@@ -246,11 +287,8 @@ is no explicit recipe for how much data should be included as training
data and say test data. An accepted rule of thumb is to use
approximately $2/3$ to $4/5$ of the data as training data.
You can easily reuse the solutions to your exercises from week 35 and week 36.
\paragraph{Exercise 2: Bias-variance trade-off and resampling techniques (score 15 points).}
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
@@ -311,7 +349,6 @@ of data points, and possibly also your training and test data using the \textbf{
Note also that when you calculate the bias, in all applications you don't know the function values $f_i$. You would hence replace them with the actual data points $y_i$.
\paragraph{Exercise 3: Cross-validation as resampling techniques, adding more complexity (score 15 points).}
The aim here is to write your own code for another widely popular
resampling technique, the so-called cross-validation method. Again,
@@ -328,7 +365,6 @@ you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
folds. You can also compare your own cross-validation code with the
one provided by \textbf{Scikit-Learn}.
\paragraph{Exercise 4: Ridge Regression on the Franke function with resampling (score 20 points).}
Write your own code for the Ridge method, either using matrix
inversion or the singular value decomposition as done in the previous
@@ -366,13 +402,33 @@ Arc-Second Global} and download the data as a \textbf{GeoTIF} file. The
files are then stored in \emph{tif} format which can be imported into a
Python program using
\begin{verbatim}
scipy.misc.imread
\end{verbatim}
Here is a simple part of a Python code which reads and plots the data
from such files
\begin{verbatim}
import numpy as np
from imageio import imread
@@ -389,18 +445,18 @@ plt.imshow(terrain1, cmap='gray')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
\end{verbatim}
If you should have problems in downloading the digital terrain data,
we provide two examples under the data folder of project 1. One is
from a region close to Stavanger in Norway and the other Møsvatn
Austfjell, again in Norway.
Feel free to produce your own terrain data.
Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example \href{{https://www.kaggle.com/datasets}}{kaggle.com} for examples.
Our final part deals with the parameterization of your digital terrain
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
approximation and cross-validation as resampling technique to evaluate which
@@ -410,9 +466,6 @@ At the end, you should present a critical evaluation of your results
and discuss the applicability of these regression methods to the type
of data presented here (either the terrain data we propose or other data sets).
\subsection*{Background literature}
\begin{enumerate}
@@ -426,7 +479,6 @@ of data presented here (either the terrain data we propose or other data sets).
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. Note that you can answer question by question and there is no need to structure your report as a scientific report with abstract, introduction, theory, results and discussions, conclusions etc. But you have the following elements in mind when you answer the various questions.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
@@ -465,8 +517,6 @@ Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.
\subsection*{Software and needed installations}
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
@@ -522,9 +572,6 @@ encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
% ------------------- end of main content ---------------
\end{document}
@@ -106,9 +106,10 @@ distribution $N(0,1)$.
*Write your own code* (using either a matrix inversion or a singular
value decomposition from e.g., _numpy_ ) or use your code from
homeworks 1 and 2 and perform a standard least square regression
analysis using polynomials in $x$ and $y$ up to fifth order. Find the
"confidence intervals":"https://en.wikipedia.org/wiki/Confidence_interval" of the parameters (estimators) $\beta$ by computing their
variances, evaluate the Mean Squared error (MSE)
analysis using polynomials in $x$ and $y$ up to fifth order.
Evaluate the Mean Squared error (MSE)
!bt
\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n}
@@ -134,7 +135,10 @@ where we have defined the mean value of $\bm{y}$ as
\]
!et
Your code has to include a scaling of the data (for example by
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five).
Plot also the parameters $\beta$ as you increase the order of the polynomial. Comment your results.
Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
either write your own code or use for example the function for