update
This commit is contained in:
@@ -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">'N[%d]=%.1f'</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">'N[%d]=%.1f'</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">'ecoli.csv'</span>, delimiter=<span style="color: #CD5555">','</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">'Estimated r=%.5f'</span> % r
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Estimated r=%.5f'</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">'r-'</span>, t_e, N_e, <span style="color: #CD5555">'bo'</span>)
|
||||
plt.xlabel(<span style="color: #CD5555">'time [s]'</span>); plt.ylabel(<span style="color: #CD5555">'N'</span>)
|
||||
plt.legend([<span style="color: #CD5555">'model'</span>, <span style="color: #CD5555">'experiment'</span>], loc=<span style="color: #CD5555">'upper left'</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">"Linear Regression"
|
||||
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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user