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 --------------- -->
|
||||
|
||||
@@ -150,15 +150,7 @@ MathJax.Hub.Config({
|
||||
<p> <br>
|
||||
<center><h4>May 29, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright --> © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
||||
</center>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec0">Introduction </h2>
|
||||
|
||||
<p>
|
||||
@@ -198,10 +190,7 @@ tensorflow (see below for links etc). Moreover, the examples we
|
||||
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.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec1">Software and needed installations </h2>
|
||||
|
||||
<p>
|
||||
@@ -246,10 +235,7 @@ you can use <b>pip</b> as well and simply install Python as
|
||||
<p>
|
||||
|
||||
etc etc.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec2">Python installers </h2>
|
||||
|
||||
<p>
|
||||
@@ -278,10 +264,7 @@ is a Python
|
||||
distribution for scientific and analytic computing distribution and
|
||||
analysis environment, available for free and under a commercial
|
||||
license.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec3">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
@@ -299,10 +282,7 @@ texts.
|
||||
<p>
|
||||
To install <b>R</b> with Jupyter notebook
|
||||
<a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">follow the link here</a>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec4">Installing R, C++, cython, Numba etc </h2>
|
||||
|
||||
<p>
|
||||
@@ -336,10 +316,7 @@ And to add more versatility, the Python package <a href="http://www.sympy.org/en
|
||||
Finally, if you wish to use the light mark-up language
|
||||
<a href="https://github.com/hplgit/doconce" target="_blank">doconce</a> you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec5">Simple linear regression model using <b>scikit-learn</b> </h2>
|
||||
|
||||
<p>
|
||||
@@ -644,10 +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.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec6">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
@@ -695,10 +669,7 @@ data = {<span style="color: #CD5555">'Name'</span>: [<span style="color:
|
||||
data_pandas = pd.DataFrame(data)
|
||||
display(data_pandas)
|
||||
</pre></div>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec7">Examples </h2>
|
||||
|
||||
<p>
|
||||
@@ -1396,6 +1367,107 @@ plt.axis([-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">5
|
||||
plt.grid(<span style="color: #658b00">True</span>)
|
||||
plt.show()
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec12">Random walk model </h3>
|
||||
|
||||
<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: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LinearRegression
|
||||
|
||||
steps=<span style="color: #B452CD">250</span>
|
||||
|
||||
distance=<span style="color: #B452CD">0</span>
|
||||
x=<span style="color: #B452CD">0</span>
|
||||
distance_list=[]
|
||||
steps_list=[]
|
||||
<span style="color: #8B008B; font-weight: bold">while</span> x<steps:
|
||||
distance+=np.random.randint(-<span style="color: #B452CD">1</span>,<span style="color: #B452CD">2</span>)
|
||||
distance_list.append(distance)
|
||||
x+=<span style="color: #B452CD">1</span>
|
||||
steps_list.append(x)
|
||||
plt.plot(steps_list,distance_list, color=<span style="color: #CD5555">'green'</span>, label=<span style="color: #CD5555">"Random Walk Data"</span>)
|
||||
|
||||
steps_list=np.asarray(steps_list)
|
||||
distance_list=np.asarray(distance_list)
|
||||
|
||||
X=steps_list[:,np.newaxis]
|
||||
|
||||
<span style="color: #228B22">#Polynomial fits</span>
|
||||
|
||||
<span style="color: #228B22">#Degree 2</span>
|
||||
poly_features=PolynomialFeatures(degree=<span style="color: #B452CD">2</span>, include_bias=<span style="color: #658b00">False</span>)
|
||||
X_poly=poly_features.fit_transform(X)
|
||||
|
||||
lin_reg=LinearRegression()
|
||||
poly_fit=lin_reg.fit(X_poly,distance_list)
|
||||
b=lin_reg.coef_
|
||||
c=lin_reg.intercept_
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"2nd degree coefficients:"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"zero power: "</span>,c)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"first power: "</span>, b[<span style="color: #B452CD">0</span>])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"second power: "</span>,b[<span style="color: #B452CD">1</span>])
|
||||
|
||||
z = np.arange(<span style="color: #B452CD">0</span>, steps, .<span style="color: #B452CD">01</span>)
|
||||
z_mod=b[<span style="color: #B452CD">1</span>]*z**<span style="color: #B452CD">2</span>+b[<span style="color: #B452CD">0</span>]*z+c
|
||||
|
||||
fit_mod=b[<span style="color: #B452CD">1</span>]*X**<span style="color: #B452CD">2</span>+b[<span style="color: #B452CD">0</span>]*X+c
|
||||
plt.plot(z, z_mod, color=<span style="color: #CD5555">'r'</span>, label=<span style="color: #CD5555">"2nd Degree Fit"</span>)
|
||||
plt.title(<span style="color: #CD5555">"Polynomial Regression"</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">"Steps"</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">"Distance"</span>)
|
||||
|
||||
<span style="color: #228B22">#Degree 10</span>
|
||||
poly_features10=PolynomialFeatures(degree=<span style="color: #B452CD">10</span>, include_bias=<span style="color: #658b00">False</span>)
|
||||
X_poly10=poly_features10.fit_transform(X)
|
||||
|
||||
poly_fit10=lin_reg.fit(X_poly10,distance_list)
|
||||
|
||||
y_plot=poly_fit10.predict(X_poly10)
|
||||
plt.plot(X, y_plot, color=<span style="color: #CD5555">'black'</span>, label=<span style="color: #CD5555">"10th Degree Fit"</span>)
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
<span style="color: #228B22">#Decision Tree Regression</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
regr_1=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">2</span>)
|
||||
regr_2=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">5</span>)
|
||||
regr_3=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">7</span>)
|
||||
regr_1.fit(X, distance_list)
|
||||
regr_2.fit(X, distance_list)
|
||||
regr_3.fit(X, distance_list)
|
||||
|
||||
X_test = np.arange(<span style="color: #B452CD">0.0</span>, steps, <span style="color: #B452CD">0.01</span>)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
y_3=regr_3.predict(X_test)
|
||||
|
||||
<span style="color: #228B22"># Plot the results</span>
|
||||
plt.figure()
|
||||
plt.scatter(X, distance_list, s=<span style="color: #B452CD">2.5</span>, c=<span style="color: #CD5555">"black"</span>, label=<span style="color: #CD5555">"data"</span>)
|
||||
plt.plot(X_test, y_1, color=<span style="color: #CD5555">"red"</span>,
|
||||
label=<span style="color: #CD5555">"max_depth=2"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
plt.plot(X_test, y_2, color=<span style="color: #CD5555">"green"</span>, label=<span style="color: #CD5555">"max_depth=5"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
plt.plot(X_test, y_3, color=<span style="color: #CD5555">"m"</span>, label=<span style="color: #CD5555">"max_depth=7"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">"Data"</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">"Darget"</span>)
|
||||
plt.title(<span style="color: #CD5555">"Decision Tree Regression"</span>)
|
||||
plt.legend()
|
||||
plt.show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright --> © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
||||
</center>
|
||||
</section>
|
||||
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
('Particle in one dimension and velocity distribution',
|
||||
3,
|
||||
None,
|
||||
'___sec11')]}
|
||||
'___sec11'),
|
||||
('Random walk model', 3, None, '___sec12')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -121,8 +122,6 @@ MathJax.Hub.Config({
|
||||
<p>
|
||||
<center><h4>May 29, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Introduction </h2>
|
||||
|
||||
@@ -164,9 +163,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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Software and needed installations </h2>
|
||||
|
||||
<p>
|
||||
@@ -209,9 +205,6 @@ you can use <b>pip</b> as well and simply install Python as
|
||||
|
||||
etc etc.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Python installers </h2>
|
||||
|
||||
<p>
|
||||
@@ -239,9 +232,6 @@ distribution for scientific and analytic computing distribution and
|
||||
analysis environment, available for free and under a commercial
|
||||
license.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
@@ -260,9 +250,6 @@ texts.
|
||||
To install <b>R</b> with Jupyter notebook
|
||||
<a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">follow the link here</a>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">Installing R, C++, cython, Numba etc </h2>
|
||||
|
||||
<p>
|
||||
@@ -297,9 +284,6 @@ Finally, if you wish to use the light mark-up language
|
||||
<a href="https://github.com/hplgit/doconce" target="_blank">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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Simple linear regression model using <b>scikit-learn</b> </h2>
|
||||
|
||||
<p>
|
||||
@@ -586,7 +570,6 @@ plt.show()
|
||||
</pre></div>
|
||||
<p>
|
||||
Similarly, using <b>R</b>, we can perform similar studies. The following <b>R</b> code illustrates this.
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -637,8 +620,6 @@ data = {<span style="color: #CD5555">'Name'</span>: [<span style="color:
|
||||
data_pandas = pd.DataFrame(data)
|
||||
display(data_pandas)
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Examples </h2>
|
||||
|
||||
@@ -1300,6 +1281,101 @@ plt.axis([-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">5
|
||||
plt.grid(<span style="color: #658b00">True</span>)
|
||||
plt.show()
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec12">Random walk model </h3>
|
||||
|
||||
<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: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LinearRegression
|
||||
|
||||
steps=<span style="color: #B452CD">250</span>
|
||||
|
||||
distance=<span style="color: #B452CD">0</span>
|
||||
x=<span style="color: #B452CD">0</span>
|
||||
distance_list=[]
|
||||
steps_list=[]
|
||||
<span style="color: #8B008B; font-weight: bold">while</span> x<steps:
|
||||
distance+=np.random.randint(-<span style="color: #B452CD">1</span>,<span style="color: #B452CD">2</span>)
|
||||
distance_list.append(distance)
|
||||
x+=<span style="color: #B452CD">1</span>
|
||||
steps_list.append(x)
|
||||
plt.plot(steps_list,distance_list, color=<span style="color: #CD5555">'green'</span>, label=<span style="color: #CD5555">"Random Walk Data"</span>)
|
||||
|
||||
steps_list=np.asarray(steps_list)
|
||||
distance_list=np.asarray(distance_list)
|
||||
|
||||
X=steps_list[:,np.newaxis]
|
||||
|
||||
<span style="color: #228B22">#Polynomial fits</span>
|
||||
|
||||
<span style="color: #228B22">#Degree 2</span>
|
||||
poly_features=PolynomialFeatures(degree=<span style="color: #B452CD">2</span>, include_bias=<span style="color: #658b00">False</span>)
|
||||
X_poly=poly_features.fit_transform(X)
|
||||
|
||||
lin_reg=LinearRegression()
|
||||
poly_fit=lin_reg.fit(X_poly,distance_list)
|
||||
b=lin_reg.coef_
|
||||
c=lin_reg.intercept_
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"2nd degree coefficients:"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"zero power: "</span>,c)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"first power: "</span>, b[<span style="color: #B452CD">0</span>])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (<span style="color: #CD5555">"second power: "</span>,b[<span style="color: #B452CD">1</span>])
|
||||
|
||||
z = np.arange(<span style="color: #B452CD">0</span>, steps, .<span style="color: #B452CD">01</span>)
|
||||
z_mod=b[<span style="color: #B452CD">1</span>]*z**<span style="color: #B452CD">2</span>+b[<span style="color: #B452CD">0</span>]*z+c
|
||||
|
||||
fit_mod=b[<span style="color: #B452CD">1</span>]*X**<span style="color: #B452CD">2</span>+b[<span style="color: #B452CD">0</span>]*X+c
|
||||
plt.plot(z, z_mod, color=<span style="color: #CD5555">'r'</span>, label=<span style="color: #CD5555">"2nd Degree Fit"</span>)
|
||||
plt.title(<span style="color: #CD5555">"Polynomial Regression"</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">"Steps"</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">"Distance"</span>)
|
||||
|
||||
<span style="color: #228B22">#Degree 10</span>
|
||||
poly_features10=PolynomialFeatures(degree=<span style="color: #B452CD">10</span>, include_bias=<span style="color: #658b00">False</span>)
|
||||
X_poly10=poly_features10.fit_transform(X)
|
||||
|
||||
poly_fit10=lin_reg.fit(X_poly10,distance_list)
|
||||
|
||||
y_plot=poly_fit10.predict(X_poly10)
|
||||
plt.plot(X, y_plot, color=<span style="color: #CD5555">'black'</span>, label=<span style="color: #CD5555">"10th Degree Fit"</span>)
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
<span style="color: #228B22">#Decision Tree Regression</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
regr_1=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">2</span>)
|
||||
regr_2=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">5</span>)
|
||||
regr_3=DecisionTreeRegressor(max_depth=<span style="color: #B452CD">7</span>)
|
||||
regr_1.fit(X, distance_list)
|
||||
regr_2.fit(X, distance_list)
|
||||
regr_3.fit(X, distance_list)
|
||||
|
||||
X_test = np.arange(<span style="color: #B452CD">0.0</span>, steps, <span style="color: #B452CD">0.01</span>)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
y_3=regr_3.predict(X_test)
|
||||
|
||||
<span style="color: #228B22"># Plot the results</span>
|
||||
plt.figure()
|
||||
plt.scatter(X, distance_list, s=<span style="color: #B452CD">2.5</span>, c=<span style="color: #CD5555">"black"</span>, label=<span style="color: #CD5555">"data"</span>)
|
||||
plt.plot(X_test, y_1, color=<span style="color: #CD5555">"red"</span>,
|
||||
label=<span style="color: #CD5555">"max_depth=2"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
plt.plot(X_test, y_2, color=<span style="color: #CD5555">"green"</span>, label=<span style="color: #CD5555">"max_depth=5"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
plt.plot(X_test, y_3, color=<span style="color: #CD5555">"m"</span>, label=<span style="color: #CD5555">"max_depth=7"</span>, linewidth=<span style="color: #B452CD">2</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">"Data"</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">"Darget"</span>)
|
||||
plt.title(<span style="color: #CD5555">"Decision Tree Regression"</span>)
|
||||
plt.legend()
|
||||
plt.show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -82,7 +82,8 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
('Particle in one dimension and velocity distribution',
|
||||
3,
|
||||
None,
|
||||
'___sec11')]}
|
||||
'___sec11'),
|
||||
('Random walk model', 3, None, '___sec12')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -126,8 +127,6 @@ MathJax.Hub.Config({
|
||||
<p>
|
||||
<center><h4>May 29, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Introduction </h2>
|
||||
|
||||
@@ -169,9 +168,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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Software and needed installations </h2>
|
||||
|
||||
<p>
|
||||
@@ -214,9 +210,6 @@ you can use <b>pip</b> as well and simply install Python as
|
||||
|
||||
etc etc.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Python installers </h2>
|
||||
|
||||
<p>
|
||||
@@ -244,9 +237,6 @@ distribution for scientific and analytic computing distribution and
|
||||
analysis environment, available for free and under a commercial
|
||||
license.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Installing R, C++, cython or Julia </h2>
|
||||
|
||||
<p>
|
||||
@@ -265,9 +255,6 @@ texts.
|
||||
To install <b>R</b> with Jupyter notebook
|
||||
<a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">follow the link here</a>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">Installing R, C++, cython, Numba etc </h2>
|
||||
|
||||
<p>
|
||||
@@ -302,9 +289,6 @@ Finally, if you wish to use the light mark-up language
|
||||
<a href="https://github.com/hplgit/doconce" target="_blank">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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Simple linear regression model using <b>scikit-learn</b> </h2>
|
||||
|
||||
<p>
|
||||
@@ -591,7 +575,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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -642,8 +625,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 --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Examples </h2>
|
||||
|
||||
@@ -1305,6 +1286,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">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 --------------- -->
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Introduction\n",
|
||||
"\n",
|
||||
"Our emphasis throughout this series of lectures \n",
|
||||
@@ -57,6 +58,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Software and needed installations\n",
|
||||
"\n",
|
||||
"We will make extensive use of Python as programming language and its\n",
|
||||
@@ -91,6 +93,7 @@
|
||||
"\n",
|
||||
"etc etc. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Python installers\n",
|
||||
"\n",
|
||||
"If you don't want to perform these operations separately and venture\n",
|
||||
@@ -114,6 +117,7 @@
|
||||
"license.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Installing R, C++, cython or Julia\n",
|
||||
"\n",
|
||||
"You will also find it convenient to utilize R. Although we will mainly\n",
|
||||
@@ -132,6 +136,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Installing R, C++, cython, Numba etc\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -170,6 +175,7 @@
|
||||
"[doconce](https://github.com/hplgit/doconce) you can convert a standard ascii text file into various HTML \n",
|
||||
"formats, ipython notebooks, latex files, pdf files etc with minimal edits.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Simple linear regression model using **scikit-learn**\n",
|
||||
"\n",
|
||||
"We start with perhaps our simplest possible example, using **scikit-learn** to perform linear regression analysis on a data set produced by us. \n",
|
||||
@@ -582,6 +588,8 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Similarly, using **R**, we can perform similar studies. The following **R** code illustrates this.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Non-Linear Least squares in R"
|
||||
]
|
||||
},
|
||||
@@ -1573,6 +1581,111 @@
|
||||
"plt.grid(True)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Random walk model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from sklearn.preprocessing import PolynomialFeatures\n",
|
||||
"from sklearn.linear_model import LinearRegression\n",
|
||||
"\n",
|
||||
"steps=250\n",
|
||||
"\n",
|
||||
"distance=0\n",
|
||||
"x=0\n",
|
||||
"distance_list=[]\n",
|
||||
"steps_list=[]\n",
|
||||
"while x<steps:\n",
|
||||
" distance+=np.random.randint(-1,2)\n",
|
||||
" distance_list.append(distance)\n",
|
||||
" x+=1\n",
|
||||
" steps_list.append(x)\n",
|
||||
"plt.plot(steps_list,distance_list, color='green', label=\"Random Walk Data\")\n",
|
||||
"\n",
|
||||
"steps_list=np.asarray(steps_list)\n",
|
||||
"distance_list=np.asarray(distance_list)\n",
|
||||
"\n",
|
||||
"X=steps_list[:,np.newaxis]\n",
|
||||
"\n",
|
||||
"#Polynomial fits\n",
|
||||
"\n",
|
||||
"#Degree 2\n",
|
||||
"poly_features=PolynomialFeatures(degree=2, include_bias=False)\n",
|
||||
"X_poly=poly_features.fit_transform(X)\n",
|
||||
"\n",
|
||||
"lin_reg=LinearRegression()\n",
|
||||
"poly_fit=lin_reg.fit(X_poly,distance_list)\n",
|
||||
"b=lin_reg.coef_\n",
|
||||
"c=lin_reg.intercept_\n",
|
||||
"print (\"2nd degree coefficients:\")\n",
|
||||
"print (\"zero power: \",c)\n",
|
||||
"print (\"first power: \", b[0])\n",
|
||||
"print (\"second power: \",b[1])\n",
|
||||
"\n",
|
||||
"z = np.arange(0, steps, .01)\n",
|
||||
"z_mod=b[1]*z**2+b[0]*z+c\n",
|
||||
"\n",
|
||||
"fit_mod=b[1]*X**2+b[0]*X+c\n",
|
||||
"plt.plot(z, z_mod, color='r', label=\"2nd Degree Fit\")\n",
|
||||
"plt.title(\"Polynomial Regression\")\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"Steps\")\n",
|
||||
"plt.ylabel(\"Distance\")\n",
|
||||
"\n",
|
||||
"#Degree 10\n",
|
||||
"poly_features10=PolynomialFeatures(degree=10, include_bias=False)\n",
|
||||
"X_poly10=poly_features10.fit_transform(X)\n",
|
||||
"\n",
|
||||
"poly_fit10=lin_reg.fit(X_poly10,distance_list)\n",
|
||||
"\n",
|
||||
"y_plot=poly_fit10.predict(X_poly10)\n",
|
||||
"plt.plot(X, y_plot, color='black', label=\"10th Degree Fit\")\n",
|
||||
"\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"#Decision Tree Regression\n",
|
||||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||||
"regr_1=DecisionTreeRegressor(max_depth=2)\n",
|
||||
"regr_2=DecisionTreeRegressor(max_depth=5)\n",
|
||||
"regr_3=DecisionTreeRegressor(max_depth=7)\n",
|
||||
"regr_1.fit(X, distance_list)\n",
|
||||
"regr_2.fit(X, distance_list)\n",
|
||||
"regr_3.fit(X, distance_list)\n",
|
||||
"\n",
|
||||
"X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]\n",
|
||||
"y_1 = regr_1.predict(X_test)\n",
|
||||
"y_2 = regr_2.predict(X_test)\n",
|
||||
"y_3=regr_3.predict(X_test)\n",
|
||||
"\n",
|
||||
"# Plot the results\n",
|
||||
"plt.figure()\n",
|
||||
"plt.scatter(X, distance_list, s=2.5, c=\"black\", label=\"data\")\n",
|
||||
"plt.plot(X_test, y_1, color=\"red\",\n",
|
||||
" label=\"max_depth=2\", linewidth=2)\n",
|
||||
"plt.plot(X_test, y_2, color=\"green\", label=\"max_depth=5\", linewidth=2)\n",
|
||||
"plt.plot(X_test, y_3, color=\"m\", label=\"max_depth=7\", linewidth=2)\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"Data\")\n",
|
||||
"plt.ylabel(\"Darget\")\n",
|
||||
"plt.title(\"Decision Tree Regression\")\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -3,7 +3,7 @@ AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of
|
||||
DATE: today
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Introduction =====
|
||||
|
||||
Our emphasis throughout this series of lectures
|
||||
@@ -44,7 +44,7 @@ get started with programming.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Software and needed installations =====
|
||||
|
||||
We will make extensive use of Python as programming language and its
|
||||
@@ -79,7 +79,7 @@ o sudo apt-get install python3 (or python for pyhton2.7)
|
||||
|
||||
etc etc.
|
||||
|
||||
!split
|
||||
|
||||
===== Python installers =====
|
||||
|
||||
If you don't want to perform these operations separately and venture
|
||||
@@ -103,7 +103,7 @@ analysis environment, available for free and under a commercial
|
||||
license.
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Installing R, C++, cython or Julia =====
|
||||
|
||||
You will also find it convenient to utilize R. Although we will mainly
|
||||
@@ -122,7 +122,7 @@ To install _R_ with Jupyter notebook
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Installing R, C++, cython, Numba etc =====
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ Finally, if you wish to use the light mark-up language
|
||||
"doconce":"https://github.com/hplgit/doconce" you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
|
||||
|
||||
!split
|
||||
|
||||
===== Simple linear regression model using _scikit-learn_ =====
|
||||
|
||||
We start with perhaps our simplest possible example, using _scikit-learn_ to perform linear regression analysis on a data set produced by us.
|
||||
@@ -430,7 +430,8 @@ print (error(y))
|
||||
!ec
|
||||
|
||||
Similarly, using _R_, we can perform similar studies. The following _R_ code illustrates this.
|
||||
!split
|
||||
|
||||
|
||||
===== Non-Linear Least squares in R =====
|
||||
!bblock
|
||||
!bc r
|
||||
@@ -473,7 +474,7 @@ display(data_pandas)
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Examples =====
|
||||
|
||||
We present here several examples, with pertinent Python codes that we
|
||||
@@ -1026,3 +1027,96 @@ plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
=== Random walk model ===
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
steps=250
|
||||
|
||||
distance=0
|
||||
x=0
|
||||
distance_list=[]
|
||||
steps_list=[]
|
||||
while x<steps:
|
||||
distance+=np.random.randint(-1,2)
|
||||
distance_list.append(distance)
|
||||
x+=1
|
||||
steps_list.append(x)
|
||||
plt.plot(steps_list,distance_list, color='green', label="Random Walk Data")
|
||||
|
||||
steps_list=np.asarray(steps_list)
|
||||
distance_list=np.asarray(distance_list)
|
||||
|
||||
X=steps_list[:,np.newaxis]
|
||||
|
||||
#Polynomial fits
|
||||
|
||||
#Degree 2
|
||||
poly_features=PolynomialFeatures(degree=2, include_bias=False)
|
||||
X_poly=poly_features.fit_transform(X)
|
||||
|
||||
lin_reg=LinearRegression()
|
||||
poly_fit=lin_reg.fit(X_poly,distance_list)
|
||||
b=lin_reg.coef_
|
||||
c=lin_reg.intercept_
|
||||
print ("2nd degree coefficients:")
|
||||
print ("zero power: ",c)
|
||||
print ("first power: ", b[0])
|
||||
print ("second power: ",b[1])
|
||||
|
||||
z = np.arange(0, steps, .01)
|
||||
z_mod=b[1]*z**2+b[0]*z+c
|
||||
|
||||
fit_mod=b[1]*X**2+b[0]*X+c
|
||||
plt.plot(z, z_mod, color='r', label="2nd Degree Fit")
|
||||
plt.title("Polynomial Regression")
|
||||
|
||||
plt.xlabel("Steps")
|
||||
plt.ylabel("Distance")
|
||||
|
||||
#Degree 10
|
||||
poly_features10=PolynomialFeatures(degree=10, include_bias=False)
|
||||
X_poly10=poly_features10.fit_transform(X)
|
||||
|
||||
poly_fit10=lin_reg.fit(X_poly10,distance_list)
|
||||
|
||||
y_plot=poly_fit10.predict(X_poly10)
|
||||
plt.plot(X, y_plot, color='black', label="10th Degree Fit")
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
#Decision Tree Regression
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
regr_1=DecisionTreeRegressor(max_depth=2)
|
||||
regr_2=DecisionTreeRegressor(max_depth=5)
|
||||
regr_3=DecisionTreeRegressor(max_depth=7)
|
||||
regr_1.fit(X, distance_list)
|
||||
regr_2.fit(X, distance_list)
|
||||
regr_3.fit(X, distance_list)
|
||||
|
||||
X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
y_3=regr_3.predict(X_test)
|
||||
|
||||
# Plot the results
|
||||
plt.figure()
|
||||
plt.scatter(X, distance_list, s=2.5, c="black", label="data")
|
||||
plt.plot(X_test, y_1, color="red",
|
||||
label="max_depth=2", linewidth=2)
|
||||
plt.plot(X_test, y_2, color="green", label="max_depth=5", linewidth=2)
|
||||
plt.plot(X_test, y_3, color="m", label="max_depth=7", linewidth=2)
|
||||
|
||||
plt.xlabel("Data")
|
||||
plt.ylabel("Darget")
|
||||
plt.title("Decision Tree Regression")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
Reference in New Issue
Block a user