update on machine learning
This commit is contained in:
@@ -71,45 +71,39 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
('Software and needed installations', 2, None, '___sec3'),
|
||||
('Python installers', 2, None, '___sec4'),
|
||||
('Installing R, C++, cython or Julia', 2, None, '___sec5'),
|
||||
('Installing R, C++, cython or Julia', 2, None, '___sec6'),
|
||||
('Introduction to Jupyter notebook and available tools',
|
||||
2,
|
||||
None,
|
||||
'___sec6'),
|
||||
('Doing it in R', 2, None, '___sec7'),
|
||||
('Representing data, overarching aims', 2, None, '___sec8'),
|
||||
('Representing data, more examples', 2, None, '___sec9'),
|
||||
('Binning of data', 2, None, '___sec10'),
|
||||
('Adding model complexity: Predator-Prey model from ecology',
|
||||
2,
|
||||
None,
|
||||
'___sec11'),
|
||||
('Case study from Hudson bay', 2, None, '___sec12'),
|
||||
('Hudson bay data', 2, None, '___sec13'),
|
||||
('Plotting the data', 2, None, '___sec14'),
|
||||
'___sec7'),
|
||||
('Representing data, more examples', 2, None, '___sec8'),
|
||||
('Predator-Prey model from ecology', 2, None, '___sec9'),
|
||||
('Case study from Hudson bay', 2, None, '___sec10'),
|
||||
('Hudson bay data', 2, None, '___sec11'),
|
||||
('Plotting the data', 2, None, '___sec12'),
|
||||
('Hares and lynx in Hudson bay from 1900 to 1920',
|
||||
2,
|
||||
None,
|
||||
'___sec15'),
|
||||
'___sec13'),
|
||||
('Why now create a computer model for the hare and lynx '
|
||||
'populations?',
|
||||
2,
|
||||
None,
|
||||
'___sec16'),
|
||||
('The traditional (top-down) approach', 2, None, '___sec17'),
|
||||
("The ``new'' discrete bottom-up approach", 2, None, '___sec18'),
|
||||
('Basic (computer-friendly) mathematics notation',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
'___sec14'),
|
||||
('The traditional (top-down) approach', 2, None, '___sec15'),
|
||||
('Basic mathematics notation', 2, None, '___sec16'),
|
||||
('Basic dynamics of the population of hares',
|
||||
2,
|
||||
None,
|
||||
'___sec20'),
|
||||
('Basic dynamics of the population of lynx', 2, None, '___sec21'),
|
||||
('Evolution equations', 2, None, '___sec22'),
|
||||
('Adapt the model to the Hudson Bay case', 2, None, '___sec23'),
|
||||
('The program', 2, None, '___sec24'),
|
||||
('The plot', 2, None, '___sec25')]}
|
||||
'___sec17'),
|
||||
('Basic dynamics of the population of lynx', 2, None, '___sec18'),
|
||||
('Evolution equations', 2, None, '___sec19'),
|
||||
('Adapt the model to the Hudson Bay case', 2, None, '___sec20'),
|
||||
('The program', 2, None, '___sec21'),
|
||||
('The plot', 2, None, '___sec22'),
|
||||
('Linear regression in Python', 2, None, '___sec23'),
|
||||
('Linear Least squares in R', 2, None, '___sec24'),
|
||||
('Non-Linear Least squares in R', 2, None, '___sec25')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -151,7 +145,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Dec 1, 2017</h4></center> <!-- date -->
|
||||
<center><h4>Dec 6, 2017</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -263,8 +257,8 @@ If you don't want to perform these operations separately, we recommend two widel
|
||||
all relevant dependencies for Python, namely
|
||||
|
||||
<ol>
|
||||
<li> anaconda</li>
|
||||
<li> Enthought canopy</li>
|
||||
<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>
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -272,10 +266,18 @@ all relevant dependencies for Python, namely
|
||||
<h2 id="___sec5">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
You will also find it convenient to utilize R. Say more about R.
|
||||
You will also find it convenient to utilize R.
|
||||
Jupyter/Ipython notebook allows you run <b>R</b> code interactively in your browser. The software library <b>R</b> is
|
||||
tuned to statistically analysis and allows for an easy usage of the tools we will discuss in these texts.
|
||||
|
||||
<p>
|
||||
To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">following the link here</a>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
For the C++ affecianodas, Jupyter/IPython notebook allows you also to install C++ and run codes written in this language
|
||||
interactively in the browser. Since we will emphasize writing many of the algorithms yourself, you can thus opt for
|
||||
@@ -288,27 +290,16 @@ including symbolic operations, Python offers a unique computational environment.
|
||||
can easily be converted into a nicely rendered <b>PDF</b> file or a Latex file for further processing.
|
||||
|
||||
<p>
|
||||
This never ends,
|
||||
|
||||
<p>
|
||||
If you se the light mark-up language <b>doconce</b> you can convert a standard ascii text file into various HTML
|
||||
This never ends.
|
||||
If you use the light mark-up language <b>doconce</b> you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Introduction to Jupyter notebook and available tools </h2>
|
||||
<h2 id="___sec7">Introduction to Jupyter notebook and available tools </h2>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Doing it in R </h2>
|
||||
Use the Hudson bay model to load data and make plot in R and with python
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Representing data, overarching aims </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -328,7 +319,7 @@ x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>l
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sin(x)
|
||||
plt<span style="color: #666666">.</span>plot(x,y,marker<span style="color: #666666">=</span><span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
data <span style="color: #666666">=</span> {<span style="color: #BA2121">'Name'</span>: [<span style="color: #BA2121">"John"</span>, <span style="color: #BA2121">"Anna"</span>, <span style="color: #BA2121">"Peter"</span>, <span style="color: #BA2121">"Linda"</span>], <span style="color: #BA2121">'Location'</span>: [<span style="color: #BA2121">"Roma"</span>, <span style="color: #BA2121">"Napoli"</span>, <span style="color: #BA2121">"Torino"</span>, <span style="color: #BA2121">"Milano"</span>], <span style="color: #BA2121">'Age'</span>:[<span style="color: #666666">51</span>, <span style="color: #666666">21</span>, <span style="color: #666666">34</span>, <span style="color: #666666">45</span>]}
|
||||
data <span style="color: #666666">=</span> {<span style="color: #BA2121">'Name'</span>: [<span style="color: #BA2121">"John"</span>, <span style="color: #BA2121">"Anna"</span>, <span style="color: #BA2121">"Peter"</span>, <span style="color: #BA2121">"Linda"</span>], <span style="color: #BA2121">'Location'</span>: [<span style="color: #BA2121">"Nairobi"</span>, <span style="color: #BA2121">"Napoli"</span>, <span style="color: #BA2121">"London"</span>, <span style="color: #BA2121">"Buenos Aires"</span>], <span style="color: #BA2121">'Age'</span>:[<span style="color: #666666">51</span>, <span style="color: #666666">21</span>, <span style="color: #666666">34</span>, <span style="color: #666666">45</span>]}
|
||||
data_pandas <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(data)
|
||||
display(data_pandas)
|
||||
</pre></div>
|
||||
@@ -339,7 +330,7 @@ display(data_pandas)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec9">Representing data, more examples </h2>
|
||||
<h2 id="___sec8">Representing data, more examples </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -370,13 +361,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Binning of data </h2>
|
||||
Use the Hudson bay model to load data and make plot in R and with python
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Adding model complexity: Predator-Prey model from ecology </h2>
|
||||
<h2 id="___sec9">Predator-Prey model from ecology </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -400,14 +385,14 @@ scientific method:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec12">Case study from Hudson bay </h2>
|
||||
<h2 id="___sec10">Case study from Hudson bay </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why?
|
||||
We shall demonstrate the scientific method by
|
||||
Here we start by
|
||||
|
||||
<ol>
|
||||
<li> plotting the data</li>
|
||||
@@ -421,18 +406,18 @@ We shall demonstrate the scientific method by
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec13">Hudson bay data </h2>
|
||||
<h2 id="___sec11">Hudson bay data </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
% if FORMAT == 'ipynb':
|
||||
|
||||
<p>
|
||||
Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle.
|
||||
|
||||
<p>
|
||||
One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920.
|
||||
% endif
|
||||
|
||||
<p>
|
||||
<table border="1">
|
||||
@@ -470,7 +455,7 @@ One reason that this particular system has been so extensively studied is that t
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec14">Plotting the data </h2>
|
||||
<h2 id="___sec12">Plotting the data </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -483,7 +468,7 @@ One reason that this particular system has been so extensively studied is that t
|
||||
<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> pyplot <span style="color: #008000; font-weight: bold">as</span> plt
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Load in data file</span>
|
||||
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">'Hudson_Bay.dat'</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>, skiprows<span style="color: #666666">=1</span>)
|
||||
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">'src/Hudson_Bay.csv'</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>, skiprows<span style="color: #666666">=1</span>)
|
||||
<span style="color: #408080; font-style: italic"># Make arrays containing x-axis and hares and lynx populations</span>
|
||||
year <span style="color: #666666">=</span> data[:,<span style="color: #666666">0</span>]
|
||||
hares <span style="color: #666666">=</span> data[:,<span style="color: #666666">1</span>]
|
||||
@@ -504,23 +489,20 @@ plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
<p>
|
||||
% if FORMAT != 'ipynb':
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
|
||||
<h2 id="___sec13">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
|
||||
|
||||
<p>
|
||||
<br /><br /><center><p><img src="fig/Hudson_Bay_data.png" align="bottom" width=700></p></center><br /><br />
|
||||
% endif
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Why now create a computer model for the hare and lynx populations? </h2>
|
||||
<h2 id="___sec14">Why now create a computer model for the hare and lynx populations? </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
% if FORMAT == 'ipynb':
|
||||
We see from the plot that there are indeed fluctuations.
|
||||
We would like to create a mathematical model that explains these
|
||||
population fluctuations. Ecologists have predicted that in a simple
|
||||
@@ -535,7 +517,6 @@ this? What causes cycles to slow or speed up? What affects the
|
||||
amplitude of the oscillation or do you expect to see the oscillations
|
||||
damp to a stable equilibrium? The models tend to ignore factors like
|
||||
climate and other complicating factors. How significant are these?
|
||||
% else:
|
||||
|
||||
<ul>
|
||||
<li> We see oscillations in the data</li>
|
||||
@@ -545,15 +526,13 @@ climate and other complicating factors. How significant are these?
|
||||
<li> More important: we can understand the ecology dynamics of
|
||||
predator-pray populations</li>
|
||||
</ul>
|
||||
|
||||
% endif
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">The traditional (top-down) approach </h2>
|
||||
<h2 id="___sec15">The traditional (top-down) approach </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -586,32 +565,7 @@ ODEs</em> (which cannot be solved)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">The "new" discrete bottom-up approach </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b>The bottom-up approach.</b>
|
||||
<p>
|
||||
<!-- !bpop -->
|
||||
|
||||
<ul>
|
||||
<li> Start with experimental data and discuss the methods which have been used to collect the data, the assumptions, the electronic devices, the aims etc. That is, expose the students to the theory and assumptions behind the data that have been collected and motivate for the scientific method.</li>
|
||||
<li> Where appropriate the students should do the experiment(s) needed to collect the data.</li>
|
||||
<li> The first programming tasks are to read and visualize the data to see if there are patterns or regularities. This strengthens a research-driven intuition.</li>
|
||||
<li> Now we want to increase the understanding through modeling.</li>
|
||||
<li> Most of the biology lies in the <em>derivation</em> of the model. We shall
|
||||
focus on an intuitive discrete approach that leads to difference
|
||||
equations that can be programmed <em>and solved</em> directly.</li>
|
||||
</ul>
|
||||
|
||||
<!-- !epop -->
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">Basic (computer-friendly) mathematics notation </h2>
|
||||
<h2 id="___sec16">Basic mathematics notation </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -631,7 +585,7 @@ ODEs</em> (which cannot be solved)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Basic dynamics of the population of hares </h2>
|
||||
<h2 id="___sec17">Basic dynamics of the population of hares </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -650,8 +604,7 @@ All the hares and lynx can form
|
||||
interval \( \Delta t \), there is some
|
||||
small probablity that the lynx will eat the hare.
|
||||
So in fraction \( b\Delta t HL \), the lynx eat hares. This
|
||||
loss of hares and must be accounted for:
|
||||
subtracted in the equation for hares:
|
||||
loss of hares must be accounted for. Subtracted in the equation for hares:
|
||||
|
||||
$$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
|
||||
</div>
|
||||
@@ -660,7 +613,7 @@ $$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec21">Basic dynamics of the population of lynx </h2>
|
||||
<h2 id="___sec18">Basic dynamics of the population of lynx </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -689,7 +642,7 @@ $$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Evolution equations </h2>
|
||||
<h2 id="___sec19">Evolution equations </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -718,7 +671,7 @@ Note:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">Adapt the model to the Hudson Bay case </h2>
|
||||
<h2 id="___sec20">Adapt the model to the Hudson Bay case </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -741,7 +694,7 @@ Note:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">The program </h2>
|
||||
<h2 id="___sec21">The program </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -762,7 +715,7 @@ Note:
|
||||
H <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(t<span style="color: #666666">.</span>size)
|
||||
L <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(t<span style="color: #666666">.</span>size)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">'Init:'</span>, H0, L0, dt
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Init:'</span>, H0, L0, dt)
|
||||
H[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> H0
|
||||
L[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> L0
|
||||
|
||||
@@ -772,7 +725,7 @@ Note:
|
||||
<span style="color: #008000; font-weight: bold">return</span> H, L, t
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Load in data file</span>
|
||||
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">'Hudson_Bay.csv'</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>, skiprows<span style="color: #666666">=1</span>)
|
||||
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">'src/Hudson_Bay.csv'</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>, skiprows<span style="color: #666666">=1</span>)
|
||||
<span style="color: #408080; font-style: italic"># Make arrays containing x-axis and hares and lynx populations</span>
|
||||
t_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">0</span>]
|
||||
H_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">1</span>]
|
||||
@@ -799,18 +752,116 @@ plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
<p>
|
||||
% if FORMAT != 'ipynb':
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">The plot </h2>
|
||||
<h2 id="___sec22">The plot </h2>
|
||||
|
||||
<p>
|
||||
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
|
||||
|
||||
<p>
|
||||
% else:
|
||||
If we perform a least-square fitting, we can find optimal values for the parameters \( a \), \( b \), \( d \), \( c \). The optimal parameters are \( a=0.4807 \), \( b=0.02482 \), \( d=0.9272 \) and \( c=0.02756 \). These parameters result in a slightly modified initial conditions, namely \( H(0) = 34.91 \) and \( L(0)=3.857 \). With these parameters we are now ready to solve the equations and plot these data together with the experimental values.
|
||||
% endif
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">Linear regression in Python </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<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>
|
||||
<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">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">sklearn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
|
||||
|
||||
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">'src/Hudson_Bay.csv'</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>, skiprows<span style="color: #666666">=1</span>)
|
||||
x <span style="color: #666666">=</span> data[:,<span style="color: #666666">0</span>]
|
||||
y <span style="color: #666666">=</span> data[:,<span style="color: #666666">1</span>]
|
||||
line <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">1900</span>,<span style="color: #666666">1920</span>,<span style="color: #666666">1000</span>,endpoint<span style="color: #666666">=</span><span style="color: #008000">False</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>)
|
||||
reg <span style="color: #666666">=</span> DecisionTreeRegressor(min_samples_split<span style="color: #666666">=3</span>)<span style="color: #666666">.</span>fit(x<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>),y<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>))
|
||||
plt<span style="color: #666666">.</span>plot(line, reg<span style="color: #666666">.</span>predict(line), label<span style="color: #666666">=</span><span style="color: #BA2121">"decision tree"</span>)
|
||||
regline <span style="color: #666666">=</span> LinearRegression()<span style="color: #666666">.</span>fit(x<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>),y<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>))
|
||||
plt<span style="color: #666666">.</span>plot(line, regline<span style="color: #666666">.</span>predict(line), label<span style="color: #666666">=</span> <span style="color: #BA2121">"Linear Regression"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x, y, label<span style="color: #666666">=</span> <span style="color: #BA2121">"Linear Regression"</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>HudsonBay <span style="color: #666666">=</span> read<span style="color: #666666">.</span>csv(<span style="color: #BA2121">"src/Hudson_Bay.csv"</span>,header<span style="color: #666666">=</span>T)
|
||||
fix(HudsonBay)
|
||||
dim(HudsonBay)
|
||||
names(HudsonBay)
|
||||
plot(HudsonBay$Year, HudsonBay$Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
|
||||
attach(HudsonBay)
|
||||
plot(Year, Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
|
||||
plot(Year, Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>, col<span style="color: #666666">=</span><span style="color: #BA2121">"red"</span>, varwidth<span style="color: #666666">=</span>T, xlab<span style="color: #666666">=</span><span style="color: #BA2121">"Years"</span>, ylab<span style="color: #666666">=</span><span style="color: #BA2121">"Haresx 1000"</span>)
|
||||
summary(HudsonBay)
|
||||
summary(Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
|
||||
library(MASS)
|
||||
library(ISLR)
|
||||
scatter<span style="color: #666666">.</span>smooth(x<span style="color: #666666">=</span>Year, y <span style="color: #666666">=</span> Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
|
||||
linearMod <span style="color: #666666">=</span> lm(Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span> <span style="color: #666666">~</span> Year)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(linearMod)
|
||||
summary(linearMod)
|
||||
plot(linearMod)
|
||||
confint(linearMod)
|
||||
predict(linearMod,data<span style="color: #666666">.</span>frame(Year<span style="color: #666666">=</span>c(<span style="color: #666666">1910</span>,<span style="color: #666666">1914</span>,<span style="color: #666666">1920</span>)),interval<span style="color: #666666">=</span><span style="color: #BA2121">"confidence"</span>)
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<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">set</span><span style="color: #666666">.</span>seed(<span style="color: #666666">1485</span>)
|
||||
<span style="color: #008000">len</span> <span style="color: #666666">=</span> <span style="color: #666666">24</span>
|
||||
x <span style="color: #666666">=</span> runif(<span style="color: #008000">len</span>)
|
||||
y <span style="color: #666666">=</span> x<span style="color: #666666">^3+</span>rnorm(<span style="color: #008000">len</span>, <span style="color: #666666">0</span>,<span style="color: #666666">0.06</span>)
|
||||
ds <span style="color: #666666">=</span> data<span style="color: #666666">.</span>frame(x <span style="color: #666666">=</span> x, y <span style="color: #666666">=</span> y)
|
||||
<span style="color: #008000">str</span>(ds)
|
||||
plot( y <span style="color: #666666">~</span> x, main <span style="color: #666666">=</span><span style="color: #BA2121">"Known cubic with noise"</span>)
|
||||
s <span style="color: #666666">=</span> seq(<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,length <span style="color: #666666">=100</span>)
|
||||
lines(s, s<span style="color: #666666">^3</span>, lty <span style="color: #666666">=2</span>, col <span style="color: #666666">=</span><span style="color: #BA2121">"green"</span>)
|
||||
m <span style="color: #666666">=</span> nls(y <span style="color: #666666">~</span> I(x<span style="color: #666666">^</span>power), data <span style="color: #666666">=</span> ds, start <span style="color: #666666">=</span> <span style="color: #008000">list</span>(power<span style="color: #666666">=1</span>), trace <span style="color: #666666">=</span> T)
|
||||
class(m)
|
||||
summary(m)
|
||||
power <span style="color: #666666">=</span> <span style="color: #008000">round</span>(summary(m)$coefficients[<span style="color: #666666">1</span>], <span style="color: #666666">3</span>)
|
||||
power<span style="color: #666666">.</span>se <span style="color: #666666">=</span> <span style="color: #008000">round</span>(summary(m)$coefficients[<span style="color: #666666">2</span>], <span style="color: #666666">3</span>)
|
||||
plot(y <span style="color: #666666">~</span> x, main <span style="color: #666666">=</span> <span style="color: #BA2121">"Fitted power model"</span>, sub <span style="color: #666666">=</span> <span style="color: #BA2121">"Blue: fit; green: known"</span>)
|
||||
s <span style="color: #666666">=</span> seq(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, length <span style="color: #666666">=</span> <span style="color: #666666">100</span>)
|
||||
lines(s, s<span style="color: #666666">^3</span>, lty <span style="color: #666666">=</span> <span style="color: #666666">2</span>, col <span style="color: #666666">=</span> <span style="color: #BA2121">"green"</span>)
|
||||
lines(s, predict(m, <span style="color: #008000">list</span>(x <span style="color: #666666">=</span> s)), lty <span style="color: #666666">=</span> <span style="color: #666666">1</span>, col <span style="color: #666666">=</span> <span style="color: #BA2121">"blue"</span>)
|
||||
text(<span style="color: #666666">0</span>, <span style="color: #666666">0.5</span>, paste(<span style="color: #BA2121">"y =x^ ("</span>, power, <span style="color: #BA2121">" +/- "</span>, power<span style="color: #666666">.</span>se, <span style="color: #BA2121">")"</span>, sep <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>), pos <span style="color: #666666">=</span> <span style="color: #666666">4</span>)
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user