This commit is contained in:
mhjensen
2018-05-30 07:39:18 -04:00
parent d34d39836b
commit 2a34931899
8 changed files with 84 additions and 98 deletions
+14 -16
View File
@@ -125,7 +125,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>May 29, 2018</h4></center> <!-- date -->
<center><h4>May 30, 2018</h4></center> <!-- date -->
<br>
<h2 id="___sec0">Introduction </h2>
@@ -462,7 +462,7 @@ example of the functionality of scikit-learn.
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
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+</span> <span style="color: #666666">5*</span>x<span style="color: #666666">+0.5</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>)
y <span style="color: #666666">=</span> <span style="color: #666666">2.0+</span> <span style="color: #666666">5*</span>x<span style="color: #666666">+0.5*</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>)
linreg <span style="color: #666666">=</span> LinearRegression()
linreg<span style="color: #666666">.</span>fit(x,y)
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(x)
@@ -575,6 +575,7 @@ plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
Similarly, using <b>R</b>, we can perform similar studies. The following <b>R</b> code illustrates this.
(more details on <b>R</b> will be inserted later).
<h2 id="___sec6">Non-Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
@@ -630,7 +631,7 @@ display(data_pandas)
<p>
We present here several examples, with pertinent Python codes that we
will us to illustrate various machine learning methods and ways to
will use to illustrate various machine learning methods and ways to
analyze, from simple to complex, various data sets. Many of these
examples allow us to generate the data we want to analyze, following
much of the same philosophy we discussed above when
@@ -658,7 +659,7 @@ Here we will construct a model for cell growth based on a simple difference equa
interval \( \Delta t \) as \( N \) cells would: \( \Delta N \propto N \)</li>
<li> \( N \) cells result in twice as many new individuals \( \Delta N \) in
time \( 2\Delta t \) as in time \( \Delta t \): \( \Delta N \propto\Delta t \)</li>
<li> Same proportionality wrt death</li>
<li> Same proportionality with respect to death</li>
<li> Proposed model: \( \Delta N = b\Delta t N - d\Delta tN \) for some unknown
constants \( b \) (births) and \( d \) (deaths)</li>
<li> Describe evolution in discrete time: \( t_n=n\Delta t \)</li>
@@ -670,7 +671,7 @@ Here we will construct a model for cell growth based on a simple difference equa
<p>
The difference equation can be programmed in a simple was, and in order to get started we
The difference equation can be programmed in a simple way, and in order to get started we
set \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \). The program reads
<p>
@@ -681,13 +682,12 @@ set \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \). The program reads
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">10</span>, <span style="color: #666666">21</span>) <span style="color: #408080; font-style: italic"># 20 intervals in [0, 10]</span>
dt <span style="color: #666666">=</span> t[<span style="color: #666666">1</span>] <span style="color: #666666">-</span> t[<span style="color: #666666">0</span>]
N <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(t<span style="color: #666666">.</span>size)
N[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <span style="color: #666666">1</span>
r <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>
<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>, N<span style="color: #666666">.</span>size<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>):
N[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> N[n] <span style="color: #666666">+</span> r<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>N[n]
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">&#39;N[</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">]=</span><span style="color: #BB6688; font-weight: bold">%.1f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> (n<span style="color: #666666">+1</span>, N[n<span style="color: #666666">+1</span>])
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;N[</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">]=</span><span style="color: #BB6688; font-weight: bold">%.1f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> (n<span style="color: #666666">+1</span>, N[n<span style="color: #666666">+1</span>]))
</pre></div>
<p>
and it generates the following output
@@ -718,7 +718,7 @@ N[20]=86.7
<p>
This forms our data which later will define our training set.
In this case we defined the value of the parameter \( r \). We could alternatively assume that we just received the
above data file and where asked to use find \( r \). How can we estimate \( r \) from data?
above data file and where asked to find \( r \). How can we estimate \( r \) from data? This will be one of our tasks later.
<p>
We can use the difference equation with the experimental data
@@ -728,12 +728,13 @@ Suppose now that \( N^{n+1} \) and \( N^n \) are known from data. Then we could
$$ r = \frac{N^{n+1}-N^n}{N^n\Delta t} $$
Suppose we set \( t_1=600 \), \( t_2=1200 \),
\( N^1=140 \) and \( N^2=250 \). We obtain then \( r=0.0013 \). The exact value is \( r = 0.000694 \)
The following code plot
\( N^1=140 \) and \( N^2=250 \).
The following code plots the data
<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: #408080; font-style: italic"># Estimate r</span>
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">&#39;ecoli.csv&#39;</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">&#39;,&#39;</span>)
@@ -741,10 +742,9 @@ t_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">0<
N_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">1</span>]
i <span style="color: #666666">=</span> <span style="color: #666666">2</span> <span style="color: #408080; font-style: italic"># Data point (i,i+1) used to estimate r</span>
r <span style="color: #666666">=</span> (N_e[i<span style="color: #666666">+1</span>] <span style="color: #666666">-</span> N_e[i])<span style="color: #666666">/</span>(N_e[i]<span style="color: #666666">*</span>(t_e[i<span style="color: #666666">+1</span>] <span style="color: #666666">-</span> t_e[i]))
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">&#39;Estimated r=</span><span style="color: #BB6688; font-weight: bold">%.5f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> r
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;Estimated r=</span><span style="color: #BB6688; font-weight: bold">%.5f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> r)
<span style="color: #408080; font-style: italic"># Can experiment with r values and see if the model can</span>
<span style="color: #408080; font-style: italic"># match the data better</span>
T <span style="color: #666666">=</span> <span style="color: #666666">1200</span> <span style="color: #408080; font-style: italic"># cell can divide after T sec</span>
t_max <span style="color: #666666">=</span> <span style="color: #666666">5*</span>T <span style="color: #408080; font-style: italic"># 5 generations in experiment</span>
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, t_max, <span style="color: #666666">1000</span>)
@@ -755,7 +755,6 @@ N[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <
<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>, <span style="color: #666666">1</span>):
N[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> N[n] <span style="color: #666666">+</span> r<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>N[n]
<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>
plt<span style="color: #666666">.</span>plot(t, N, <span style="color: #BA2121">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #BA2121">&#39;bo&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;time [s]&#39;</span>); plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;N&#39;</span>)
plt<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&#39;model&#39;</span>, <span style="color: #BA2121">&#39;experiment&#39;</span>], loc<span style="color: #666666">=</span><span style="color: #BA2121">&#39;upper left&#39;</span>)
@@ -1011,7 +1010,7 @@ parameters result in a slightly modified initial conditions, namely
\( H(0) = 34.91 \) and \( L(0)=3.857 \).
<p>
The following Python demonstrates how we can use linear regression to fit for example the population of lynx.
The following Python code demonstrates how we can use linear regression to fit for example the population of lynx.
Similarly, we have also used a decision tree algorithm to fit the lynx population data. As expected, the linear regression is not exactly impressive
<p>
@@ -1036,7 +1035,7 @@ plt<span style="color: #666666">.</span>plot(x, y, label<span style="color: #666
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
The similar code for linear regression in <b>R</b> reads
The similar code for linear regression in <b>R</b> reads (more details to come)
<p>
<!-- code=r (!bc r) typeset with pygments style "default" -->
@@ -1145,7 +1144,6 @@ Our task is to first set up an algorithm which simulates the above transactions
\( w_m\Delta m \). You will need to set up a value for the interval \( \Delta m \) (typically \( 0.01-0.05 \)).
That means you need to account for the number of times you register an income in the interval
\( m,m+\Delta m \). The number of times you register this income, represents the value that enters the histogram.
You will also need to find a criterion for when the equilibrium situation has been reached.
<p>