Files
FYS-STK4155/doc/pub/week34/html/._week34-bs066.html
T
Morten Hjorth-Jensen 6f55e215de update week34
2021-08-27 07:36:50 +02:00

701 lines
43 KiB
HTML

<!--
Automatically generated HTML file from DocOnce source
(https://github.com/doconce/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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="Week 35: Introduction to the course, Logistics and Practicalities">
<title>Week 35: Introduction to the course, Logistics and Practicalities</title>
<!-- Bootstrap style: bootstrap -->
<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 {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of first week', 2, None, 'overview-of-first-week'),
('Reading Recommendations', 2, None, 'reading-recommendations'),
('Thursday August 26', 2, None, 'thursday-august-26'),
('Lectures and ComputerLab', 2, None, 'lectures-and-computerlab'),
('Announcement', 2, None, 'announcement'),
('Communication channels', 2, None, 'communication-channels'),
('Course Format', 2, None, 'course-format'),
('Teachers', 2, None, 'teachers'),
('Deadlines for projects (tentative)',
2,
None,
'deadlines-for-projects-tentative'),
('Recommended textbooks', 2, None, 'recommended-textbooks'),
('Prerequisites', 2, None, 'prerequisites'),
('Learning outcomes', 2, None, 'learning-outcomes'),
('Topics covered in this course: Statistical analysis and '
'optimization of data',
2,
None,
'topics-covered-in-this-course-statistical-analysis-and-optimization-of-data'),
('Topics covered in this course: Machine Learning',
2,
None,
'topics-covered-in-this-course-machine-learning'),
('Extremely useful tools, strongly recommended',
2,
None,
'extremely-useful-tools-strongly-recommended'),
('Other courses on Data science and Machine Learning at UiO',
2,
None,
'other-courses-on-data-science-and-machine-learning-at-uio'),
('Introduction', 2, None, 'introduction'),
('What is Machine Learning?',
2,
None,
'what-is-machine-learning'),
('Types of Machine Learning',
2,
None,
'types-of-machine-learning'),
('Essential elements of ML', 2, None, 'essential-elements-of-ml'),
('An optimization/minimization problem',
2,
None,
'an-optimization-minimization-problem'),
('A Frequentist approach to data analysis',
2,
None,
'a-frequentist-approach-to-data-analysis'),
('What is a good model?', 2, None, 'what-is-a-good-model'),
('What is a good model? Can we define it?',
2,
None,
'what-is-a-good-model-can-we-define-it'),
('Software and needed installations',
2,
None,
'software-and-needed-installations'),
('Python installers', 2, None, 'python-installers'),
('Useful Python libraries', 2, None, 'useful-python-libraries'),
('Installing R, C++, cython or Julia',
2,
None,
'installing-r-c-cython-or-julia'),
('Installing R, C++, cython, Numba etc',
2,
None,
'installing-r-c-cython-numba-etc'),
('Numpy examples and Important Matrix and vector handling '
'packages',
2,
None,
'numpy-examples-and-important-matrix-and-vector-handling-packages'),
('Basic Matrix Features', 2, None, 'basic-matrix-features'),
('Some famous Matrices', 3, None, 'some-famous-matrices'),
('More Basic Matrix Features',
3,
None,
'more-basic-matrix-features'),
('Numpy and arrays', 2, None, 'numpy-and-arrays'),
('Matrices in Python', 2, None, 'matrices-in-python'),
('Meet the Pandas', 2, None, 'meet-the-pandas'),
('Friday August 27', 2, None, 'friday-august-27'),
('Simple linear regression model using _scikit-learn_',
3,
None,
'simple-linear-regression-model-using-_scikit-learn_'),
('To our real data: nuclear binding energies. Brief reminder on '
'masses and binding energies',
3,
None,
'to-our-real-data-nuclear-binding-energies-brief-reminder-on-masses-and-binding-energies'),
('Organizing our data', 3, None, 'organizing-our-data'),
('Seeing the wood for the trees',
3,
None,
'seeing-the-wood-for-the-trees'),
('And what about using neural networks?',
3,
None,
'and-what-about-using-neural-networks'),
('A first summary', 2, None, 'a-first-summary'),
('Why Linear Regression (aka Ordinary Least Squares and family)',
2,
None,
'why-linear-regression-aka-ordinary-least-squares-and-family'),
('Regression analysis, overarching aims',
2,
None,
'regression-analysis-overarching-aims'),
('Regression analysis, overarching aims II',
2,
None,
'regression-analysis-overarching-aims-ii'),
('Examples', 2, None, 'examples'),
('General linear models', 2, None, 'general-linear-models'),
('Rewriting the fitting procedure as a linear algebra problem',
2,
None,
'rewriting-the-fitting-procedure-as-a-linear-algebra-problem'),
('Rewriting the fitting procedure as a linear algebra problem, '
'more details',
2,
None,
'rewriting-the-fitting-procedure-as-a-linear-algebra-problem-more-details'),
('Generalizing the fitting procedure as a linear algebra problem',
2,
None,
'generalizing-the-fitting-procedure-as-a-linear-algebra-problem'),
('Generalizing the fitting procedure as a linear algebra problem',
2,
None,
'generalizing-the-fitting-procedure-as-a-linear-algebra-problem'),
('Optimizing our parameters',
2,
None,
'optimizing-our-parameters'),
('Our model for the nuclear binding energies',
2,
None,
'our-model-for-the-nuclear-binding-energies'),
('Optimizing our parameters, more details',
2,
None,
'optimizing-our-parameters-more-details'),
('Interpretations and optimizing our parameters',
2,
None,
'interpretations-and-optimizing-our-parameters'),
('Interpretations and optimizing our parameters',
2,
None,
'interpretations-and-optimizing-our-parameters'),
('Some useful matrix and vector expressions',
2,
None,
'some-useful-matrix-and-vector-expressions'),
('Interpretations and optimizing our parameters',
2,
None,
'interpretations-and-optimizing-our-parameters'),
('Own code for Ordinary Least Squares',
2,
None,
'own-code-for-ordinary-least-squares'),
('Adding error analysis and training set up',
2,
None,
'adding-error-analysis-and-training-set-up'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('The $\\chi^2$ function', 2, None, 'the-chi-2-function'),
('Fitting an Equation of State for Dense Nuclear Matter',
2,
None,
'fitting-an-equation-of-state-for-dense-nuclear-matter'),
('The code', 2, None, 'the-code'),
('Splitting our Data in Training and Test data',
2,
None,
'splitting-our-data-in-training-and-test-data'),
('Exercises for week 36', 2, None, 'exercises-for-week-36'),
('Exercise 1: Setting up various Python environments',
2,
None,
'exercise-1-setting-up-various-python-environments'),
('Exercise 2: making your own data and exploring scikit-learn',
2,
None,
'exercise-2-making-your-own-data-and-exploring-scikit-learn'),
('Exercise 3: Normalizing our data',
2,
None,
'exercise-3-normalizing-our-data')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week34-bs.html">Week 35: Introduction to the course, Logistics and Practicalities</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week34-bs001.html#overview-of-first-week" style="font-size: 80%;"><b>Overview of first week</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs002.html#reading-recommendations" style="font-size: 80%;"><b>Reading Recommendations</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs003.html#thursday-august-26" style="font-size: 80%;"><b>Thursday August 26</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs004.html#lectures-and-computerlab" style="font-size: 80%;"><b>Lectures and ComputerLab</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs005.html#announcement" style="font-size: 80%;"><b>Announcement</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs006.html#communication-channels" style="font-size: 80%;"><b>Communication channels</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs007.html#course-format" style="font-size: 80%;"><b>Course Format</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs008.html#teachers" style="font-size: 80%;"><b>Teachers</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs009.html#deadlines-for-projects-tentative" style="font-size: 80%;"><b>Deadlines for projects (tentative)</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs010.html#recommended-textbooks" style="font-size: 80%;"><b>Recommended textbooks</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs011.html#prerequisites" style="font-size: 80%;"><b>Prerequisites</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs012.html#learning-outcomes" style="font-size: 80%;"><b>Learning outcomes</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs013.html#topics-covered-in-this-course-statistical-analysis-and-optimization-of-data" style="font-size: 80%;"><b>Topics covered in this course: Statistical analysis and optimization of data</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs014.html#topics-covered-in-this-course-machine-learning" style="font-size: 80%;"><b>Topics covered in this course: Machine Learning</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs015.html#extremely-useful-tools-strongly-recommended" style="font-size: 80%;"><b>Extremely useful tools, strongly recommended</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs016.html#other-courses-on-data-science-and-machine-learning-at-uio" style="font-size: 80%;"><b>Other courses on Data science and Machine Learning at UiO</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs017.html#introduction" style="font-size: 80%;"><b>Introduction</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs018.html#what-is-machine-learning" style="font-size: 80%;"><b>What is Machine Learning?</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs019.html#types-of-machine-learning" style="font-size: 80%;"><b>Types of Machine Learning</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs020.html#essential-elements-of-ml" style="font-size: 80%;"><b>Essential elements of ML</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs021.html#an-optimization-minimization-problem" style="font-size: 80%;"><b>An optimization/minimization problem</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs022.html#a-frequentist-approach-to-data-analysis" style="font-size: 80%;"><b>A Frequentist approach to data analysis</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs023.html#what-is-a-good-model" style="font-size: 80%;"><b>What is a good model?</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs024.html#what-is-a-good-model-can-we-define-it" style="font-size: 80%;"><b>What is a good model? Can we define it?</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs025.html#software-and-needed-installations" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs026.html#python-installers" style="font-size: 80%;"><b>Python installers</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs027.html#useful-python-libraries" style="font-size: 80%;"><b>Useful Python libraries</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs028.html#installing-r-c-cython-or-julia" style="font-size: 80%;"><b>Installing R, C++, cython or Julia</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs029.html#installing-r-c-cython-numba-etc" style="font-size: 80%;"><b>Installing R, C++, cython, Numba etc</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs030.html#numpy-examples-and-important-matrix-and-vector-handling-packages" style="font-size: 80%;"><b>Numpy examples and Important Matrix and vector handling packages</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs031.html#basic-matrix-features" style="font-size: 80%;"><b>Basic Matrix Features</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs032.html#some-famous-matrices" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Some famous Matrices</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs033.html#more-basic-matrix-features" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;More Basic Matrix Features</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs034.html#numpy-and-arrays" style="font-size: 80%;"><b>Numpy and arrays</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs035.html#matrices-in-python" style="font-size: 80%;"><b>Matrices in Python</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs036.html#meet-the-pandas" style="font-size: 80%;"><b>Meet the Pandas</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs037.html#friday-august-27" style="font-size: 80%;"><b>Friday August 27</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#simple-linear-regression-model-using-_scikit-learn_" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Simple linear regression model using <b>scikit-learn</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#to-our-real-data-nuclear-binding-energies-brief-reminder-on-masses-and-binding-energies" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;To our real data: nuclear binding energies. Brief reminder on masses and binding energies</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#organizing-our-data" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Organizing our data</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#seeing-the-wood-for-the-trees" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Seeing the wood for the trees</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#and-what-about-using-neural-networks" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;And what about using neural networks?</a></li>
<!-- navigation toc: --> <li><a href="._week34-bs038.html#a-first-summary" style="font-size: 80%;"><b>A first summary</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs039.html#why-linear-regression-aka-ordinary-least-squares-and-family" style="font-size: 80%;"><b>Why Linear Regression (aka Ordinary Least Squares and family)</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs040.html#regression-analysis-overarching-aims" style="font-size: 80%;"><b>Regression analysis, overarching aims</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs041.html#regression-analysis-overarching-aims-ii" style="font-size: 80%;"><b>Regression analysis, overarching aims II</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs042.html#examples" style="font-size: 80%;"><b>Examples</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs043.html#general-linear-models" style="font-size: 80%;"><b>General linear models</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs044.html#rewriting-the-fitting-procedure-as-a-linear-algebra-problem" style="font-size: 80%;"><b>Rewriting the fitting procedure as a linear algebra problem</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs045.html#rewriting-the-fitting-procedure-as-a-linear-algebra-problem-more-details" style="font-size: 80%;"><b>Rewriting the fitting procedure as a linear algebra problem, more details</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs047.html#generalizing-the-fitting-procedure-as-a-linear-algebra-problem" style="font-size: 80%;"><b>Generalizing the fitting procedure as a linear algebra problem</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs047.html#generalizing-the-fitting-procedure-as-a-linear-algebra-problem" style="font-size: 80%;"><b>Generalizing the fitting procedure as a linear algebra problem</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs048.html#optimizing-our-parameters" style="font-size: 80%;"><b>Optimizing our parameters</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs049.html#our-model-for-the-nuclear-binding-energies" style="font-size: 80%;"><b>Our model for the nuclear binding energies</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs050.html#optimizing-our-parameters-more-details" style="font-size: 80%;"><b>Optimizing our parameters, more details</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs054.html#interpretations-and-optimizing-our-parameters" style="font-size: 80%;"><b>Interpretations and optimizing our parameters</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs054.html#interpretations-and-optimizing-our-parameters" style="font-size: 80%;"><b>Interpretations and optimizing our parameters</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs053.html#some-useful-matrix-and-vector-expressions" style="font-size: 80%;"><b>Some useful matrix and vector expressions</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs054.html#interpretations-and-optimizing-our-parameters" style="font-size: 80%;"><b>Interpretations and optimizing our parameters</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs055.html#own-code-for-ordinary-least-squares" style="font-size: 80%;"><b>Own code for Ordinary Least Squares</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs056.html#adding-error-analysis-and-training-set-up" style="font-size: 80%;"><b>Adding error analysis and training set up</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs062.html#the-chi-2-function" style="font-size: 80%;"><b>The \( \chi^2 \) function</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs063.html#fitting-an-equation-of-state-for-dense-nuclear-matter" style="font-size: 80%;"><b>Fitting an Equation of State for Dense Nuclear Matter</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs064.html#the-code" style="font-size: 80%;"><b>The code</b></a></li>
<!-- navigation toc: --> <li><a href="._week34-bs065.html#splitting-our-data-in-training-and-test-data" style="font-size: 80%;"><b>Splitting our Data in Training and Test data</b></a></li>
<!-- navigation toc: --> <li><a href="#exercises-for-week-36" style="font-size: 80%;"><b>Exercises for week 36</b></a></li>
<!-- navigation toc: --> <li><a href="#exercise-1-setting-up-various-python-environments" style="font-size: 80%;"><b>Exercise 1: Setting up various Python environments</b></a></li>
<!-- navigation toc: --> <li><a href="#exercise-2-making-your-own-data-and-exploring-scikit-learn" style="font-size: 80%;"><b>Exercise 2: making your own data and exploring scikit-learn</b></a></li>
<!-- navigation toc: --> <li><a href="#exercise-3-normalizing-our-data" style="font-size: 80%;"><b>Exercise 3: Normalizing our data</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0066"></a>
<!-- !split -->
<h2 id="exercises-for-week-36" class="anchor">Exercises for week 36 </h2>
Here are three possible exercises for week 36 and the lab sessions of Wednesday September 1..
<p>
<!-- --- begin exercise --- -->
<h2 id="exercise-1-setting-up-various-python-environments" class="anchor">Exercise 1: Setting up various Python environments </h2>
<p>
The first exercise here is of a mere technical art. We want you to have
<ul>
<li> git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo <a href="https://www.uio.no/tjenester/it/maskin/filer/versjonskontroll/github.html" target="_self">GitHub facilities</a>.</li>
<li> Install various Python packages</li>
</ul>
We will make extensive use of Python as programming language and its
myriad of available libraries. You will find
IPython/Jupyter notebooks invaluable in your work. You can run <b>R</b>
codes in the Jupyter/IPython notebooks, with the immediate benefit of
visualizing your data. You can also use compiled languages like C++,
Rust, Fortran etc if you prefer. The focus in these lectures will be
on Python.
<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
<ol>
<li> pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow</li>
</ol>
For <b>Tensorflow</b>, we recommend following the instructions in the text of
<a href="http://shop.oreilly.com/product/0636920052289.do" target="_self">Aurelien Geron, Hands&#8209;On Machine Learning with Scikit&#8209;Learn and TensorFlow, O'Reilly</a>
<p>
We will come back to <b>tensorflow</b> later.
<p>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>
For OSX users we recommend, after having installed Xcode, to
install <b>brew</b>. Brew allows for a seamless installation of additional
software via for example
<ol>
<li> brew install python3</li>
</ol>
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
<ol>
<li> sudo apt-get install python3 (or python for Python2.7)</li>
</ol>
If you don't want to perform these operations separately and venture
into the hassle of exploring how to set up dependencies and paths, we
recommend two widely used distrubutions which set up all relevant
dependencies for Python, namely
<ul>
<li> <a href="https://docs.anaconda.com/" target="_self">Anaconda</a>,</li>
</ul>
which 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>.
<ul>
<li> <a href="https://www.enthought.com/product/canopy/" target="_self">Enthought canopy</a></li>
</ul>
is a Python
distribution for scientific and analytic computing distribution and
analysis environment, available for free and under a commercial
license.
<p>
We recommend using <b>Anaconda</b> if you are not too familiar with setting paths in a terminal environment.
<p>
<!-- --- end exercise --- -->
<p>
<!-- --- begin exercise --- -->
<h2 id="exercise-2-making-your-own-data-and-exploring-scikit-learn" class="anchor">Exercise 2: making your own data and exploring scikit-learn </h2>
<p>
We will generate our own dataset for a function \( y(x) \) where \( x \in [0,1] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \).
The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points).
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">2.0+5*</span>x<span style="color: #666666">*</span>x<span style="color: #666666">+0.1*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
</pre></div>
<ol>
<li> Write your own code (following the examples under the <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter1.html" target="_self">regression notes</a>) for computing the parametrization of the data set fitting a second-order polynomial.</li>
<li> Use thereafter <b>scikit-learn</b> (see again the examples in the regression slides) and compare with your own code.</li>
<li> Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as</li>
</ol>
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
$$
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
$$
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},
$$
where we have defined the mean value of \( \boldsymbol{y} \) as
$$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions.
Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits.
<p>
<!-- --- begin solution of exercise --- -->
<p>
<a class="glyphicon glyphicon-hand-right showdetails" data-toggle="collapse"
data-target="#exer_2_1" style="font-size: 80%;"></a>
<a href="#exer_2_1" data-toggle="collapse">
<b>Solution.</b>
</a>
<div class="collapse-group">
<p><div class="collapse" id="exer_2_1">
<p>
The code here is an example of where we define our own design matrix and fit parameters \( \beta \).
<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">os</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">&quot;.png&quot;</span>, <span style="color: #008000">format</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;png&#39;</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">R2</span>(y_data, y_model):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #666666">1</span> <span style="color: #666666">-</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> y_model) <span style="color: #666666">**</span> <span style="color: #666666">2</span>) <span style="color: #666666">/</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_data)) <span style="color: #666666">**</span> <span style="color: #666666">2</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">MSE</span>(y_data,y_model):
n <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(y_model)
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>sum((y_data<span style="color: #666666">-</span>y_model)<span style="color: #666666">**2</span>)<span style="color: #666666">/</span>n
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">2.0+5*</span>x<span style="color: #666666">*</span>x<span style="color: #666666">+0.1*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>)
<span style="color: #408080; font-style: italic"># The design matrix now as function of a given polynomial</span>
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(x),<span style="color: #666666">3</span>))
X[:,<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <span style="color: #666666">1.0</span>
X[:,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> x
X[:,<span style="color: #666666">2</span>] <span style="color: #666666">=</span> x<span style="color: #666666">**2</span>
<span style="color: #408080; font-style: italic"># We split the data in test and training data</span>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>)
<span style="color: #408080; font-style: italic"># matrix inversion to find beta</span>
beta <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>inv(X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> X_train) <span style="color: #666666">@</span> X_train<span style="color: #666666">.</span>T <span style="color: #666666">@</span> y_train
<span style="color: #008000">print</span>(beta)
<span style="color: #408080; font-style: italic"># and then make the prediction</span>
ytilde <span style="color: #666666">=</span> X_train <span style="color: #666666">@</span> beta
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Training R2&quot;</span>)
<span style="color: #008000">print</span>(R2(y_train,ytilde))
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Training MSE&quot;</span>)
<span style="color: #008000">print</span>(MSE(y_train,ytilde))
ypredict <span style="color: #666666">=</span> X_test <span style="color: #666666">@</span> beta
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test R2&quot;</span>)
<span style="color: #008000">print</span>(R2(y_test,ypredict))
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test MSE&quot;</span>)
<span style="color: #008000">print</span>(MSE(y_test,ypredict))
</pre></div>
<p>
</div></p>
</div>
</p>
<p>
<!-- --- end solution of exercise --- -->
<p>
<!-- --- end exercise --- -->
<p>
<!-- --- begin exercise --- -->
<h2 id="exercise-3-normalizing-our-data" class="anchor">Exercise 3: Normalizing our data </h2>
<p>
A much used approach before starting to train the data is to preprocess our
data. Normally the data may need a rescaling and/or may be sensitive
to extreme values. Scaling the data renders our inputs much more
suitable for the algorithms we want to employ.
<p>
<b>Scikit-Learn</b> has several functions which allow us to rescale the
data, normally resulting in much better results in terms of various
accuracy scores. The <b>StandardScaler</b> function in <b>Scikit-Learn</b>
ensures that for each feature/predictor we study the mean value is
zero and the variance is one (every column in the design/feature
matrix). This scaling has the drawback that it does not ensure that
we have a particular maximum or minimum in our data set. Another
function included in <b>Scikit-Learn</b> is the <b>MinMaxScaler</b> which
ensures that all features are exactly between \( 0 \) and \( 1 \). The
<p>
The <b>Normalizer</b> scales each data
point such that the feature vector has a euclidean length of one. In other words, it
projects a data point on the circle (or sphere in the case of higher dimensions) with a
radius of 1. This means every data point is scaled by a different number (by the
inverse of it&#8217;s length).
This normalization is often used when only the direction (or angle) of the data matters,
not the length of the feature vector.
<p>
The <b>RobustScaler</b> works similarly to the StandardScaler in that it
ensures statistical properties for each feature that guarantee that
they are on the same scale. However, the RobustScaler uses the median
and quartiles, instead of mean and variance. This makes the
RobustScaler ignore data points that are very different from the rest
(like measurement errors). These odd data points are also called
outliers, and might often lead to trouble for other scaling
techniques.
<p>
It also common to split the data in a <b>training</b> set and a <b>testing</b> set. A typical split is to use \( 80\% \) of the data for training and the rest
for testing. This can be done as follows with our design matrix \( \boldsymbol{X} \) and data \( \boldsymbol{y} \) (remember to import <b>scikit-learn</b>)
<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: #408080; font-style: italic"># split in training and test data</span>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X,y,test_size<span style="color: #666666">=0.2</span>)
</pre></div>
<p>
Then we can use the standard scaler to scale our data as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
</pre></div>
<p>
In this exercise we want you to to compute the MSE for the training
data and the test data as function of the complexity of a polynomial,
that is the degree of a given polynomial. We want you also to compute the \( R2 \) score as function of the complexity of the model for both training data and test data. You should also run the calculation with and without scaling.
<p>
One of
the aims is to reproduce Figure 2.11 of <a href="https://github.com/CompPhysics/MLErasmus/blob/master/doc/Textbooks/elementsstat.pdf" target="_self">Hastie et al</a>.
<p>
Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed()
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">14</span>
<span style="color: #408080; font-style: italic"># Make data set.</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(<span style="color: #666666">0</span>, <span style="color: #666666">0.1</span>, x<span style="color: #666666">.</span>shape)
</pre></div>
<p>
where \( y \) is the function we want to fit with a given polynomial.
<p>
<b>a)</b>
Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data.
<p>
<b>b)</b>
Perform an ordinary least squares and compute the means squared error and the \( R2 \) factor for the training data and the test data, with and without scaling.
<p>
<b>c)</b>
Add now a model which allows you to make polynomials up to degree \( 15 \). Perform a standard OLS fitting of the training data and compute the MSE and \( R2 \) for the training and test data and plot both test and training data MSE and \( R2 \) as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)?
<p>
<!-- --- end exercise --- -->
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._week34-bs065.html">&laquo;</a></li>
<li><a href="._week34-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._week34-bs058.html">59</a></li>
<li><a href="._week34-bs059.html">60</a></li>
<li><a href="._week34-bs060.html">61</a></li>
<li><a href="._week34-bs061.html">62</a></li>
<li><a href="._week34-bs062.html">63</a></li>
<li><a href="._week34-bs063.html">64</a></li>
<li><a href="._week34-bs064.html">65</a></li>
<li><a href="._week34-bs065.html">66</a></li>
<li class="active"><a href="._week34-bs066.html">67</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>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>