1735 lines
112 KiB
HTML
1735 lines
112 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="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Week 44: From Decision Trees to Bagging methods">
|
|
|
|
<title>Week 44: From Decision Trees to Bagging methods</title>
|
|
|
|
|
|
<link href="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/>
|
|
<script src="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/js/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
<link href="https://thomasf.github.io/solarized-css/solarized-light.min.css" rel="stylesheet">
|
|
<style type="text/css">
|
|
h1 {color: #b58900;} /* yellow */
|
|
/* h1 {color: #cb4b16;} orange */
|
|
/* h1 {color: #d33682;} magenta, the original choice of thomasf */
|
|
code { padding: 0px; background-color: inherit; }
|
|
pre {
|
|
border: 0pt solid #93a1a1;
|
|
box-shadow: none;
|
|
}
|
|
.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 #93a1a1;
|
|
border-radius: 4px;
|
|
-webkit-border-radius: 4px;
|
|
-moz-border-radius: 4px;
|
|
color: #555;
|
|
background-color: #eee8d5;
|
|
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_yellow_notice.png); }
|
|
.alert-summary { background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_summary.png); }
|
|
.alert-warning { background-image: url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_warning.png); }
|
|
.alert-question {background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_question.png); }
|
|
|
|
div { text-align: justify; text-justify: inter-word; }
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Overview of week 44', 2, None, '___sec0'),
|
|
('Thursday', 2, None, '___sec1'),
|
|
('Decision trees, overarching aims', 2, None, '___sec2'),
|
|
('Basics of a tree', 2, None, '___sec3'),
|
|
('A Sketch of a Tree, Regression problem', 2, None, '___sec4'),
|
|
('A Sketch of a Tree, Classification problem',
|
|
2,
|
|
None,
|
|
'___sec5'),
|
|
('A typical Decision Tree with its pertinent Jargon, '
|
|
'Classification Problem',
|
|
2,
|
|
None,
|
|
'___sec6'),
|
|
('General Features', 2, None, '___sec7'),
|
|
('How do we set it up?', 2, None, '___sec8'),
|
|
('Decision trees and Regression', 2, None, '___sec9'),
|
|
('Building a tree, regression', 2, None, '___sec10'),
|
|
('A top-down approach, recursive binary splitting',
|
|
2,
|
|
None,
|
|
'___sec11'),
|
|
('Making a tree', 2, None, '___sec12'),
|
|
('Pruning the tree', 2, None, '___sec13'),
|
|
('Cost complexity pruning', 2, None, '___sec14'),
|
|
('Schematic Regression Procedure', 2, None, '___sec15'),
|
|
('A Classification Tree', 2, None, '___sec16'),
|
|
('Growing a classification tree', 2, None, '___sec17'),
|
|
('Classification tree, how to split nodes', 2, None, '___sec18'),
|
|
('Visualizing the Tree, Classification', 2, None, '___sec19'),
|
|
('Visualizing the Tree, The Moons', 2, None, '___sec20'),
|
|
('Other ways of visualizing the trees', 2, None, '___sec21'),
|
|
('Printing out as text', 2, None, '___sec22'),
|
|
('Algorithms for Setting up Decision Trees', 2, None, '___sec23'),
|
|
('The CART algorithm for Classification', 2, None, '___sec24'),
|
|
('The CART algorithm for Regression', 2, None, '___sec25'),
|
|
('Computing the Gini index', 2, None, '___sec26'),
|
|
('Simple Python Code to read in Data and perform Classification',
|
|
2,
|
|
None,
|
|
'___sec27'),
|
|
('Computing the Gini Factor', 2, None, '___sec28'),
|
|
('Entropy and the ID3 algorithm', 2, None, '___sec29'),
|
|
('Cancer Data again now with Decision Trees and other Methods',
|
|
2,
|
|
None,
|
|
'___sec30'),
|
|
('Another example, the moons again', 2, None, '___sec31'),
|
|
('Playing around with regions', 2, None, '___sec32'),
|
|
('Regression trees', 2, None, '___sec33'),
|
|
('Final regressor code', 2, None, '___sec34'),
|
|
('Pros and cons of trees, pros', 2, None, '___sec35'),
|
|
('Disadvantages', 2, None, '___sec36'),
|
|
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
|
|
'Boosting, Meet the Jungle of Methods',
|
|
2,
|
|
None,
|
|
'___sec37'),
|
|
('An Overview of Ensemble Methods', 2, None, '___sec38'),
|
|
('Bagging', 2, None, '___sec39'),
|
|
('More bagging', 2, None, '___sec40'),
|
|
('Simple Voting Example, head or tail', 2, None, '___sec41'),
|
|
('Using the Voting Classifier', 2, None, '___sec42'),
|
|
('Please, not the moons again! Voting and Bagging',
|
|
2,
|
|
None,
|
|
'___sec43'),
|
|
('Bagging Examples', 2, None, '___sec44'),
|
|
('Making your own Bootstrap: Changing the Level of the Decision '
|
|
'Tree',
|
|
2,
|
|
None,
|
|
'___sec45')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
TeX: {
|
|
equationNumbers: { autoNumber: "AMS" },
|
|
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
|
}
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- ------------------- main content ---------------------- -->
|
|
|
|
|
|
|
|
<center><h1>Week 44: From Decision Trees to Bagging methods</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 5, 2020</h4></center> <!-- date -->
|
|
<br>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec0">Overview of week 44 </h2>
|
|
|
|
<ul>
|
|
<li> <a href="https://www.uio.no/studier/emner/matnat/fys/FYS-STK4155/h20/forelesningsvideoer/LectureOctober29.mp4?vrtx=view-as-webpage" target="_blank">Thursday: Wrapping up PCA from last week and basics of decision trees, classification and regression algorithms with video of lecture</a></li>
|
|
<li> <a href="https://www.uio.no/studier/emner/matnat/fys/FYS-STK4155/h20/forelesningsvideoer/LectureOctober30.mp4?vrtx=view-as-webpage" target="_blank">Friday: Decision trees, voting models and bagging with video of lecture</a></li>
|
|
</ul>
|
|
|
|
Geron's chapter 6 covers decision trees while ensemble models, voting and bagging are discussed in chapter 7. See also lecture from <a href="https://www.uio.no/studier/emner/matnat/math/STK-IN4300/h20/slides/lecture_7.pdf" target="_blank">STK-IN4300, lecture 7</a>. Chapter 9.2 of Hastie et al contains also a good discussion.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec1">Thursday </h2>
|
|
|
|
<p>
|
|
Decision trees and wrapping up PCA.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec2">Decision trees, overarching aims </h2>
|
|
|
|
<p>
|
|
We start here with the most basic algorithm, the so-called decision
|
|
tree. With this basic algorithm we can in turn build more complex
|
|
networks, spanning from homogeneous and heterogenous forests (bagging,
|
|
random forests and more) to one of the most popular supervised
|
|
algorithms nowadays, the extreme gradient boosting, or just
|
|
XGBoost. But let us start with the simplest possible ingredient.
|
|
|
|
<p>
|
|
Decision trees are supervised learning algorithms used for both,
|
|
classification and regression tasks.
|
|
|
|
<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 underlying datasets are as pure as possible.
|
|
|
|
<p>
|
|
The descriptive features which reproduce best the target/output features are normally said
|
|
to be the most informative ones. The 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>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec3">Basics of a tree </h2>
|
|
|
|
<p>
|
|
A decision tree is typically divided into a <b>root node</b>, the <b>interior nodes</b>,
|
|
and the final <b>leaf nodes</b> or just <b>leaves</b>. These entities are then connected by so-called <b>branches</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
|
|
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>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec4">A Sketch of a Tree, Regression problem </h2>
|
|
|
|
<p>
|
|
<!-- FIGURE: [DataFiles/Regsimpletree.png, width=600 frac=0.8] -->
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec5">A Sketch of a Tree, Classification problem </h2>
|
|
|
|
<p>
|
|
<!-- FIGURE: [DataFiles/Classimpletree.png, width=600 frac=0.8] -->
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec6">A typical Decision Tree with its pertinent Jargon, Classification Problem </h2>
|
|
|
|
<p>
|
|
<br /><br /><center><p><img src="DataFiles/cancer.png" align="bottom" width=600></p></center><br /><br />
|
|
|
|
<p>
|
|
This tree was produced using the Wisconsin cancer data (discussed here as well, see code examples below) using <b>Scikit-Learn</b>'s decision tree classifier. Here we have used the so-called <b>gini</b> index (see below) to split the various branches.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec7">General Features </h2>
|
|
|
|
<p>
|
|
The overarching approach to decision trees is a top-down approach.
|
|
|
|
<ul>
|
|
<li> A leaf provides the classification of a given instance.</li>
|
|
<li> A node specifies a test of some attribute of the instance.</li>
|
|
<li> A branch corresponds to a possible values of an attribute.</li>
|
|
<li> An instance is classified by starting at the root node of the tree, testing the attribute specified by this node, then moving down the tree branch corresponding to the value of the attribute in the given example.</li>
|
|
</ul>
|
|
|
|
This process is then repeated for the subtree rooted at the new
|
|
node.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec8">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="___sec9">Decision trees and Regression </h2>
|
|
<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: #8B008B; font-weight: bold">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: #658b00">print</span> (<span style="color: #CD5555">"2nd degree coefficients:"</span>)
|
|
<span style="color: #658b00">print</span> (<span style="color: #CD5555">"zero power: "</span>,c)
|
|
<span style="color: #658b00">print</span> (<span style="color: #CD5555">"first power: "</span>, b[<span style="color: #B452CD">0</span>])
|
|
<span style="color: #658b00">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: #8B008B; font-weight: bold">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>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec10">Building a tree, regression </h2>
|
|
|
|
<p>
|
|
There are mainly two steps
|
|
|
|
<ol>
|
|
<li> We split the predictor space (the set of possible values \( x_1,x_2,\dots, x_p \)) into \( J \) distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).</li>
|
|
<li> For every observation that falls into the region \( R_j \) , we make the same prediction, which is simply the mean of the response values for the training observations in \( R_j \).</li>
|
|
</ol>
|
|
|
|
How do we construct the regions \( R_1,\dots,R_J \)? In theory, the
|
|
regions could have any shape. However, we choose to divide the
|
|
predictor space into high-dimensional rectangles, or boxes, for
|
|
simplicity and for ease of interpretation of the resulting predictive
|
|
model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the
|
|
MSE, given by
|
|
|
|
$$
|
|
\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2,
|
|
$$
|
|
|
|
<p>
|
|
where \( \overline{y}_{R_j} \) is the mean response for the training observations
|
|
within box \( j \).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec11">A top-down approach, recursive binary splitting </h2>
|
|
|
|
<p>
|
|
Unfortunately, it is computationally infeasible to consider every
|
|
possible partition of the feature space into \( J \) boxes. The common
|
|
strategy is to take a top-down approach
|
|
|
|
<p>
|
|
The approach is top-down because it begins at the top of the tree (all
|
|
observations belong to a single region) and then successively splits
|
|
the predictor space; each split is indicated via two new branches
|
|
further down on the tree. It is greedy because at each step of the
|
|
tree-building process, the best split is made at that particular step,
|
|
rather than looking ahead and picking a split that will lead to a
|
|
better tree in some future step.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec12">Making a tree </h2>
|
|
|
|
<p>
|
|
In order to implement the recursive binary splitting we start by selecting
|
|
the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \)
|
|
$$
|
|
\left\{X\vert x_j < s\right\},
|
|
$$
|
|
|
|
and
|
|
$$
|
|
\left\{X\vert x_j \geq s\right\},
|
|
$$
|
|
|
|
so that we obtain the lowest MSE, that is
|
|
$$
|
|
\sum_{i:x_i\in R_j}(y_i-\overline{y}_{R_1})^2+\sum_{i:x_i\in R_2}(y_i-\overline{y}_{R_2})^2,
|
|
$$
|
|
|
|
<p>
|
|
which we want to minimize by considering all predictors
|
|
\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for
|
|
each predictor. These values could be determined by randomly assigned
|
|
numbers or by starting at the midpoint and then proceed till we find
|
|
an optimal value.
|
|
|
|
<p>
|
|
For any \( j \) and \( s \), we define the pair of half-planes where
|
|
\( \overline{y}_{R_1} \) is the mean response for the training
|
|
observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean
|
|
response for the training observations in \( R_2(j,s) \).
|
|
|
|
<p>
|
|
Finding the values of \( j \) and \( s \) that minimize the above equation can be
|
|
done quite quickly, especially when the number of features \( p \) is not
|
|
too large.
|
|
|
|
<p>
|
|
Next, we repeat the process, looking
|
|
for the best predictor and best cutpoint in order to split the data
|
|
further so as to minimize the MSE within each of the resulting
|
|
regions. However, this time, instead of splitting the entire predictor
|
|
space, we split one of the two previously identified regions. We now
|
|
have three regions. Again, we look to split one of these three regions
|
|
further, so as to minimize the MSE. The process continues until a
|
|
stopping criterion is reached; for instance, we may continue until no
|
|
region contains more than five observations.
|
|
|
|
<p>
|
|
<!-- !split -->
|
|
|
|
<h2 id="___sec13">Pruning the tree </h2>
|
|
|
|
<p>
|
|
The above procedure is rather straightforward, but leads often to
|
|
overfitting and unnecessarily large and complicated trees. The basic
|
|
idea is to grow a large tree \( T_0 \) and then prune it back in order to
|
|
obtain a subtree. A smaller tree with fewer splits (fewer regions) can
|
|
lead to smaller variance and better interpretation at the cost of a
|
|
little more bias.
|
|
|
|
<p>
|
|
The so-called Cost complexity pruning algorithm gives us a
|
|
way to do just this. Rather than considering every possible subtree,
|
|
we consider a sequence of trees indexed by a nonnegative tuning
|
|
parameter \( \alpha \).
|
|
|
|
<p>
|
|
Read more at the following <a href="https://scikit-learn.org/stable/auto_examples/tree/plot_cost_complexity_pruning.html#sphx-glr-auto-examples-tree-plot-cost-complexity-pruning-py" target="_blank">Scikit-Learn link on pruning</a>.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec14">Cost complexity pruning </h2>
|
|
|
|
<p>
|
|
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
|
$$
|
|
\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T},
|
|
$$
|
|
|
|
is as small as possible. Here \( \overline{T} \) is
|
|
the number of terminal nodes of the tree \( T \) , \( R_m \) is the
|
|
rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node.
|
|
|
|
<p>
|
|
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s
|
|
complexity and its fit to the training data. When \( \alpha = 0 \), then the
|
|
subtree \( T \) will simply equal \( T_0 \),
|
|
because then the above equation just measures the
|
|
training error.
|
|
However, as \( \alpha \) increases, there is a price to pay for
|
|
having a tree with many terminal nodes. The above equation will
|
|
tend to be minimized for a smaller subtree.
|
|
|
|
<p>
|
|
It turns out that as we increase \( \alpha \) from zero
|
|
branches get pruned from the tree in a nested and predictable fashion,
|
|
so obtaining the whole sequence of subtrees as a function of \( \alpha \) is
|
|
easy. We can select a value of \( \alpha \) using a validation set or using
|
|
cross-validation. We then return to the full data set and obtain the
|
|
subtree corresponding to \( \alpha \).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec15">Schematic Regression Procedure </h2>
|
|
|
|
<p>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Building a Regression Tree.</b>
|
|
<p>
|
|
|
|
<ol>
|
|
<li> Use recursive binary splitting to grow a large tree on the training data, stopping only when each terminal node has fewer than some minimum number of observations.</li>
|
|
<li> Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).</li>
|
|
<li> Use for example \( K \)-fold cross-validation to choose \( \alpha \). Divide the training observations into \( K \) folds. For each \( k=1,2,\dots,K \) we:</li>
|
|
|
|
<ul>
|
|
<li> repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.</li>
|
|
<li> Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).</li>
|
|
<li> Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.</li>
|
|
</ul>
|
|
|
|
<li> Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).</li>
|
|
</ol>
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec16">A Classification Tree </h2>
|
|
|
|
<p>
|
|
A classification tree is very similar to a regression tree, except
|
|
that it is used to predict a qualitative response rather than a
|
|
quantitative one. Recall that for a regression tree, the predicted
|
|
response for an observation is given by the mean response of the
|
|
training observations that belong to the same terminal node. In
|
|
contrast, for a classification tree, we predict that each observation
|
|
belongs to the most commonly occurring class of training observations
|
|
in the region to which it belongs. In interpreting the results of a
|
|
classification tree, we are often interested not only in the class
|
|
prediction corresponding to a particular terminal node region, but
|
|
also in the class proportions among the training observations that
|
|
fall into that region.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec17">Growing a classification tree </h2>
|
|
|
|
<p>
|
|
The task of growing a
|
|
classification tree is quite similar to the task of growing a
|
|
regression tree. Just as in the regression setting, we use recursive
|
|
binary splitting to grow a classification tree. However, in the
|
|
classification setting, the MSE cannot be used as a criterion for making
|
|
the binary splits. A natural alternative to MSE is the <b>classification
|
|
error rate</b>. Since we plan to assign an observation in a given region
|
|
to the most commonly occurring error rate class of training
|
|
observations in that region, the classification error rate is simply
|
|
the fraction of the training observations in that region that do not
|
|
belong to the most common class.
|
|
|
|
<p>
|
|
When building a classification tree, either the Gini index or the
|
|
entropy are typically used to evaluate the quality of a particular
|
|
split, since these two approaches are more sensitive to node purity
|
|
than is the classification error rate.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec18">Classification tree, how to split nodes </h2>
|
|
|
|
<p>
|
|
If our targets are the outcome of a classification process that takes
|
|
for example \( k=1,2,\dots,K \) values, the only thing we need to think of
|
|
is to set up the splitting criteria for each node.
|
|
|
|
<p>
|
|
We define a PDF \( p_{mk} \) that represents the number of observations of
|
|
a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent
|
|
this likelihood function in terms of the proportion \( I(y_i=k) \) of
|
|
observations of this class in the region \( R_m \) as
|
|
|
|
$$
|
|
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k).
|
|
$$
|
|
|
|
<p>
|
|
We let \( p_{mk} \) represent the majority class of observations in region
|
|
\( m \). The three most common ways of splitting a node are given by
|
|
|
|
<ul>
|
|
<li> Misclassification error</li>
|
|
</ul>
|
|
|
|
$$
|
|
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}.
|
|
$$
|
|
|
|
|
|
<ul>
|
|
<li> Gini index \( g \)</li>
|
|
</ul>
|
|
|
|
$$
|
|
g = \sum_{k=1}^K p_{mk}(1-p_{mk}).
|
|
$$
|
|
|
|
|
|
<ul>
|
|
<li> Information entropy or just entropy \( s \)</li>
|
|
</ul>
|
|
|
|
$$
|
|
s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}.
|
|
$$
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec19">Visualizing the Tree, Classification </h2>
|
|
<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">os</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
|
<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> DecisionTreeClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> confusion_matrix
|
|
<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> export_graphviz
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</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>
|
|
|
|
|
|
cancer = load_breast_cancer()
|
|
X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
|
<span style="color: #658b00">print</span>(X)
|
|
y = pd.Categorical.from_codes(cancer.target, cancer.target_names)
|
|
y = pd.get_dummies(y)
|
|
<span style="color: #658b00">print</span>(y)
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=<span style="color: #B452CD">1</span>)
|
|
tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #B452CD">5</span>)
|
|
tree_clf.fit(X_train, y_train)
|
|
|
|
export_graphviz(
|
|
tree_clf,
|
|
out_file=<span style="color: #CD5555">"DataFiles/cancer.dot"</span>,
|
|
feature_names=cancer.feature_names,
|
|
class_names=cancer.target_names,
|
|
rounded=<span style="color: #8B008B; font-weight: bold">True</span>,
|
|
filled=<span style="color: #8B008B; font-weight: bold">True</span>
|
|
)
|
|
cmd = <span style="color: #CD5555">'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'</span>
|
|
os.system(cmd)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec20">Visualizing the Tree, The Moons </h2>
|
|
<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: #228B22"># Common imports</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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<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> DecisionTreeClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> make_moons
|
|
<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> export_graphviz
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
|
|
|
np.random.seed(<span style="color: #B452CD">42</span>)
|
|
X, y = make_moons(n_samples=<span style="color: #B452CD">100</span>, noise=<span style="color: #B452CD">0.25</span>, random_state=<span style="color: #B452CD">53</span>)
|
|
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=<span style="color: #B452CD">0</span>)
|
|
tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #B452CD">5</span>)
|
|
tree_clf.fit(X_train, y_train)
|
|
|
|
export_graphviz(
|
|
tree_clf,
|
|
out_file=<span style="color: #CD5555">"DataFiles/moons.dot"</span>,
|
|
rounded=<span style="color: #8B008B; font-weight: bold">True</span>,
|
|
filled=<span style="color: #8B008B; font-weight: bold">True</span>
|
|
)
|
|
cmd = <span style="color: #CD5555">'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'</span>
|
|
os.system(cmd)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec21">Other ways of visualizing the trees </h2>
|
|
|
|
<p>
|
|
<b>Scikit-Learn</b> has also another way to visualize the trees which is very useful, here with the Iris data.
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_iris
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> tree
|
|
X, y = load_iris(return_X_y=<span style="color: #8B008B; font-weight: bold">True</span>)
|
|
tree_clf = tree.DecisionTreeClassifier()
|
|
tree_clf = tree_clf.fit(X, y)
|
|
<span style="color: #228B22"># and then plot the tree</span>
|
|
tree.plot_tree(tree_clf)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec22">Printing out as text </h2>
|
|
|
|
<p>
|
|
Alternatively, the tree can also be exported in textual format with the function exporttext.
|
|
This method doesn’t require the installation of external libraries and is more compact:
|
|
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_iris
|
|
<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> DecisionTreeClassifier
|
|
<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> export_text
|
|
iris = load_iris()
|
|
decision_tree = DecisionTreeClassifier(random_state=<span style="color: #B452CD">0</span>, max_depth=<span style="color: #B452CD">2</span>)
|
|
decision_tree = decision_tree.fit(iris.data, iris.target)
|
|
r = export_text(decision_tree, feature_names=iris[<span style="color: #CD5555">'feature_names'</span>])
|
|
<span style="color: #658b00">print</span>(r)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec23">Algorithms for Setting up Decision Trees </h2>
|
|
|
|
<p>
|
|
Two algorithms stand out in the set up of decision trees:
|
|
|
|
<ol>
|
|
<li> The CART (Classification And Regression Tree) algorithm for both classification and regression</li>
|
|
<li> The ID3 algorithm based on the computation of the information gain for classification</li>
|
|
</ol>
|
|
|
|
We discuss both algorithms with applications here. The popular library
|
|
<b>Scikit-Learn</b> uses the CART algorithm. For classification problems
|
|
you can use either the <b>gini</b> index or the <b>entropy</b> to split a tree
|
|
in two branches.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec24">The CART algorithm for Classification </h2>
|
|
|
|
<p>
|
|
For classification, the CART algorithm splits the data set in two subsets using a single feature \( k \) and a threshold \( t_k \).
|
|
This could be for example a threshold set by a number below a certain circumference of a malign tumor.
|
|
|
|
<p>
|
|
How do we find these two quantities?
|
|
We search for the pair \( (k,t_k) \) that produces the purest subset using for example the <b>gini</b> factor \( G \).
|
|
The cost function it tries to minimize is then
|
|
$$
|
|
C(k,t_k) = \frac{m_{\mathrm{left}}}{m}G_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}G_{\mathrm{right}},
|
|
$$
|
|
|
|
where \( G_{\mathrm{left/right}} \) measures the impurity of the left/right subset and \( m_{\mathrm{left/right}} \)
|
|
is the number of instances in the left/right subset
|
|
|
|
<p>
|
|
Once it has successfully split the training set in two, it splits the subsets using the same logic, then the subsubsets
|
|
and so on, recursively. It stops recursing once it reaches the maximum depth (defined by the
|
|
\( max\_depth \) hyperparameter), or if it cannot find a split that will reduce impurity. A few other
|
|
hyperparameters control additional stopping conditions such as the \( min\_samples\_split \),
|
|
\( min\_samples\_leaf \), \( min\_weight\_fraction\_leaf \), and \( max\_leaf\_nodes \).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec25">The CART algorithm for Regression </h2>
|
|
|
|
<p>
|
|
The CART algorithm for regression works is similar to the one for classification except that instead of trying to split the
|
|
training set in a way that minimizes say the <b>gini</b> or <b>entropy</b> impurity, it now tries to split the training set in a way that minimizes our well-known mean-squared error (MSE). The cost function is now
|
|
$$
|
|
C(k,t_k) = \frac{m_{\mathrm{left}}}{m}\mathrm{MSE}_{\mathrm{left}}+ \frac{m_{\mathrm{right}}}{m}\mathrm{MSE}_{\mathrm{right}}.
|
|
$$
|
|
|
|
Here the MSE for a specific node is defined as
|
|
$$
|
|
\mathrm{MSE}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}(\overline{y}_{\mathrm{node}}-y_i)^2,
|
|
$$
|
|
|
|
with
|
|
$$
|
|
\overline{y}_{\mathrm{node}}=\frac{1}{m_\mathrm{node}}\sum_{i\in \mathrm{node}}y_i,
|
|
$$
|
|
|
|
the mean value of all observations in a specific node.
|
|
|
|
<p>
|
|
Without any regularization, the regression task for decision trees,
|
|
just like for classification tasks, is prone to overfitting.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec26">Computing the Gini index </h2>
|
|
|
|
<p>
|
|
The example we will look at is a classical one in many Machine
|
|
Learning applications. Based on various meteorological features, we
|
|
have several so-called attributes which decide whether we at the end
|
|
will do some outdoor activity like skiing, going for a bike ride etc
|
|
etc. The table here contains the feautures <b>outlook</b>, <b>temperature</b>,
|
|
<b>humidity</b> and <b>wind</b>. The target or output is whether we ride
|
|
(True=1) or whether we do something else that day (False=0). The
|
|
attributes for each feature are then sunny, overcast and rain for the
|
|
outlook, hot, cold and mild for temperature, high and normal for
|
|
humidity and weak and strong for wind.
|
|
|
|
<p>
|
|
The table here summarizes the various attributes and
|
|
<table border="1">
|
|
<thead>
|
|
<tr><th align="center">Day</th> <th align="center">Outlook </th> <th align="center">Temperature</th> <th align="center">Humidity</th> <th align="center"> Wind </th> <th align="center">Ride</th> </tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr><td align="center"> 1 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
|
<tr><td align="center"> 2 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 3 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 4 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 5 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 6 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
|
<tr><td align="center"> 7 </td> <td align="center"> Overcast </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 8 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
|
<tr><td align="center"> 9 </td> <td align="center"> Sunny </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 10 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 11 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 12 </td> <td align="center"> Overcast </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 13 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
|
<tr><td align="center"> 14 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
|
</tbody>
|
|
</table>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec27">Simple Python Code to read in Data and perform Classification </h2>
|
|
|
|
<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: #228B22"># Common imports</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">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</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.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<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> export_graphviz
|
|
<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> StandardScaler, OneHotEncoder
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.compose</span> <span style="color: #8B008B; font-weight: bold">import</span> ColumnTransformer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> Image
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pydot</span> <span style="color: #8B008B; font-weight: bold">import</span> graph_from_dot_data
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
|
|
|
<span style="color: #228B22"># Where to save the figures and data files</span>
|
|
PROJECT_ROOT_DIR = <span style="color: #CD5555">"Results"</span>
|
|
FIGURE_ID = <span style="color: #CD5555">"Results/FigureFiles"</span>
|
|
DATA_ID = <span style="color: #CD5555">"DataFiles/"</span>
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(PROJECT_ROOT_DIR):
|
|
os.mkdir(PROJECT_ROOT_DIR)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(FIGURE_ID):
|
|
os.makedirs(FIGURE_ID)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(DATA_ID):
|
|
os.makedirs(DATA_ID)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">image_path</span>(fig_id):
|
|
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(FIGURE_ID, fig_id)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">data_path</span>(dat_id):
|
|
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(DATA_ID, dat_id)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">save_fig</span>(fig_id):
|
|
plt.savefig(image_path(fig_id) + <span style="color: #CD5555">".png"</span>, <span style="color: #658b00">format</span>=<span style="color: #CD5555">'png'</span>)
|
|
|
|
infile = <span style="color: #658b00">open</span>(data_path(<span style="color: #CD5555">"rideclass.csv"</span>),<span style="color: #CD5555">'r'</span>)
|
|
|
|
<span style="color: #228B22"># Read the experimental data with Pandas</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> display
|
|
ridedata = pd.read_csv(infile,names = (<span style="color: #CD5555">'Outlook'</span>,<span style="color: #CD5555">'Temperature'</span>,<span style="color: #CD5555">'Humidity'</span>,<span style="color: #CD5555">'Wind'</span>,<span style="color: #CD5555">'Ride'</span>))
|
|
ridedata = pd.DataFrame(ridedata)
|
|
|
|
<span style="color: #228B22"># Features and targets</span>
|
|
X = ridedata.loc[:, ridedata.columns != <span style="color: #CD5555">'Ride'</span>].values
|
|
y = ridedata.loc[:, ridedata.columns == <span style="color: #CD5555">'Ride'</span>].values
|
|
|
|
<span style="color: #228B22"># Create the encoder.</span>
|
|
encoder = OneHotEncoder(handle_unknown=<span style="color: #CD5555">"ignore"</span>)
|
|
<span style="color: #228B22"># Assume for simplicity all features are categorical.</span>
|
|
encoder.fit(X)
|
|
<span style="color: #228B22"># Apply the encoder.</span>
|
|
X = encoder.transform(X)
|
|
<span style="color: #658b00">print</span>(X)
|
|
<span style="color: #228B22"># Then do a Classification tree</span>
|
|
tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #B452CD">2</span>)
|
|
tree_clf.fit(X, y)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy with Decision Tree: {:.2f}"</span>.format(tree_clf.score(X,y)))
|
|
<span style="color: #228B22">#transfer to a decision tree graph</span>
|
|
export_graphviz(
|
|
tree_clf,
|
|
out_file=<span style="color: #CD5555">"DataFiles/ride.dot"</span>,
|
|
rounded=<span style="color: #8B008B; font-weight: bold">True</span>,
|
|
filled=<span style="color: #8B008B; font-weight: bold">True</span>
|
|
)
|
|
cmd = <span style="color: #CD5555">'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'</span>
|
|
os.system(cmd)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec28">Computing the Gini Factor </h2>
|
|
|
|
<p>
|
|
The above functions (gini, entropy and misclassification error) are
|
|
important components of the so-called CART algorithm. We will discuss
|
|
this algorithm below after we have discussed the information gain
|
|
algorithm ID3.
|
|
|
|
<p>
|
|
In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.
|
|
|
|
<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: #228B22"># Split a dataset based on an attribute and an attribute value</span>
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">test_split</span>(index, value, dataset):
|
|
left, right = <span style="color: #658b00">list</span>(), <span style="color: #658b00">list</span>()
|
|
<span style="color: #8B008B; font-weight: bold">for</span> row <span style="color: #8B008B">in</span> dataset:
|
|
<span style="color: #8B008B; font-weight: bold">if</span> row[index] < value:
|
|
left.append(row)
|
|
<span style="color: #8B008B; font-weight: bold">else</span>:
|
|
right.append(row)
|
|
<span style="color: #8B008B; font-weight: bold">return</span> left, right
|
|
|
|
<span style="color: #228B22"># Calculate the Gini index for a split dataset</span>
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">gini_index</span>(groups, classes):
|
|
<span style="color: #228B22"># count all samples at split point</span>
|
|
n_instances = <span style="color: #658b00">float</span>(<span style="color: #658b00">sum</span>([<span style="color: #658b00">len</span>(group) <span style="color: #8B008B; font-weight: bold">for</span> group <span style="color: #8B008B">in</span> groups]))
|
|
<span style="color: #228B22"># sum weighted Gini index for each group</span>
|
|
gini = <span style="color: #B452CD">0.0</span>
|
|
<span style="color: #8B008B; font-weight: bold">for</span> group <span style="color: #8B008B">in</span> groups:
|
|
size = <span style="color: #658b00">float</span>(<span style="color: #658b00">len</span>(group))
|
|
<span style="color: #228B22"># avoid divide by zero</span>
|
|
<span style="color: #8B008B; font-weight: bold">if</span> size == <span style="color: #B452CD">0</span>:
|
|
<span style="color: #8B008B; font-weight: bold">continue</span>
|
|
score = <span style="color: #B452CD">0.0</span>
|
|
<span style="color: #228B22"># score the group based on the score for each class</span>
|
|
<span style="color: #8B008B; font-weight: bold">for</span> class_val <span style="color: #8B008B">in</span> classes:
|
|
p = [row[-<span style="color: #B452CD">1</span>] <span style="color: #8B008B; font-weight: bold">for</span> row <span style="color: #8B008B">in</span> group].count(class_val) / size
|
|
score += p * p
|
|
<span style="color: #228B22"># weight the group score by its relative size</span>
|
|
gini += (<span style="color: #B452CD">1.0</span> - score) * (size / n_instances)
|
|
<span style="color: #8B008B; font-weight: bold">return</span> gini
|
|
|
|
<span style="color: #228B22"># Select the best split point for a dataset</span>
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">get_split</span>(dataset):
|
|
class_values = <span style="color: #658b00">list</span>(<span style="color: #658b00">set</span>(row[-<span style="color: #B452CD">1</span>] <span style="color: #8B008B; font-weight: bold">for</span> row <span style="color: #8B008B">in</span> dataset))
|
|
b_index, b_value, b_score, b_groups = <span style="color: #B452CD">999</span>, <span style="color: #B452CD">999</span>, <span style="color: #B452CD">999</span>, <span style="color: #8B008B; font-weight: bold">None</span>
|
|
<span style="color: #8B008B; font-weight: bold">for</span> index <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(dataset[<span style="color: #B452CD">0</span>])-<span style="color: #B452CD">1</span>):
|
|
<span style="color: #8B008B; font-weight: bold">for</span> row <span style="color: #8B008B">in</span> dataset:
|
|
groups = test_split(index, row[index], dataset)
|
|
gini = gini_index(groups, class_values)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'X%d < %.3f Gini=%.3f'</span> % ((index+<span style="color: #B452CD">1</span>), row[index], gini))
|
|
<span style="color: #8B008B; font-weight: bold">if</span> gini < b_score:
|
|
b_index, b_value, b_score, b_groups = index, row[index], gini, groups
|
|
<span style="color: #8B008B; font-weight: bold">return</span> {<span style="color: #CD5555">'index'</span>:b_index, <span style="color: #CD5555">'value'</span>:b_value, <span style="color: #CD5555">'groups'</span>:b_groups}
|
|
|
|
dataset = [[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>],
|
|
[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">2</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">2</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>],
|
|
[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>],
|
|
[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>],
|
|
[<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]]
|
|
|
|
split = get_split(dataset)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Split: [X%d < %.3f]'</span> % ((split[<span style="color: #CD5555">'index'</span>]+<span style="color: #B452CD">1</span>), split[<span style="color: #CD5555">'value'</span>]))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec29">Entropy and the ID3 algorithm </h2>
|
|
|
|
<p>
|
|
The ID3 algorithm learns decision trees by constructing
|
|
them in a top down way, beginning with the question <b>which attribute should be tested at the root of the tree</b>?
|
|
|
|
<ol>
|
|
<li> Each instance attribute is evaluated using a statistical test to determine how well it alone classifies the training examples.</li>
|
|
<li> The best attribute is selected and used as the test at the root node of the tree.</li>
|
|
<li> A descendant of the root node is then created for each possible value of this attribute.</li>
|
|
<li> Training examples are sorted to the appropriate descendant node.</li>
|
|
<li> The entire process is then repeated using the training examples associated with each descendant node to select the best attribute to test at that point in the tree.</li>
|
|
<li> This forms a greedy search for an acceptable decision tree, in which the algorithm never backtracks to reconsider earlier choices.</li>
|
|
</ol>
|
|
|
|
The ID3 algorithm selects which attribute to test at each node in the
|
|
tree.
|
|
|
|
<p>
|
|
We would like to select the attribute that is most useful for classifying
|
|
examples.
|
|
|
|
<p>
|
|
What is a good quantitative measure of the worth of an attribute?
|
|
|
|
<p>
|
|
Information gain measures how well a given attribute separates the
|
|
training examples according to their target classification.
|
|
|
|
<p>
|
|
The ID3 algorithm uses this information gain measure to select among the candidate
|
|
attributes at each step while growing the tree.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec30">Cancer Data again now with Decision Trees and other Methods </h2>
|
|
<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">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">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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
|
<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> LogisticRegression
|
|
<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> DecisionTreeClassifier
|
|
|
|
<span style="color: #228B22"># Load the data</span>
|
|
cancer = load_breast_cancer()
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
|
|
<span style="color: #658b00">print</span>(X_train.shape)
|
|
<span style="color: #658b00">print</span>(X_test.shape)
|
|
<span style="color: #228B22"># Logistic Regression</span>
|
|
logreg = LogisticRegression(solver=<span style="color: #CD5555">'lbfgs'</span>)
|
|
logreg.fit(X_train, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Logistic Regression: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
|
<span style="color: #228B22"># Support vector machine</span>
|
|
svm = SVC(gamma=<span style="color: #CD5555">'auto'</span>, C=<span style="color: #B452CD">100</span>)
|
|
svm.fit(X_train, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with SVM: {:.2f}"</span>.format(svm.score(X_test,y_test)))
|
|
<span style="color: #228B22"># Decision Trees</span>
|
|
deep_tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #8B008B; font-weight: bold">None</span>)
|
|
deep_tree_clf.fit(X_train, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Decision Trees: {:.2f}"</span>.format(deep_tree_clf.score(X_test,y_test)))
|
|
<span style="color: #228B22">#now scale the data</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> StandardScaler
|
|
scaler = StandardScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
<span style="color: #228B22"># Logistic Regression</span>
|
|
logreg.fit(X_train_scaled, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy Logistic Regression with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
|
<span style="color: #228B22"># Support Vector Machine</span>
|
|
svm.fit(X_train_scaled, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy SVM with scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
|
<span style="color: #228B22"># Decision Trees</span>
|
|
deep_tree_clf.fit(X_train_scaled, y_train)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Test set accuracy with Decision Trees and scaled data: {:.2f}"</span>.format(deep_tree_clf.score(X_test_scaled,y_test)))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec31">Another example, the moons again </h2>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">__future__</span> <span style="color: #8B008B; font-weight: bold">import</span> division, print_function, unicode_literals
|
|
|
|
<span style="color: #228B22"># Common imports</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">os</span>
|
|
|
|
<span style="color: #228B22"># to make this notebook's output stable across runs</span>
|
|
np.random.seed(<span style="color: #B452CD">42</span>)
|
|
|
|
<span style="color: #228B22"># To plot pretty figures</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib</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">matplotlib.colors</span> <span style="color: #8B008B; font-weight: bold">import</span> ListedColormap
|
|
plt.rcParams[<span style="color: #CD5555">'axes.labelsize'</span>] = <span style="color: #B452CD">14</span>
|
|
plt.rcParams[<span style="color: #CD5555">'xtick.labelsize'</span>] = <span style="color: #B452CD">12</span>
|
|
plt.rcParams[<span style="color: #CD5555">'ytick.labelsize'</span>] = <span style="color: #B452CD">12</span>
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets
|
|
<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> DecisionTreeClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> make_moons
|
|
<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> export_graphviz
|
|
|
|
Xm, ym = make_moons(n_samples=<span style="color: #B452CD">100</span>, noise=<span style="color: #B452CD">0.25</span>, random_state=<span style="color: #B452CD">53</span>)
|
|
|
|
deep_tree_clf1 = DecisionTreeClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
deep_tree_clf2 = DecisionTreeClassifier(min_samples_leaf=<span style="color: #B452CD">4</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
deep_tree_clf1.fit(Xm, ym)
|
|
deep_tree_clf2.fit(Xm, ym)
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">plot_decision_boundary</span>(clf, X, y, axes=[<span style="color: #B452CD">0</span>, <span style="color: #B452CD">7.5</span>, <span style="color: #B452CD">0</span>, <span style="color: #B452CD">3</span>], iris=<span style="color: #8B008B; font-weight: bold">True</span>, legend=<span style="color: #8B008B; font-weight: bold">False</span>, plot_training=<span style="color: #8B008B; font-weight: bold">True</span>):
|
|
x1s = np.linspace(axes[<span style="color: #B452CD">0</span>], axes[<span style="color: #B452CD">1</span>], <span style="color: #B452CD">100</span>)
|
|
x2s = np.linspace(axes[<span style="color: #B452CD">2</span>], axes[<span style="color: #B452CD">3</span>], <span style="color: #B452CD">100</span>)
|
|
x1, x2 = np.meshgrid(x1s, x2s)
|
|
X_new = np.c_[x1.ravel(), x2.ravel()]
|
|
y_pred = clf.predict(X_new).reshape(x1.shape)
|
|
custom_cmap = ListedColormap([<span style="color: #CD5555">'#fafab0'</span>,<span style="color: #CD5555">'#9898ff'</span>,<span style="color: #CD5555">'#a0faa0'</span>])
|
|
plt.contourf(x1, x2, y_pred, alpha=<span style="color: #B452CD">0.3</span>, cmap=custom_cmap)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> iris:
|
|
custom_cmap2 = ListedColormap([<span style="color: #CD5555">'#7d7d58'</span>,<span style="color: #CD5555">'#4c4c7f'</span>,<span style="color: #CD5555">'#507d50'</span>])
|
|
plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=<span style="color: #B452CD">0.8</span>)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> plot_training:
|
|
plt.plot(X[:, <span style="color: #B452CD">0</span>][y==<span style="color: #B452CD">0</span>], X[:, <span style="color: #B452CD">1</span>][y==<span style="color: #B452CD">0</span>], <span style="color: #CD5555">"yo"</span>, label=<span style="color: #CD5555">"Iris-Setosa"</span>)
|
|
plt.plot(X[:, <span style="color: #B452CD">0</span>][y==<span style="color: #B452CD">1</span>], X[:, <span style="color: #B452CD">1</span>][y==<span style="color: #B452CD">1</span>], <span style="color: #CD5555">"bs"</span>, label=<span style="color: #CD5555">"Iris-Versicolor"</span>)
|
|
plt.plot(X[:, <span style="color: #B452CD">0</span>][y==<span style="color: #B452CD">2</span>], X[:, <span style="color: #B452CD">1</span>][y==<span style="color: #B452CD">2</span>], <span style="color: #CD5555">"g^"</span>, label=<span style="color: #CD5555">"Iris-Virginica"</span>)
|
|
plt.axis(axes)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> iris:
|
|
plt.xlabel(<span style="color: #CD5555">"Petal length"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
plt.ylabel(<span style="color: #CD5555">"Petal width"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
<span style="color: #8B008B; font-weight: bold">else</span>:
|
|
plt.xlabel(<span style="color: #CD5555">r"$x_1$"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.ylabel(<span style="color: #CD5555">r"$x_2$"</span>, fontsize=<span style="color: #B452CD">18</span>, rotation=<span style="color: #B452CD">0</span>)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> legend:
|
|
plt.legend(loc=<span style="color: #CD5555">"lower right"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
plt.figure(figsize=(<span style="color: #B452CD">11</span>, <span style="color: #B452CD">4</span>))
|
|
plt.subplot(<span style="color: #B452CD">121</span>)
|
|
plot_decision_boundary(deep_tree_clf1, Xm, ym, axes=[-<span style="color: #B452CD">1.5</span>, <span style="color: #B452CD">2.5</span>, -<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1.5</span>], iris=<span style="color: #8B008B; font-weight: bold">False</span>)
|
|
plt.title(<span style="color: #CD5555">"No restrictions"</span>, fontsize=<span style="color: #B452CD">16</span>)
|
|
plt.subplot(<span style="color: #B452CD">122</span>)
|
|
plot_decision_boundary(deep_tree_clf2, Xm, ym, axes=[-<span style="color: #B452CD">1.5</span>, <span style="color: #B452CD">2.5</span>, -<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1.5</span>], iris=<span style="color: #8B008B; font-weight: bold">False</span>)
|
|
plt.title(<span style="color: #CD5555">"min_samples_leaf = {}"</span>.format(deep_tree_clf2.min_samples_leaf), fontsize=<span style="color: #B452CD">14</span>)
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec32">Playing around with regions </h2>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>np.random.seed(<span style="color: #B452CD">6</span>)
|
|
Xs = np.random.rand(<span style="color: #B452CD">100</span>, <span style="color: #B452CD">2</span>) - <span style="color: #B452CD">0.5</span>
|
|
ys = (Xs[:, <span style="color: #B452CD">0</span>] > <span style="color: #B452CD">0</span>).astype(np.float32) * <span style="color: #B452CD">2</span>
|
|
|
|
angle = np.pi/<span style="color: #B452CD">4</span>
|
|
rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
|
|
Xsr = Xs.dot(rotation_matrix)
|
|
|
|
tree_clf_s = DecisionTreeClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
tree_clf_s.fit(Xs, ys)
|
|
tree_clf_sr = DecisionTreeClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
tree_clf_sr.fit(Xsr, ys)
|
|
|
|
plt.figure(figsize=(<span style="color: #B452CD">11</span>, <span style="color: #B452CD">4</span>))
|
|
plt.subplot(<span style="color: #B452CD">121</span>)
|
|
plot_decision_boundary(tree_clf_s, Xs, ys, axes=[-<span style="color: #B452CD">0.7</span>, <span style="color: #B452CD">0.7</span>, -<span style="color: #B452CD">0.7</span>, <span style="color: #B452CD">0.7</span>], iris=<span style="color: #8B008B; font-weight: bold">False</span>)
|
|
plt.subplot(<span style="color: #B452CD">122</span>)
|
|
plot_decision_boundary(tree_clf_sr, Xsr, ys, axes=[-<span style="color: #B452CD">0.7</span>, <span style="color: #B452CD">0.7</span>, -<span style="color: #B452CD">0.7</span>, <span style="color: #B452CD">0.7</span>], iris=<span style="color: #8B008B; font-weight: bold">False</span>)
|
|
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec33">Regression trees </h2>
|
|
<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: #228B22"># Quadratic training set + noise</span>
|
|
np.random.seed(<span style="color: #B452CD">42</span>)
|
|
m = <span style="color: #B452CD">200</span>
|
|
X = np.random.rand(m, <span style="color: #B452CD">1</span>)
|
|
y = <span style="color: #B452CD">4</span> * (X - <span style="color: #B452CD">0.5</span>) ** <span style="color: #B452CD">2</span>
|
|
y = y + np.random.randn(m, <span style="color: #B452CD">1</span>) / <span style="color: #B452CD">10</span>
|
|
</pre></div>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
|
|
|
tree_reg = DecisionTreeRegressor(max_depth=<span style="color: #B452CD">2</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
tree_reg.fit(X, y)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec34">Final regressor code </h2>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.tree</span> <span style="color: #8B008B; font-weight: bold">import</span> DecisionTreeRegressor
|
|
|
|
tree_reg1 = DecisionTreeRegressor(random_state=<span style="color: #B452CD">42</span>, max_depth=<span style="color: #B452CD">2</span>)
|
|
tree_reg2 = DecisionTreeRegressor(random_state=<span style="color: #B452CD">42</span>, max_depth=<span style="color: #B452CD">3</span>)
|
|
tree_reg1.fit(X, y)
|
|
tree_reg2.fit(X, y)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">plot_regression_predictions</span>(tree_reg, X, y, axes=[<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, -<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1</span>], ylabel=<span style="color: #CD5555">"$y$"</span>):
|
|
x1 = np.linspace(axes[<span style="color: #B452CD">0</span>], axes[<span style="color: #B452CD">1</span>], <span style="color: #B452CD">500</span>).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
|
y_pred = tree_reg.predict(x1)
|
|
plt.axis(axes)
|
|
plt.xlabel(<span style="color: #CD5555">"$x_1$"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> ylabel:
|
|
plt.ylabel(ylabel, fontsize=<span style="color: #B452CD">18</span>, rotation=<span style="color: #B452CD">0</span>)
|
|
plt.plot(X, y, <span style="color: #CD5555">"b."</span>)
|
|
plt.plot(x1, y_pred, <span style="color: #CD5555">"r.-"</span>, linewidth=<span style="color: #B452CD">2</span>, label=<span style="color: #CD5555">r"$\hat{y}$"</span>)
|
|
|
|
plt.figure(figsize=(<span style="color: #B452CD">11</span>, <span style="color: #B452CD">4</span>))
|
|
plt.subplot(<span style="color: #B452CD">121</span>)
|
|
plot_regression_predictions(tree_reg1, X, y)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> split, style <span style="color: #8B008B">in</span> ((<span style="color: #B452CD">0.1973</span>, <span style="color: #CD5555">"k-"</span>), (<span style="color: #B452CD">0.0917</span>, <span style="color: #CD5555">"k--"</span>), (<span style="color: #B452CD">0.7718</span>, <span style="color: #CD5555">"k--"</span>)):
|
|
plt.plot([split, split], [-<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1</span>], style, linewidth=<span style="color: #B452CD">2</span>)
|
|
plt.text(<span style="color: #B452CD">0.21</span>, <span style="color: #B452CD">0.65</span>, <span style="color: #CD5555">"Depth=0"</span>, fontsize=<span style="color: #B452CD">15</span>)
|
|
plt.text(<span style="color: #B452CD">0.01</span>, <span style="color: #B452CD">0.2</span>, <span style="color: #CD5555">"Depth=1"</span>, fontsize=<span style="color: #B452CD">13</span>)
|
|
plt.text(<span style="color: #B452CD">0.65</span>, <span style="color: #B452CD">0.8</span>, <span style="color: #CD5555">"Depth=1"</span>, fontsize=<span style="color: #B452CD">13</span>)
|
|
plt.legend(loc=<span style="color: #CD5555">"upper center"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.title(<span style="color: #CD5555">"max_depth=2"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
|
|
plt.subplot(<span style="color: #B452CD">122</span>)
|
|
plot_regression_predictions(tree_reg2, X, y, ylabel=<span style="color: #8B008B; font-weight: bold">None</span>)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> split, style <span style="color: #8B008B">in</span> ((<span style="color: #B452CD">0.1973</span>, <span style="color: #CD5555">"k-"</span>), (<span style="color: #B452CD">0.0917</span>, <span style="color: #CD5555">"k--"</span>), (<span style="color: #B452CD">0.7718</span>, <span style="color: #CD5555">"k--"</span>)):
|
|
plt.plot([split, split], [-<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1</span>], style, linewidth=<span style="color: #B452CD">2</span>)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> split <span style="color: #8B008B">in</span> (<span style="color: #B452CD">0.0458</span>, <span style="color: #B452CD">0.1298</span>, <span style="color: #B452CD">0.2873</span>, <span style="color: #B452CD">0.9040</span>):
|
|
plt.plot([split, split], [-<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1</span>], <span style="color: #CD5555">"k:"</span>, linewidth=<span style="color: #B452CD">1</span>)
|
|
plt.text(<span style="color: #B452CD">0.3</span>, <span style="color: #B452CD">0.5</span>, <span style="color: #CD5555">"Depth=2"</span>, fontsize=<span style="color: #B452CD">13</span>)
|
|
plt.title(<span style="color: #CD5555">"max_depth=3"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>tree_reg1 = DecisionTreeRegressor(random_state=<span style="color: #B452CD">42</span>)
|
|
tree_reg2 = DecisionTreeRegressor(random_state=<span style="color: #B452CD">42</span>, min_samples_leaf=<span style="color: #B452CD">10</span>)
|
|
tree_reg1.fit(X, y)
|
|
tree_reg2.fit(X, y)
|
|
|
|
x1 = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">500</span>).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
|
y_pred1 = tree_reg1.predict(x1)
|
|
y_pred2 = tree_reg2.predict(x1)
|
|
|
|
plt.figure(figsize=(<span style="color: #B452CD">11</span>, <span style="color: #B452CD">4</span>))
|
|
|
|
plt.subplot(<span style="color: #B452CD">121</span>)
|
|
plt.plot(X, y, <span style="color: #CD5555">"b."</span>)
|
|
plt.plot(x1, y_pred1, <span style="color: #CD5555">"r.-"</span>, linewidth=<span style="color: #B452CD">2</span>, label=<span style="color: #CD5555">r"$\hat{y}$"</span>)
|
|
plt.axis([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, -<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1.1</span>])
|
|
plt.xlabel(<span style="color: #CD5555">"$x_1$"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.ylabel(<span style="color: #CD5555">"$y$"</span>, fontsize=<span style="color: #B452CD">18</span>, rotation=<span style="color: #B452CD">0</span>)
|
|
plt.legend(loc=<span style="color: #CD5555">"upper center"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.title(<span style="color: #CD5555">"No restrictions"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
|
|
plt.subplot(<span style="color: #B452CD">122</span>)
|
|
plt.plot(X, y, <span style="color: #CD5555">"b."</span>)
|
|
plt.plot(x1, y_pred2, <span style="color: #CD5555">"r.-"</span>, linewidth=<span style="color: #B452CD">2</span>, label=<span style="color: #CD5555">r"$\hat{y}$"</span>)
|
|
plt.axis([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, -<span style="color: #B452CD">0.2</span>, <span style="color: #B452CD">1.1</span>])
|
|
plt.xlabel(<span style="color: #CD5555">"$x_1$"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.title(<span style="color: #CD5555">"min_samples_leaf={}"</span>.format(tree_reg2.min_samples_leaf), fontsize=<span style="color: #B452CD">14</span>)
|
|
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec35">Pros and cons of trees, pros </h2>
|
|
|
|
<ul>
|
|
<li> White box, easy to interpret model. Some people believe that decision trees more closely mirror human decision-making than do the regression and classification approaches discussed earlier (think of support vector machines)</li>
|
|
<li> Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression!</li>
|
|
<li> No feature normalization needed</li>
|
|
<li> Tree models can handle both continuous and categorical data (Classification and Regression Trees)</li>
|
|
<li> Can model nonlinear relationships</li>
|
|
<li> Can model interactions between the different descriptive features</li>
|
|
<li> Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)</li>
|
|
</ul>
|
|
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec36">Disadvantages </h2>
|
|
|
|
<ul>
|
|
<li> Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches</li>
|
|
<li> If continuous features are used the tree may become quite large and hence less interpretable</li>
|
|
<li> Decision trees are prone to overfit the training data and hence do not well generalize the data if no stopping criteria or improvements like pruning, boosting or bagging are implemented</li>
|
|
<li> Small changes in the data may lead to a completely different tree. This issue can be addressed by using ensemble methods like bagging, boosting or random forests</li>
|
|
<li> Unbalanced datasets where some target feature values occur much more frequently than others may lead to biased trees since the frequently occurring feature values are preferred over the less frequently occurring ones.</li>
|
|
<li> If the number of features is relatively large (high dimensional) and the number of instances is relatively low, the tree might overfit the data</li>
|
|
<li> Features with many levels may be preferred over features with less levels since for them it is <em>more easy</em> to split the dataset such that the sub datasets only contain pure target feature values. This issue can be addressed by preferring for instance the information gain ratio as splitting criteria over information gain</li>
|
|
</ul>
|
|
|
|
However, by aggregating many decision trees, using methods like
|
|
bagging, random forests, and boosting, the predictive performance of
|
|
trees can be substantially improved.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec37">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods </h2>
|
|
|
|
<p>
|
|
As stated above and seen in many of the examples discussed here about
|
|
a single decision tree, we often end up overfitting our training
|
|
data. This normally means that we have a high variance. Can we reduce
|
|
the variance of a statistical learning method?
|
|
|
|
<p>
|
|
This leads us to a set of different methods that can combine different
|
|
machine learning algorithms or just use one of them to construct
|
|
forests and jungles of trees, homogeneous ones or heterogenous
|
|
ones. These methods are recognized by different names which we will
|
|
try to explain here. These are
|
|
|
|
<ol>
|
|
<li> Voting classifiers</li>
|
|
<li> Bagging and Pasting</li>
|
|
<li> Random forests</li>
|
|
<li> Boosting methods, from adaptive to Extreme Gradient Boosting (XGBoost)</li>
|
|
</ol>
|
|
|
|
We discuss these methods here.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec38">An Overview of Ensemble Methods </h2>
|
|
|
|
<p>
|
|
<br /><br /><center><p><img src="DataFiles/ensembleoverview.png" align="bottom" width=600></p></center><br /><br />
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec39">Bagging </h2>
|
|
|
|
<p>
|
|
The <b>plain</b> decision trees suffer from high
|
|
variance. This means that if we split the training data into two parts
|
|
at random, and fit a decision tree to both halves, the results that we
|
|
get could be quite different. In contrast, a procedure with low
|
|
variance will yield similar results if applied repeatedly to distinct
|
|
data sets; linear regression tends to have low variance, if the ratio
|
|
of \( n \) to \( p \) is moderately large.
|
|
|
|
<p>
|
|
<b>Bootstrap aggregation</b>, or just <b>bagging</b>, is a
|
|
general-purpose procedure for reducing the variance of a statistical
|
|
learning method.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec40">More bagging </h2>
|
|
|
|
<p>
|
|
Bagging typically results in improved accuracy
|
|
over prediction using a single tree. Unfortunately, however, it can be
|
|
difficult to interpret the resulting model. Recall that one of the
|
|
advantages of decision trees is the attractive and easily interpreted
|
|
diagram that results.
|
|
|
|
<p>
|
|
However, when we bag a large number of trees, it is no longer
|
|
possible to represent the resulting statistical learning procedure
|
|
using a single tree, and it is no longer clear which variables are
|
|
most important to the procedure. Thus, bagging improves prediction
|
|
accuracy at the expense of interpretability. Although the collection
|
|
of bagged trees is much more difficult to interpret than a single
|
|
tree, one can obtain an overall summary of the importance of each
|
|
predictor using the MSE (for bagging regression trees) or the Gini
|
|
index (for bagging classification trees). In the case of bagging
|
|
regression trees, we can record the total amount that the MSE is
|
|
decreased due to splits over a given predictor, averaged over all \( B \) possible
|
|
trees. A large value indicates an important predictor. Similarly, in
|
|
the context of bagging classification trees, we can add up the total
|
|
amount that the Gini index is decreased by splits over a given
|
|
predictor, averaged over all \( B \) trees.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec41">Simple Voting Example, head or tail </h2>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>heads_proba = <span style="color: #B452CD">0.51</span>
|
|
coin_tosses = (np.random.rand(<span style="color: #B452CD">10000</span>, <span style="color: #B452CD">10</span>) < heads_proba).astype(np.int32)
|
|
cumulative_heads_ratio = np.cumsum(coin_tosses, axis=<span style="color: #B452CD">0</span>) / np.arange(<span style="color: #B452CD">1</span>, <span style="color: #B452CD">10001</span>).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
|
plt.figure(figsize=(<span style="color: #B452CD">8</span>,<span style="color: #B452CD">3.5</span>))
|
|
plt.plot(cumulative_heads_ratio)
|
|
plt.plot([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10000</span>], [<span style="color: #B452CD">0.51</span>, <span style="color: #B452CD">0.51</span>], <span style="color: #CD5555">"k--"</span>, linewidth=<span style="color: #B452CD">2</span>, label=<span style="color: #CD5555">"51%"</span>)
|
|
plt.plot([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10000</span>], [<span style="color: #B452CD">0.5</span>, <span style="color: #B452CD">0.5</span>], <span style="color: #CD5555">"k-"</span>, label=<span style="color: #CD5555">"50%"</span>)
|
|
plt.xlabel(<span style="color: #CD5555">"Number of coin tosses"</span>)
|
|
plt.ylabel(<span style="color: #CD5555">"Heads ratio"</span>)
|
|
plt.legend(loc=<span style="color: #CD5555">"lower right"</span>)
|
|
plt.axis([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10000</span>, <span style="color: #B452CD">0.42</span>, <span style="color: #B452CD">0.58</span>])
|
|
save_fig(<span style="color: #CD5555">"votingsimple"</span>)
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec42">Using the Voting Classifier </h2>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> make_moons
|
|
|
|
X, y = make_moons(n_samples=<span style="color: #B452CD">500</span>, noise=<span style="color: #B452CD">0.30</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=<span style="color: #B452CD">42</span>)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> VotingClassifier
|
|
<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> LogisticRegression
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
|
|
|
log_clf = LogisticRegression(solver=<span style="color: #CD5555">"liblinear"</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
rnd_clf = RandomForestClassifier(n_estimators=<span style="color: #B452CD">10</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
svm_clf = SVC(gamma=<span style="color: #CD5555">"auto"</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
|
|
voting_clf = VotingClassifier(
|
|
estimators=[(<span style="color: #CD5555">'lr'</span>, log_clf), (<span style="color: #CD5555">'rf'</span>, rnd_clf), (<span style="color: #CD5555">'svc'</span>, svm_clf)],
|
|
voting=<span style="color: #CD5555">'hard'</span>)
|
|
|
|
voting_clf.fit(X_train, y_train)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> accuracy_score
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> clf <span style="color: #8B008B">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
|
|
clf.fit(X_train, y_train)
|
|
y_pred = clf.predict(X_test)
|
|
<span style="color: #658b00">print</span>(clf.<span style="color: #00688B">__class__</span>.<span style="color: #00688B">__name__</span>, accuracy_score(y_test, y_pred))
|
|
|
|
log_clf = LogisticRegression(solver=<span style="color: #CD5555">"liblinear"</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
rnd_clf = RandomForestClassifier(n_estimators=<span style="color: #B452CD">10</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
svm_clf = SVC(gamma=<span style="color: #CD5555">"auto"</span>, probability=<span style="color: #8B008B; font-weight: bold">True</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
|
|
voting_clf = VotingClassifier(
|
|
estimators=[(<span style="color: #CD5555">'lr'</span>, log_clf), (<span style="color: #CD5555">'rf'</span>, rnd_clf), (<span style="color: #CD5555">'svc'</span>, svm_clf)],
|
|
voting=<span style="color: #CD5555">'soft'</span>)
|
|
voting_clf.fit(X_train, y_train)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> accuracy_score
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> clf <span style="color: #8B008B">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
|
|
clf.fit(X_train, y_train)
|
|
y_pred = clf.predict(X_test)
|
|
<span style="color: #658b00">print</span>(clf.<span style="color: #00688B">__class__</span>.<span style="color: #00688B">__name__</span>, accuracy_score(y_test, y_pred))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec43">Please, not the moons again! Voting and Bagging </h2>
|
|
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> make_moons
|
|
|
|
X, y = make_moons(n_samples=<span style="color: #B452CD">500</span>, noise=<span style="color: #B452CD">0.30</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=<span style="color: #B452CD">42</span>)
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> RandomForestClassifier
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> VotingClassifier
|
|
<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> LogisticRegression
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
|
|
|
log_clf = LogisticRegression(random_state=<span style="color: #B452CD">42</span>)
|
|
rnd_clf = RandomForestClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
svm_clf = SVC(random_state=<span style="color: #B452CD">42</span>)
|
|
|
|
voting_clf = VotingClassifier(
|
|
estimators=[(<span style="color: #CD5555">'lr'</span>, log_clf), (<span style="color: #CD5555">'rf'</span>, rnd_clf), (<span style="color: #CD5555">'svc'</span>, svm_clf)],
|
|
voting=<span style="color: #CD5555">'hard'</span>)
|
|
voting_clf.fit(X_train, y_train)
|
|
</pre></div>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> accuracy_score
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> clf <span style="color: #8B008B">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
|
|
clf.fit(X_train, y_train)
|
|
y_pred = clf.predict(X_test)
|
|
<span style="color: #658b00">print</span>(clf.<span style="color: #00688B">__class__</span>.<span style="color: #00688B">__name__</span>, accuracy_score(y_test, y_pred))
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>log_clf = LogisticRegression(random_state=<span style="color: #B452CD">42</span>)
|
|
rnd_clf = RandomForestClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
svm_clf = SVC(probability=<span style="color: #8B008B; font-weight: bold">True</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
|
|
voting_clf = VotingClassifier(
|
|
estimators=[(<span style="color: #CD5555">'lr'</span>, log_clf), (<span style="color: #CD5555">'rf'</span>, rnd_clf), (<span style="color: #CD5555">'svc'</span>, svm_clf)],
|
|
voting=<span style="color: #CD5555">'soft'</span>)
|
|
voting_clf.fit(X_train, y_train)
|
|
</pre></div>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> accuracy_score
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> clf <span style="color: #8B008B">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
|
|
clf.fit(X_train, y_train)
|
|
y_pred = clf.predict(X_test)
|
|
<span style="color: #658b00">print</span>(clf.<span style="color: #00688B">__class__</span>.<span style="color: #00688B">__name__</span>, accuracy_score(y_test, y_pred))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec44">Bagging Examples </h2>
|
|
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.ensemble</span> <span style="color: #8B008B; font-weight: bold">import</span> BaggingClassifier
|
|
<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> DecisionTreeClassifier
|
|
|
|
bag_clf = BaggingClassifier(
|
|
DecisionTreeClassifier(random_state=<span style="color: #B452CD">42</span>), n_estimators=<span style="color: #B452CD">500</span>,
|
|
max_samples=<span style="color: #B452CD">100</span>, bootstrap=<span style="color: #8B008B; font-weight: bold">True</span>, n_jobs=-<span style="color: #B452CD">1</span>, random_state=<span style="color: #B452CD">42</span>)
|
|
bag_clf.fit(X_train, y_train)
|
|
y_pred = bag_clf.predict(X_test)
|
|
</pre></div>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> accuracy_score
|
|
<span style="color: #658b00">print</span>(accuracy_score(y_test, y_pred))
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>tree_clf = DecisionTreeClassifier(random_state=<span style="color: #B452CD">42</span>)
|
|
tree_clf.fit(X_train, y_train)
|
|
y_pred_tree = tree_clf.predict(X_test)
|
|
<span style="color: #658b00">print</span>(accuracy_score(y_test, y_pred_tree))
|
|
</pre></div>
|
|
<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">from</span> <span style="color: #008b45; text-decoration: underline">matplotlib.colors</span> <span style="color: #8B008B; font-weight: bold">import</span> ListedColormap
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">plot_decision_boundary</span>(clf, X, y, axes=[-<span style="color: #B452CD">1.5</span>, <span style="color: #B452CD">2.5</span>, -<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1.5</span>], alpha=<span style="color: #B452CD">0.5</span>, contour=<span style="color: #8B008B; font-weight: bold">True</span>):
|
|
x1s = np.linspace(axes[<span style="color: #B452CD">0</span>], axes[<span style="color: #B452CD">1</span>], <span style="color: #B452CD">100</span>)
|
|
x2s = np.linspace(axes[<span style="color: #B452CD">2</span>], axes[<span style="color: #B452CD">3</span>], <span style="color: #B452CD">100</span>)
|
|
x1, x2 = np.meshgrid(x1s, x2s)
|
|
X_new = np.c_[x1.ravel(), x2.ravel()]
|
|
y_pred = clf.predict(X_new).reshape(x1.shape)
|
|
custom_cmap = ListedColormap([<span style="color: #CD5555">'#fafab0'</span>,<span style="color: #CD5555">'#9898ff'</span>,<span style="color: #CD5555">'#a0faa0'</span>])
|
|
plt.contourf(x1, x2, y_pred, alpha=<span style="color: #B452CD">0.3</span>, cmap=custom_cmap)
|
|
<span style="color: #8B008B; font-weight: bold">if</span> contour:
|
|
custom_cmap2 = ListedColormap([<span style="color: #CD5555">'#7d7d58'</span>,<span style="color: #CD5555">'#4c4c7f'</span>,<span style="color: #CD5555">'#507d50'</span>])
|
|
plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=<span style="color: #B452CD">0.8</span>)
|
|
plt.plot(X[:, <span style="color: #B452CD">0</span>][y==<span style="color: #B452CD">0</span>], X[:, <span style="color: #B452CD">1</span>][y==<span style="color: #B452CD">0</span>], <span style="color: #CD5555">"yo"</span>, alpha=alpha)
|
|
plt.plot(X[:, <span style="color: #B452CD">0</span>][y==<span style="color: #B452CD">1</span>], X[:, <span style="color: #B452CD">1</span>][y==<span style="color: #B452CD">1</span>], <span style="color: #CD5555">"bs"</span>, alpha=alpha)
|
|
plt.axis(axes)
|
|
plt.xlabel(<span style="color: #CD5555">r"$x_1$"</span>, fontsize=<span style="color: #B452CD">18</span>)
|
|
plt.ylabel(<span style="color: #CD5555">r"$x_2$"</span>, fontsize=<span style="color: #B452CD">18</span>, rotation=<span style="color: #B452CD">0</span>)
|
|
plt.figure(figsize=(<span style="color: #B452CD">11</span>,<span style="color: #B452CD">4</span>))
|
|
plt.subplot(<span style="color: #B452CD">121</span>)
|
|
plot_decision_boundary(tree_clf, X, y)
|
|
plt.title(<span style="color: #CD5555">"Decision Tree"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
plt.subplot(<span style="color: #B452CD">122</span>)
|
|
plot_decision_boundary(bag_clf, X, y)
|
|
plt.title(<span style="color: #CD5555">"Decision Trees with Bagging"</span>, fontsize=<span style="color: #B452CD">14</span>)
|
|
save_fig(<span style="color: #CD5555">"baggingtree"</span>)
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec45">Making your own Bootstrap: Changing the Level of the Decision Tree </h2>
|
|
|
|
<p>
|
|
Let us bring up our good old boostrap example from the linear regression lectures. We change the linerar regression algorithm with
|
|
a decision tree wth different depths and perform a bootstrap aggregate (in this case we perform as many bootstraps as data points \( n \)).
|
|
<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">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">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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.pipeline</span> <span style="color: #8B008B; font-weight: bold">import</span> make_pipeline
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> resample
|
|
<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
|
|
|
|
n = <span style="color: #B452CD">100</span>
|
|
n_boostraps = <span style="color: #B452CD">100</span>
|
|
maxdepth = <span style="color: #B452CD">8</span>
|
|
|
|
<span style="color: #228B22"># Make data set.</span>
|
|
x = np.linspace(-<span style="color: #B452CD">3</span>, <span style="color: #B452CD">3</span>, n).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
|
y = np.exp(-x**<span style="color: #B452CD">2</span>) + <span style="color: #B452CD">1.5</span> * np.exp(-(x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>)+ np.random.normal(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">0.1</span>, x.shape)
|
|
error = np.zeros(maxdepth)
|
|
bias = np.zeros(maxdepth)
|
|
variance = np.zeros(maxdepth)
|
|
polydegree = np.zeros(maxdepth)
|
|
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=<span style="color: #B452CD">0.2</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> StandardScaler
|
|
scaler = StandardScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
|
|
<span style="color: #228B22"># we produce a simple tree first as benchmark</span>
|
|
simpletree = DecisionTreeRegressor(max_depth=<span style="color: #B452CD">3</span>)
|
|
simpletree.fit(X_train_scaled, y_train)
|
|
simpleprediction = simpletree.predict(X_test_scaled)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">1</span>,maxdepth):
|
|
model = DecisionTreeRegressor(max_depth=degree)
|
|
y_pred = np.empty((y_test.shape[<span style="color: #B452CD">0</span>], n_boostraps))
|
|
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(n_boostraps):
|
|
x_, y_ = resample(X_train_scaled, y_train)
|
|
model.fit(x_, y_)
|
|
y_pred[:, i] = model.predict(X_test_scaled)<span style="color: #228B22">#.ravel()</span>
|
|
|
|
polydegree[degree] = degree
|
|
error[degree] = np.mean( np.mean((y_test - y_pred)**<span style="color: #B452CD">2</span>, axis=<span style="color: #B452CD">1</span>, keepdims=<span style="color: #8B008B; font-weight: bold">True</span>) )
|
|
bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=<span style="color: #B452CD">1</span>, keepdims=<span style="color: #8B008B; font-weight: bold">True</span>))**<span style="color: #B452CD">2</span> )
|
|
variance[degree] = np.mean( np.var(y_pred, axis=<span style="color: #B452CD">1</span>, keepdims=<span style="color: #8B008B; font-weight: bold">True</span>) )
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Polynomial degree:'</span>, degree)
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Error:'</span>, error[degree])
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Bias^2:'</span>, bias[degree])
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Var:'</span>, variance[degree])
|
|
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'{} >= {} + {} = {}'</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
|
|
|
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**<span style="color: #B452CD">2</span>)
|
|
<span style="color: #658b00">print</span>(mse_simpletree)
|
|
plt.xlim(<span style="color: #B452CD">1</span>,maxdepth)
|
|
plt.plot(polydegree, error, label=<span style="color: #CD5555">'MSE'</span>)
|
|
plt.plot(polydegree, bias, label=<span style="color: #CD5555">'bias'</span>)
|
|
plt.plot(polydegree, variance, label=<span style="color: #CD5555">'Variance'</span>)
|
|
plt.legend()
|
|
save_fig(<span style="color: #CD5555">"baggingboot"</span>)
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- ------------------- end of main content --------------- -->
|
|
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
|
</center>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|
|
|