update
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user