Files
FYS-STK4155/doc/pub/How2ReadData/html/._How2ReadData-bs022.html
T
2017-12-06 14:13:28 +01:00

263 lines
16 KiB
HTML

<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Introduction and Representing data">
<title>Data Analysis and Machine Learning: Introduction and Representing data</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': [('What is Machine Learning?', 2, None, '___sec0'),
('Types of Machine Learning', 2, None, '___sec1'),
('Different algorithms', 2, None, '___sec2'),
('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,
'___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,
'___sec13'),
('Why now create a computer model for the hare and lynx '
'populations?',
2,
None,
'___sec14'),
('The traditional (top-down) approach', 2, None, '___sec15'),
('Basic mathematics notation', 2, None, '___sec16'),
('Basic dynamics of the population of hares',
2,
None,
'___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>
<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="How2ReadData-bs.html">Data Analysis and Machine Learning: Introduction and Representing data</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="._How2ReadData-bs001.html#___sec0" style="font-size: 80%;">What is Machine Learning?</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs002.html#___sec1" style="font-size: 80%;">Types of Machine Learning</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs003.html#___sec2" style="font-size: 80%;">Different algorithms</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs004.html#___sec3" style="font-size: 80%;">Software and needed installations</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs005.html#___sec4" style="font-size: 80%;">Python installers</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs006.html#___sec5" style="font-size: 80%;">Installing R, C++, cython or Julia</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs007.html#___sec6" style="font-size: 80%;">Installing R, C++, cython or Julia</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs008.html#___sec7" style="font-size: 80%;">Introduction to Jupyter notebook and available tools</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs009.html#___sec8" style="font-size: 80%;">Representing data, more examples</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs010.html#___sec9" style="font-size: 80%;">Predator-Prey model from ecology</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs011.html#___sec10" style="font-size: 80%;">Case study from Hudson bay</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs012.html#___sec11" style="font-size: 80%;">Hudson bay data</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs013.html#___sec12" style="font-size: 80%;">Plotting the data</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs014.html#___sec13" style="font-size: 80%;">Hares and lynx in Hudson bay from 1900 to 1920</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs015.html#___sec14" style="font-size: 80%;">Why now create a computer model for the hare and lynx populations?</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs016.html#___sec15" style="font-size: 80%;">The traditional (top-down) approach</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs017.html#___sec16" style="font-size: 80%;">Basic mathematics notation</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs018.html#___sec17" style="font-size: 80%;">Basic dynamics of the population of hares</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs019.html#___sec18" style="font-size: 80%;">Basic dynamics of the population of lynx</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs020.html#___sec19" style="font-size: 80%;">Evolution equations</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs021.html#___sec20" style="font-size: 80%;">Adapt the model to the Hudson Bay case</a></li>
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">The program</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs023.html#___sec22" style="font-size: 80%;">The plot</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs024.html#___sec23" style="font-size: 80%;">Linear regression in Python</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs025.html#___sec24" style="font-size: 80%;">Linear Least squares in R</a></li>
<!-- navigation toc: --> <li><a href="._How2ReadData-bs026.html#___sec25" style="font-size: 80%;">Non-Linear Least squares in R</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="part0022"></a>
<!-- !split -->
<h2 id="___sec21" class="anchor">The program </h2>
<p>
<div class="panel panel-default">
<div class="panel-body">
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<p>
<!-- code=python (!bc pypro) 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">def</span> <span style="color: #0000FF">solver</span>(m, H0, L0, dt, a, b, c, d, t0):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Solve the difference equations for H and L over m years</span>
<span style="color: #BA2121; font-style: italic"> with time step dt (measured in years.&quot;&quot;&quot;</span>
num_intervals <span style="color: #666666">=</span> <span style="color: #008000">int</span>(m<span style="color: #666666">/</span><span style="color: #008000">float</span>(dt))
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(t0, t0 <span style="color: #666666">+</span> m, num_intervals<span style="color: #666666">+1</span>)
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">&#39;Init:&#39;</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
<span style="color: #008000; font-weight: bold">for</span> n <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>, <span style="color: #008000">len</span>(t)<span style="color: #666666">-1</span>):
H[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> H[n] <span style="color: #666666">+</span> a<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>H[n] <span style="color: #666666">-</span> b<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>H[n]<span style="color: #666666">*</span>L[n]
L[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> L[n] <span style="color: #666666">+</span> d<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>H[n]<span style="color: #666666">*</span>L[n] <span style="color: #666666">-</span> c<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>L[n]
<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">&#39;src/Hudson_Bay.csv&#39;</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">&#39;,&#39;</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>]
L_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">2</span>]
<span style="color: #408080; font-style: italic"># Simulate using the model</span>
H, L, t <span style="color: #666666">=</span> solver(m<span style="color: #666666">=20</span>, H0<span style="color: #666666">=34.91</span>, L0<span style="color: #666666">=3.857</span>, dt<span style="color: #666666">=0.1</span>,
a<span style="color: #666666">=0.4807</span>, b<span style="color: #666666">=0.02482</span>, c<span style="color: #666666">=0.9272</span>, d<span style="color: #666666">=0.02756</span>,
t0<span style="color: #666666">=1900</span>)
<span style="color: #408080; font-style: italic"># Visualize simulations and data</span>
plt<span style="color: #666666">.</span>plot(t_e, H_e, <span style="color: #BA2121">&#39;b-+&#39;</span>, t_e, L_e, <span style="color: #BA2121">&#39;r-o&#39;</span>, t, H, <span style="color: #BA2121">&#39;m--&#39;</span>, t, L, <span style="color: #BA2121">&#39;k--&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;Year&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Numbers of hares and lynx&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">1900</span>, <span style="color: #666666">1920</span>, <span style="color: #666666">0</span>, <span style="color: #666666">140</span>])
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Population of hares and lynx 1900-1920 (x1000)&#39;</span>)
plt<span style="color: #666666">.</span>legend((<span style="color: #BA2121">&#39;H_e&#39;</span>, <span style="color: #BA2121">&#39;L_e&#39;</span>, <span style="color: #BA2121">&#39;H&#39;</span>, <span style="color: #BA2121">&#39;L&#39;</span>), loc<span style="color: #666666">=</span><span style="color: #BA2121">&#39;upper left&#39;</span>)
plt<span style="color: #666666">.</span>savefig(<span style="color: #BA2121">&#39;Hudson_Bay_sim.pdf&#39;</span>)
plt<span style="color: #666666">.</span>savefig(<span style="color: #BA2121">&#39;Hudson_Bay_sim.png&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
</div>
</div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._How2ReadData-bs021.html">&laquo;</a></li>
<li><a href="._How2ReadData-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._How2ReadData-bs014.html">15</a></li>
<li><a href="._How2ReadData-bs015.html">16</a></li>
<li><a href="._How2ReadData-bs016.html">17</a></li>
<li><a href="._How2ReadData-bs017.html">18</a></li>
<li><a href="._How2ReadData-bs018.html">19</a></li>
<li><a href="._How2ReadData-bs019.html">20</a></li>
<li><a href="._How2ReadData-bs020.html">21</a></li>
<li><a href="._How2ReadData-bs021.html">22</a></li>
<li class="active"><a href="._How2ReadData-bs022.html">23</a></li>
<li><a href="._How2ReadData-bs023.html">24</a></li>
<li><a href="._How2ReadData-bs024.html">25</a></li>
<li><a href="._How2ReadData-bs025.html">26</a></li>
<li><a href="._How2ReadData-bs026.html">27</a></li>
<li><a href="._How2ReadData-bs023.html">&raquo;</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="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>