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
@@ -142,7 +142,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>
<p>
</div> <!-- end jumbotron -->
@@ -481,7 +481,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)
@@ -594,6 +594,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" class="anchor">Non-Linear Least squares in R </h2>
<div class="panel panel-default">
@@ -650,7 +651,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
@@ -678,7 +679,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>
@@ -691,7 +692,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>
@@ -702,13 +703,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
@@ -739,7 +739,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
@@ -749,12 +749,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>)
@@ -762,10 +763,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>)
@@ -776,7 +776,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>)
@@ -1038,7 +1037,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>
@@ -1063,7 +1062,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" -->
@@ -1172,7 +1171,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>
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>&nbsp;<br>
<center><h4>May 29, 2018</h4></center> <!-- date -->
<center><h4>May 30, 2018</h4></center> <!-- date -->
<br>
<h2 id="___sec0">Introduction </h2>
@@ -498,7 +498,7 @@ example of the functionality of scikit-learn.
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
x = np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">2.0</span>+ <span style="color: #B452CD">5</span>*x+<span style="color: #B452CD">0.5</span>np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">2.0</span>+ <span style="color: #B452CD">5</span>*x+<span style="color: #B452CD">0.5</span>*np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
linreg = LinearRegression()
linreg.fit(x,y)
ypredict = linreg.predict(x)
@@ -621,6 +621,7 @@ plt.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">
@@ -674,7 +675,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
@@ -700,7 +701,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>
<p><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>
<p><li> Same proportionality wrt death</li>
<p><li> Same proportionality with respect to death</li>
<p><li> Proposed model: \( \Delta N = b\Delta t N - d\Delta tN \) for some unknown
constants \( b \) (births) and \( d \) (deaths)</li>
<p><li> Describe evolution in discrete time: \( t_n=n\Delta t \)</li>
@@ -711,7 +712,7 @@ Here we will construct a model for cell growth based on a simple difference equa
</div>
<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>
@@ -722,13 +723,12 @@ set \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \). The program reads
t = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">21</span>) <span style="color: #228B22"># 20 intervals in [0, 10]</span>
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">1</span>
r = <span style="color: #B452CD">0.5</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, N.size-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>])
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>]))
</pre></div>
<p>
and it generates the following output
@@ -759,7 +759,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
@@ -773,12 +773,13 @@ $$ r = \frac{N^{n+1}-N^n}{N^n\Delta t} $$
<p>&nbsp;<br>
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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #228B22"># Estimate r</span>
data = np.loadtxt(<span style="color: #CD5555">&#39;ecoli.csv&#39;</span>, delimiter=<span style="color: #CD5555">&#39;,&#39;</span>)
@@ -786,10 +787,9 @@ t_e = data[:,<span style="color: #B452CD">0</span>]
N_e = data[:,<span style="color: #B452CD">1</span>]
i = <span style="color: #B452CD">2</span> <span style="color: #228B22"># Data point (i,i+1) used to estimate r</span>
r = (N_e[i+<span style="color: #B452CD">1</span>] - N_e[i])/(N_e[i]*(t_e[i+<span style="color: #B452CD">1</span>] - t_e[i]))
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r)
<span style="color: #228B22"># Can experiment with r values and see if the model can</span>
<span style="color: #228B22"># match the data better</span>
T = <span style="color: #B452CD">1200</span> <span style="color: #228B22"># cell can divide after T sec</span>
t_max = <span style="color: #B452CD">5</span>*T <span style="color: #228B22"># 5 generations in experiment</span>
t = np.linspace(<span style="color: #B452CD">0</span>, t_max, <span style="color: #B452CD">1000</span>)
@@ -800,7 +800,6 @@ N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">100</spa
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, <span style="color: #658b00">len</span>(t)-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
plt.plot(t, N, <span style="color: #CD5555">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #CD5555">&#39;bo&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;time [s]&#39;</span>); plt.ylabel(<span style="color: #CD5555">&#39;N&#39;</span>)
plt.legend([<span style="color: #CD5555">&#39;model&#39;</span>, <span style="color: #CD5555">&#39;experiment&#39;</span>], loc=<span style="color: #CD5555">&#39;upper left&#39;</span>)
@@ -1070,7 +1069,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>
@@ -1095,7 +1094,7 @@ plt.plot(x, y, label= <span style="color: #CD5555">&quot;Linear Regression&quot;
plt.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 "perldoc" -->
@@ -1216,7 +1215,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>
@@ -120,7 +120,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>
@@ -457,7 +457,7 @@ example of the functionality of scikit-learn.
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
x = np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">2.0</span>+ <span style="color: #B452CD">5</span>*x+<span style="color: #B452CD">0.5</span>np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">2.0</span>+ <span style="color: #B452CD">5</span>*x+<span style="color: #B452CD">0.5</span>*np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
linreg = LinearRegression()
linreg.fit(x,y)
ypredict = linreg.predict(x)
@@ -570,6 +570,7 @@ plt.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">
@@ -625,7 +626,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
@@ -653,7 +654,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>
@@ -665,7 +666,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>
@@ -676,13 +677,12 @@ set \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \). The program reads
t = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">21</span>) <span style="color: #228B22"># 20 intervals in [0, 10]</span>
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">1</span>
r = <span style="color: #B452CD">0.5</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, N.size-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>])
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>]))
</pre></div>
<p>
and it generates the following output
@@ -713,7 +713,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
@@ -723,12 +723,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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #228B22"># Estimate r</span>
data = np.loadtxt(<span style="color: #CD5555">&#39;ecoli.csv&#39;</span>, delimiter=<span style="color: #CD5555">&#39;,&#39;</span>)
@@ -736,10 +737,9 @@ t_e = data[:,<span style="color: #B452CD">0</span>]
N_e = data[:,<span style="color: #B452CD">1</span>]
i = <span style="color: #B452CD">2</span> <span style="color: #228B22"># Data point (i,i+1) used to estimate r</span>
r = (N_e[i+<span style="color: #B452CD">1</span>] - N_e[i])/(N_e[i]*(t_e[i+<span style="color: #B452CD">1</span>] - t_e[i]))
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r)
<span style="color: #228B22"># Can experiment with r values and see if the model can</span>
<span style="color: #228B22"># match the data better</span>
T = <span style="color: #B452CD">1200</span> <span style="color: #228B22"># cell can divide after T sec</span>
t_max = <span style="color: #B452CD">5</span>*T <span style="color: #228B22"># 5 generations in experiment</span>
t = np.linspace(<span style="color: #B452CD">0</span>, t_max, <span style="color: #B452CD">1000</span>)
@@ -750,7 +750,6 @@ N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">100</spa
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, <span style="color: #658b00">len</span>(t)-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
plt.plot(t, N, <span style="color: #CD5555">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #CD5555">&#39;bo&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;time [s]&#39;</span>); plt.ylabel(<span style="color: #CD5555">&#39;N&#39;</span>)
plt.legend([<span style="color: #CD5555">&#39;model&#39;</span>, <span style="color: #CD5555">&#39;experiment&#39;</span>], loc=<span style="color: #CD5555">&#39;upper left&#39;</span>)
@@ -1006,7 +1005,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>
@@ -1031,7 +1030,7 @@ plt.plot(x, y, label= <span style="color: #CD5555">&quot;Linear Regression&quot;
plt.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 "perldoc" -->
@@ -1140,7 +1139,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>
+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>
+15 -18
View File
@@ -10,7 +10,7 @@
"<!-- Author: --> \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
"Date: **May 29, 2018**\n",
"Date: **May 30, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -410,7 +410,7 @@
"from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error\n",
"\n",
"x = np.random.rand(100,1)\n",
"y = 2.0+ 5*x+0.5np.random.randn(100,1)\n",
"y = 2.0+ 5*x+0.5*np.random.randn(100,1)\n",
"linreg = LinearRegression()\n",
"linreg.fit(x,y)\n",
"ypredict = linreg.predict(x)\n",
@@ -588,7 +588,7 @@
"metadata": {},
"source": [
"Similarly, using **R**, we can perform similar studies. The following **R** code illustrates this.\n",
"\n",
"(more details on **R** will be inserted later).\n",
"\n",
"## Non-Linear Least squares in R"
]
@@ -653,7 +653,7 @@
"## Examples\n",
"\n",
"We present here several examples, with pertinent Python codes that we\n",
"will us to illustrate various machine learning methods and ways to\n",
"will use to illustrate various machine learning methods and ways to\n",
"analyze, from simple to complex, various data sets. Many of these\n",
"examples allow us to generate the data we want to analyze, following\n",
"much of the same philosophy we discussed above when\n",
@@ -678,7 +678,7 @@
"3. $N$ cells result in twice as many new individuals $\\Delta N$ in\n",
" time $2\\Delta t$ as in time $\\Delta t$: $\\Delta N \\propto\\Delta t$\n",
"\n",
"4. Same proportionality wrt death \n",
"4. Same proportionality with respect to death \n",
"\n",
"5. Proposed model: $\\Delta N = b\\Delta t N - d\\Delta tN$ for some unknown\n",
" constants $b$ (births) and $d$ (deaths)\n",
@@ -693,7 +693,7 @@
"\n",
"\n",
"\n",
"The difference equation can be programmed in a simple was, and in order to get started we\n",
"The difference equation can be programmed in a simple way, and in order to get started we\n",
"set $r=1.5$, $N^0=1$, $\\Delta t=0.5$. The program reads"
]
},
@@ -710,13 +710,12 @@
"t = np.linspace(0, 10, 21) # 20 intervals in [0, 10]\n",
"dt = t[1] - t[0]\n",
"N = np.zeros(t.size)\n",
"\n",
"N[0] = 1\n",
"r = 0.5\n",
"\n",
"for n in range(0, N.size-1, 1):\n",
" N[n+1] = N[n] + r*dt*N[n]\n",
" print 'N[%d]=%.1f' % (n+1, N[n+1])"
" print('N[%d]=%.1f' % (n+1, N[n+1]))"
]
},
{
@@ -758,7 +757,7 @@
"source": [
"This forms our data which later will define our training set. \n",
"In this case we defined the value of the parameter $r$. We could alternatively assume that we just received the \n",
"above data file and where asked to use find $r$. How can we estimate $r$ from data?\n",
"above data file and where asked to find $r$. How can we estimate $r$ from data? This will be one of our tasks later.\n",
"\n",
"We can use the difference equation with the experimental data"
]
@@ -793,8 +792,8 @@
"metadata": {},
"source": [
"Suppose we set $t_1=600$, $t_2=1200$,\n",
"$N^1=140$ and $N^2=250$. We obtain then $r=0.0013$. The exact value is $r = 0.000694$\n",
"The following code plot"
"$N^1=140$ and $N^2=250$. \n",
"The following code plots the data"
]
},
{
@@ -806,6 +805,7 @@
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Estimate r\n",
"data = np.loadtxt('ecoli.csv', delimiter=',')\n",
@@ -813,10 +813,9 @@
"N_e = data[:,1]\n",
"i = 2 # Data point (i,i+1) used to estimate r\n",
"r = (N_e[i+1] - N_e[i])/(N_e[i]*(t_e[i+1] - t_e[i]))\n",
"print 'Estimated r=%.5f' % r\n",
"print('Estimated r=%.5f' % r)\n",
"# Can experiment with r values and see if the model can\n",
"# match the data better\n",
"\n",
"T = 1200 # cell can divide after T sec\n",
"t_max = 5*T # 5 generations in experiment\n",
"t = np.linspace(0, t_max, 1000)\n",
@@ -827,7 +826,6 @@
"for n in range(0, len(t)-1, 1):\n",
" N[n+1] = N[n] + r*dt*N[n]\n",
"\n",
"import matplotlib.pyplot as plt\n",
"plt.plot(t, N, 'r-', t_e, N_e, 'bo')\n",
"plt.xlabel('time [s]'); plt.ylabel('N')\n",
"plt.legend(['model', 'experiment'], loc='upper left')\n",
@@ -1180,7 +1178,7 @@
"$H(0) = 34.91$ and $L(0)=3.857$. \n",
"\n",
"\n",
"The following Python demonstrates how we can use linear regression to fit for example the population of lynx.\n",
"The following Python code demonstrates how we can use linear regression to fit for example the population of lynx.\n",
"Similarly, we have also used a decision tree algorithm to fit the lynx population data. As expected, the linear regression is not exactly impressive"
]
},
@@ -1216,7 +1214,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The similar code for linear regression in **R** reads"
"The similar code for linear regression in **R** reads (more details to come)"
]
},
{
@@ -1383,8 +1381,7 @@
" This histogram contains the number of times a value $m$ is registered and represents\n",
" $w_m\\Delta m$. You will need to set up a value for the interval $\\Delta m$ (typically $0.01-0.05$).\n",
" That means you need to account for the number of times you register an income in the interval\n",
" $m,m+\\Delta m$. The number of times you register this income, represents the value that enters the histogram.\n",
" You will also need to find a criterion for when the equilibrium situation has been reached."
" $m,m+\\Delta m$. The number of times you register this income, represents the value that enters the histogram."
]
},
{
Binary file not shown.
+13 -16
View File
@@ -317,7 +317,7 @@ from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
x = np.random.rand(100,1)
y = 2.0+ 5*x+0.5np.random.randn(100,1)
y = 2.0+ 5*x+0.5*np.random.randn(100,1)
linreg = LinearRegression()
linreg.fit(x,y)
ypredict = linreg.predict(x)
@@ -430,7 +430,7 @@ print (error(y))
!ec
Similarly, using _R_, we can perform similar studies. The following _R_ code illustrates this.
(more details on _R_ will be inserted later).
===== Non-Linear Least squares in R =====
!bblock
@@ -478,7 +478,7 @@ display(data_pandas)
===== Examples =====
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
@@ -502,7 +502,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$
o $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$
o Same proportionality wrt death
o Same proportionality with respect to death
o Proposed model: $\Delta N = b\Delta t N - d\Delta tN$ for some unknown
constants $b$ (births) and $d$ (deaths)
o Describe evolution in discrete time: $t_n=n\Delta t$
@@ -511,7 +511,7 @@ Here we will construct a model for cell growth based on a simple difference equa
o Program model: `N[n+1] = N[n] + r*dt*N[n]`
!eblock
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
!bc pycod
@@ -520,13 +520,12 @@ import numpy as np
t = np.linspace(0, 10, 21) # 20 intervals in [0, 10]
dt = t[1] - t[0]
N = np.zeros(t.size)
N[0] = 1
r = 0.5
for n in range(0, N.size-1, 1):
N[n+1] = N[n] + r*dt*N[n]
print 'N[%d]=%.1f' % (n+1, N[n+1])
print('N[%d]=%.1f' % (n+1, N[n+1]))
!ec
and it generates the following output
!bc
@@ -553,7 +552,7 @@ N[20]=86.7
!ec
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.
We can use the difference equation with the experimental data
!bt
@@ -564,10 +563,11 @@ Suppose now that $N^{n+1}$ and $N^n$ are known from data. Then we could solve w
\[ r = \frac{N^{n+1}-N^n}{N^n\Delta t} \]
!et
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
!bc pycod
import numpy as np
import matplotlib.pyplot as plt
# Estimate r
data = np.loadtxt('ecoli.csv', delimiter=',')
@@ -575,10 +575,9 @@ t_e = data[:,0]
N_e = data[:,1]
i = 2 # Data point (i,i+1) used to estimate r
r = (N_e[i+1] - N_e[i])/(N_e[i]*(t_e[i+1] - t_e[i]))
print 'Estimated r=%.5f' % r
print('Estimated r=%.5f' % r)
# Can experiment with r values and see if the model can
# match the data better
T = 1200 # cell can divide after T sec
t_max = 5*T # 5 generations in experiment
t = np.linspace(0, t_max, 1000)
@@ -589,7 +588,6 @@ N[0] = 100
for n in range(0, len(t)-1, 1):
N[n+1] = N[n] + r*dt*N[n]
import matplotlib.pyplot as plt
plt.plot(t, N, 'r-', t_e, N_e, 'bo')
plt.xlabel('time [s]'); plt.ylabel('N')
plt.legend(['model', 'experiment'], loc='upper left')
@@ -767,7 +765,7 @@ parameters result in a slightly modified initial conditions, namely
$H(0) = 34.91$ and $L(0)=3.857$.
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
!bc pycod
import numpy as np
@@ -792,7 +790,7 @@ plt.show()
The similar code for linear regression in _R_ reads
The similar code for linear regression in _R_ reads (more details to come)
!bc r
HudsonBay = read.csv("src/Hudson_Bay.csv",header=T)
fix(HudsonBay)
@@ -890,7 +888,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.
!bc pycod
#!/usr/bin/env python