50471e835b
nothing about random forests
383 lines
27 KiB
HTML
383 lines
27 KiB
HTML
<!--
|
|
Automatically generated HTML file from DocOnce source
|
|
(https://github.com/hplgit/doconce/)
|
|
-->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
|
<meta name="description" content="Data Analysis and Machine Learning: Trees, forests and all that">
|
|
|
|
<title>Data Analysis and Machine Learning: Trees, forests and all that</title>
|
|
|
|
|
|
<style type="text/css">
|
|
/* bloodish style */
|
|
|
|
body {
|
|
font-family: Helvetica, Verdana, Arial, Sans-serif;
|
|
color: #404040;
|
|
background: #ffffff;
|
|
}
|
|
h1 { font-size: 1.8em; color: #8A0808; }
|
|
h2 { font-size: 1.6em; color: #8A0808; }
|
|
h3 { font-size: 1.4em; color: #8A0808; }
|
|
h4 { color: #8A0808; }
|
|
a { color: #8A0808; text-decoration:none; }
|
|
tt { font-family: "Courier New", Courier; }
|
|
/* pre style removed because it will interfer with pygments */
|
|
p { text-indent: 0px; }
|
|
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
|
|
p.caption { width: 80%; font-style: normal; text-align: left; }
|
|
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa}
|
|
.alert-text-small { font-size: 80%; }
|
|
.alert-text-large { font-size: 130%; }
|
|
.alert-text-normal { font-size: 90%; }
|
|
.alert {
|
|
padding:8px 35px 8px 14px; margin-bottom:18px;
|
|
text-shadow:0 1px 0 rgba(255,255,255,0.5);
|
|
border:1px solid #bababa;
|
|
border-radius: 4px;
|
|
-webkit-border-radius: 4px;
|
|
-moz-border-radius: 4px;
|
|
color: #555;
|
|
background-color: #f8f8f8;
|
|
background-position: 10px 5px;
|
|
background-repeat: no-repeat;
|
|
background-size: 38px;
|
|
padding-left: 55px;
|
|
width: 75%;
|
|
}
|
|
.alert-block {padding-top:14px; padding-bottom:14px}
|
|
.alert-block > p, .alert-block > ul {margin-bottom:1em}
|
|
.alert li {margin-top: 1em}
|
|
.alert-block p+p {margin-top:5px}
|
|
.alert-notice { background-image: url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_gray_notice.png); }
|
|
.alert-summary { background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_gray_summary.png); }
|
|
.alert-warning { background-image: url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_gray_warning.png); }
|
|
.alert-question {background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_gray_question.png); }
|
|
|
|
div { text-align: justify; text-justify: inter-word; }
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Decision trees, overarching aims', 2, None, '___sec0'),
|
|
('Nodes, leafs, roots and branches', 2, None, '___sec1'),
|
|
('How do we set it up?', 2, None, '___sec2'),
|
|
('Decision trees and Regression', 2, None, '___sec3'),
|
|
('Maxwell-Boltzmann velocity distribution', 2, None, '___sec4')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
<!-- ------------------- main content ---------------------- -->
|
|
|
|
|
|
|
|
<center><h1>Data Analysis and Machine Learning: Trees, forests and all that</h1></center> <!-- document title -->
|
|
|
|
<p>
|
|
<!-- author(s): Morten Hjorth-Jensen -->
|
|
|
|
<center>
|
|
<b>Morten Hjorth-Jensen</b> [1, 2]
|
|
</center>
|
|
|
|
<p>
|
|
<!-- institution(s) -->
|
|
|
|
<center>[1] <b>Department of Physics, University of Oslo</b></center>
|
|
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
|
<br>
|
|
<p>
|
|
<center><h4>Nov 2, 2018</h4></center> <!-- date -->
|
|
<br>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec0">Decision trees, overarching aims </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
|
|
<p>
|
|
Decision trees are supervised learning algorithms used for both,
|
|
classification and regression tasks where we will concentrate on
|
|
classification in this first part of our decision tree tutorial.
|
|
Decision trees are assigned to the information based learning
|
|
algorithms which use different measures of information gain for
|
|
learning. We can use decision trees for issues where we have
|
|
continuous but also categorical input and target features.
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split -->
|
|
|
|
<h2 id="___sec1">Nodes, leafs, roots and branches </h2>
|
|
|
|
<p>
|
|
The main idea of decision trees
|
|
is to find those descriptive features which contain the most
|
|
<b>information</b> regarding the target feature and then split the dataset
|
|
along the values of these features such that the target feature values
|
|
for the resulting sub\_datasets are as pure as possible.
|
|
|
|
<p>
|
|
The descriptive feature which leaves the target feature most purely is said
|
|
to be the most informative one. This process of finding the <b>most
|
|
informative</b> feature is done until we accomplish a stopping criteria
|
|
where we then finally end up in so called <b>leaf nodes</b>.
|
|
|
|
<p>
|
|
The leaf nodes
|
|
contain the predictions we will make for new query instances presented
|
|
to our trained model. This is possible since the model has kind of
|
|
learned the underlying structure of the training data and hence can,
|
|
given some assumptions, make predictions about the target feature value
|
|
(class) of unseen query instances.
|
|
|
|
<p>
|
|
A decision tree mainly contains of a <b>root node</b>, <b>interior nodes</b>,
|
|
and <b>leaf nodes</b> which are then connected by <b>branches</b>.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec2">How do we set it up? </h2>
|
|
|
|
<p>
|
|
In simplified terms, the process of training a decision tree and
|
|
predicting the target features of query instances is as follows:
|
|
|
|
<ol>
|
|
<li> Present a dataset containing of a number of training instances characterized by a number of descriptive features and a target feature</li>
|
|
<li> Train the decision tree model by continuously splitting the target feature along the values of the descriptive features using a measure of information gain during the training process</li>
|
|
<li> Grow the tree until we accomplish a stopping criteria create leaf nodes which represent the <em>predictions</em> we want to make for new query instances</li>
|
|
<li> Show query instances to the tree and run down the tree until we arrive at leaf nodes</li>
|
|
</ol>
|
|
|
|
Then we are essentially done!
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec3">Decision trees and Regression </h2>
|
|
<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>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec4">Maxwell-Boltzmann velocity distribution </h2>
|
|
|
|
<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: #408080; font-style: italic"># Program to test the Metropolis algorithm with one particle at given temp in</span>
|
|
<span style="color: #408080; font-style: italic"># one dimension</span>
|
|
<span style="color: #408080; font-style: italic">#!/usr/bin/env python</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.mlab</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">mlab</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">import</span> <span style="color: #0000FF; font-weight: bold">random</span>
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">math</span> <span style="color: #008000; font-weight: bold">import</span> sqrt, exp, log
|
|
<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
|
|
<span style="color: #408080; font-style: italic"># initialize the rng with a seed</span>
|
|
random<span style="color: #666666">.</span>seed()
|
|
<span style="color: #408080; font-style: italic"># Hard coding of input parameters</span>
|
|
MCcycles <span style="color: #666666">=</span> <span style="color: #666666">100000</span>
|
|
Temperature <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
|
|
beta <span style="color: #666666">=</span> <span style="color: #666666">1./</span>Temperature
|
|
InitialVelocity <span style="color: #666666">=</span> <span style="color: #666666">-2.0</span>
|
|
CurrentVelocity <span style="color: #666666">=</span> InitialVelocity
|
|
Energy <span style="color: #666666">=</span> <span style="color: #666666">0.5*</span>InitialVelocity<span style="color: #666666">*</span>InitialVelocity
|
|
VelocityRange <span style="color: #666666">=</span> <span style="color: #666666">10*</span>sqrt(Temperature)
|
|
VelocityStep <span style="color: #666666">=</span> <span style="color: #666666">2*</span>VelocityRange<span style="color: #666666">/10.</span>
|
|
AverageEnergy <span style="color: #666666">=</span> Energy
|
|
AverageEnergy2 <span style="color: #666666">=</span> Energy<span style="color: #666666">*</span>Energy
|
|
VelocityValues <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(MCcycles)
|
|
<span style="color: #408080; font-style: italic"># The Monte Carlo sampling with Metropolis starts here</span>
|
|
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span> (<span style="color: #666666">1</span>, MCcycles, <span style="color: #666666">1</span>):
|
|
TrialVelocity <span style="color: #666666">=</span> CurrentVelocity <span style="color: #666666">+</span> (<span style="color: #666666">2.0*</span>random<span style="color: #666666">.</span>random() <span style="color: #666666">-</span> <span style="color: #666666">1.0</span>)<span style="color: #666666">*</span>VelocityStep
|
|
EnergyChange <span style="color: #666666">=</span> <span style="color: #666666">0.5*</span>(TrialVelocity<span style="color: #666666">*</span>TrialVelocity <span style="color: #666666">-</span>CurrentVelocity<span style="color: #666666">*</span>CurrentVelocity);
|
|
<span style="color: #008000; font-weight: bold">if</span> random<span style="color: #666666">.</span>random() <span style="color: #666666"><=</span> exp(<span style="color: #666666">-</span>beta<span style="color: #666666">*</span>EnergyChange):
|
|
CurrentVelocity <span style="color: #666666">=</span> TrialVelocity
|
|
Energy <span style="color: #666666">+=</span> EnergyChange
|
|
VelocityValues[i] <span style="color: #666666">=</span> CurrentVelocity
|
|
AverageEnergy <span style="color: #666666">+=</span> Energy
|
|
AverageEnergy2 <span style="color: #666666">+=</span> Energy<span style="color: #666666">*</span>Energy
|
|
<span style="color: #408080; font-style: italic">#Final averages</span>
|
|
AverageEnergy <span style="color: #666666">=</span> AverageEnergy<span style="color: #666666">/</span>MCcycles
|
|
AverageEnergy2 <span style="color: #666666">=</span> AverageEnergy2<span style="color: #666666">/</span>MCcycles
|
|
Variance <span style="color: #666666">=</span> AverageEnergy2 <span style="color: #666666">-</span> AverageEnergy<span style="color: #666666">*</span>AverageEnergy
|
|
<span style="color: #008000; font-weight: bold">print</span>(AverageEnergy, Variance)
|
|
n, bins, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(VelocityValues, <span style="color: #666666">400</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">'green'</span>)
|
|
|
|
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'$v$'</span>)
|
|
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'Velocity distribution P(v)'</span>)
|
|
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r'Velocity histogram at $k_BT=2$'</span>)
|
|
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-5</span>, <span style="color: #666666">5</span>, <span style="color: #666666">0</span>, <span style="color: #666666">600</span>])
|
|
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>)
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">collections</span> <span style="color: #008000; font-weight: bold">import</span> Counter
|
|
|
|
<span style="color: #408080; font-style: italic">#print (Counter(VelocityValues))</span>
|
|
|
|
<span style="color: #008000; font-weight: bold">print</span> (VelocityValues[:<span style="color: #666666">20</span>])
|
|
VelocityValues<span style="color: #666666">=</span><span style="color: #008000">list</span>(Counter(VelocityValues)<span style="color: #666666">.</span>keys())
|
|
d<span style="color: #666666">=</span><span style="color: #008000">list</span>(Counter(VelocityValues)<span style="color: #666666">.</span>values())
|
|
|
|
VelocityValues<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(VelocityValues)[:, np<span style="color: #666666">.</span>newaxis]
|
|
d<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(d)
|
|
<span style="color: #008000; font-weight: bold">print</span> (VelocityValues<span style="color: #666666">.</span>shape, d<span style="color: #666666">.</span>shape)
|
|
|
|
plt<span style="color: #666666">.</span>scatter(VelocityValues, d)
|
|
plt<span style="color: #666666">.</span>show()
|
|
|
|
<span style="color: #408080; font-style: italic">#2nd Degree Polynomial</span>
|
|
poly_feat<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=20</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
|
X_poly<span style="color: #666666">=</span>poly_feat<span style="color: #666666">.</span>fit_transform(VelocityValues)
|
|
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,d)
|
|
|
|
y_plot<span style="color: #666666">=</span>poly_fit<span style="color: #666666">.</span>predict(X_poly)
|
|
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Polynomial Fit"</span>)
|
|
plt<span style="color: #666666">.</span>plot(VelocityValues, y_plot, color<span style="color: #666666">=</span><span style="color: #BA2121">'black'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Fit"</span>)
|
|
plt<span style="color: #666666">.</span>show()
|
|
|
|
<span style="color: #408080; font-style: italic">#Decision Trees</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(VelocityValues, d)
|
|
regr_2<span style="color: #666666">.</span>fit(VelocityValues, d)
|
|
regr_3<span style="color: #666666">.</span>fit(VelocityValues, d)
|
|
|
|
X_test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0.0</span>, MCcycles, <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)
|
|
|
|
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Decision Tree"</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>show()
|
|
|
|
<span style="color: #408080; font-style: italic">#Separate each frequency not in one specific velocity, but in a range of values,</span>
|
|
<span style="color: #408080; font-style: italic">#i.e. frequency of all velocities in range -5 to -4.9, -4.9 to -4.8, etc...</span>
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- ------------------- end of main content --------------- -->
|
|
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
|
</center>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|
|
|