added random walk example
This commit is contained in:
@@ -57,7 +57,8 @@ Automatically generated HTML file from DocOnce source
|
||||
('Particle in one dimension and velocity distribution',
|
||||
3,
|
||||
None,
|
||||
'___sec11')]}
|
||||
'___sec11'),
|
||||
('Random walk model', 3, None, '___sec12')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -107,6 +108,7 @@ MathJax.Hub.Config({
|
||||
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"> Predator-Prey model from ecology</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;"> Simulating financial transactions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;"> Particle in one dimension and velocity distribution</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;"> Random walk model</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -143,11 +145,8 @@ MathJax.Hub.Config({
|
||||
<center><h4>May 29, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- potential-jumbotron-button -->
|
||||
</div> <!-- end jumbotron -->
|
||||
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec0" class="anchor">Introduction </h2>
|
||||
|
||||
<p>
|
||||
@@ -188,9 +187,6 @@ introduce will serve as inputs to many of our discussions later, as
|
||||
well as allowing you to set up models and produce your own data and
|
||||
get started with programming.
|
||||
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec1" class="anchor">Software and needed installations </h2>
|
||||
|
||||
<p>
|
||||
@@ -233,9 +229,6 @@ you can use <b>pip</b> as well and simply install Python as
|
||||
|
||||
etc etc.
|
||||
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec2" class="anchor">Python installers </h2>
|
||||
|
||||
<p>
|
||||
@@ -263,9 +256,6 @@ distribution for scientific and analytic computing distribution and
|
||||
analysis environment, available for free and under a commercial
|
||||
license.
|
||||
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec3" class="anchor">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
@@ -284,9 +274,6 @@ texts.
|
||||
To install <b>R</b> with Jupyter notebook
|
||||
<a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_self">follow the link here</a>
|
||||
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4" class="anchor">Installing R, C++, cython, Numba etc </h2>
|
||||
|
||||
<p>
|
||||
@@ -321,9 +308,6 @@ Finally, if you wish to use the light mark-up language
|
||||
<a href="https://github.com/hplgit/doconce" target="_self">doconce</a> you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
|
||||
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec5" class="anchor">Simple linear regression model using <b>scikit-learn</b> </h2>
|
||||
|
||||
<p>
|
||||
@@ -610,7 +594,6 @@ 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.
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec6" class="anchor">Non-Linear Least squares in R </h2>
|
||||
<div class="panel panel-default">
|
||||
@@ -662,8 +645,6 @@ data <span style="color: #666666">=</span> {<span style="color: #BA2121">'Na
|
||||
data_pandas <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(data)
|
||||
display(data_pandas)
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec7" class="anchor">Examples </h2>
|
||||
|
||||
@@ -1332,6 +1313,101 @@ plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-5</s
|
||||
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec12" class="anchor">Random walk model </h3>
|
||||
|
||||
<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: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
|
||||
|
||||
steps<span style="color: #666666">=250</span>
|
||||
|
||||
distance<span style="color: #666666">=0</span>
|
||||
x<span style="color: #666666">=0</span>
|
||||
distance_list<span style="color: #666666">=</span>[]
|
||||
steps_list<span style="color: #666666">=</span>[]
|
||||
<span style="color: #008000; font-weight: bold">while</span> x<span style="color: #666666"><</span>steps:
|
||||
distance<span style="color: #666666">+=</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randint(<span style="color: #666666">-1</span>,<span style="color: #666666">2</span>)
|
||||
distance_list<span style="color: #666666">.</span>append(distance)
|
||||
x<span style="color: #666666">+=1</span>
|
||||
steps_list<span style="color: #666666">.</span>append(x)
|
||||
plt<span style="color: #666666">.</span>plot(steps_list,distance_list, color<span style="color: #666666">=</span><span style="color: #BA2121">'green'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Random Walk Data"</span>)
|
||||
|
||||
steps_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(steps_list)
|
||||
distance_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(distance_list)
|
||||
|
||||
X<span style="color: #666666">=</span>steps_list[:,np<span style="color: #666666">.</span>newaxis]
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Polynomial fits</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 2</span>
|
||||
poly_features<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=2</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly<span style="color: #666666">=</span>poly_features<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
lin_reg<span style="color: #666666">=</span>LinearRegression()
|
||||
poly_fit<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly,distance_list)
|
||||
b<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>coef_
|
||||
c<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>intercept_
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"2nd degree coefficients:"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"zero power: "</span>,c)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"first power: "</span>, b[<span style="color: #666666">0</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"second power: "</span>,b[<span style="color: #666666">1</span>])
|
||||
|
||||
z <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0</span>, steps, <span style="color: #666666">.01</span>)
|
||||
z_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>z<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>z<span style="color: #666666">+</span>c
|
||||
|
||||
fit_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>X<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>X<span style="color: #666666">+</span>c
|
||||
plt<span style="color: #666666">.</span>plot(z, z_mod, color<span style="color: #666666">=</span><span style="color: #BA2121">'r'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"2nd Degree Fit"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Polynomial Regression"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Steps"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Distance"</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 10</span>
|
||||
poly_features10<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=10</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly10<span style="color: #666666">=</span>poly_features10<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
poly_fit10<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly10,distance_list)
|
||||
|
||||
y_plot<span style="color: #666666">=</span>poly_fit10<span style="color: #666666">.</span>predict(X_poly10)
|
||||
plt<span style="color: #666666">.</span>plot(X, y_plot, color<span style="color: #666666">=</span><span style="color: #BA2121">'black'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"10th Degree Fit"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Decision Tree Regression</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
regr_1<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=2</span>)
|
||||
regr_2<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=5</span>)
|
||||
regr_3<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=7</span>)
|
||||
regr_1<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_2<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_3<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
|
||||
X_test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0.0</span>, steps, <span style="color: #666666">0.01</span>)[:, np<span style="color: #666666">.</span>newaxis]
|
||||
y_1 <span style="color: #666666">=</span> regr_1<span style="color: #666666">.</span>predict(X_test)
|
||||
y_2 <span style="color: #666666">=</span> regr_2<span style="color: #666666">.</span>predict(X_test)
|
||||
y_3<span style="color: #666666">=</span>regr_3<span style="color: #666666">.</span>predict(X_test)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>scatter(X, distance_list, s<span style="color: #666666">=2.5</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">"black"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"data"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_1, color<span style="color: #666666">=</span><span style="color: #BA2121">"red"</span>,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=2"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_2, color<span style="color: #666666">=</span><span style="color: #BA2121">"green"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=5"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_3, color<span style="color: #666666">=</span><span style="color: #BA2121">"m"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=7"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Data"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Darget"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Decision Tree Regression"</span>)
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
Reference in New Issue
Block a user