Files
FYS-STK4155/doc/pub/week45/html/._week45-bs024.html
T
2021-11-10 16:37:21 +01:00

370 lines
21 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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, '___sec0'),
('Decision trees, overarching aims', 2, None, '___sec1'),
('Basics of a tree', 2, None, '___sec2'),
('A Sketch of a Tree, Regression problem', 2, None, '___sec3'),
('A Sketch of a Tree, Classification problem',
2,
None,
'___sec4'),
('A typical Decision Tree with its pertinent Jargon, '
'Classification Problem',
2,
None,
'___sec5'),
('General Features', 2, None, '___sec6'),
('How do we set it up?', 2, None, '___sec7'),
('Decision trees and Regression', 2, None, '___sec8'),
('Building a tree, regression', 2, None, '___sec9'),
('A top-down approach, recursive binary splitting',
2,
None,
'___sec10'),
('Making a tree', 2, None, '___sec11'),
('Pruning the tree', 2, None, '___sec12'),
('Cost complexity pruning', 2, None, '___sec13'),
('Schematic Regression Procedure', 2, None, '___sec14'),
('A Classification Tree', 2, None, '___sec15'),
('Growing a classification tree', 2, None, '___sec16'),
('Classification tree, how to split nodes', 2, None, '___sec17'),
('Visualizing the Tree, Classification', 2, None, '___sec18'),
('Visualizing the Tree, The Moons', 2, None, '___sec19'),
('Other ways of visualizing the trees', 2, None, '___sec20'),
('Printing out as text', 2, None, '___sec21'),
('Algorithms for Setting up Decision Trees', 2, None, '___sec22'),
('The CART algorithm for Classification', 2, None, '___sec23'),
('The CART algorithm for Regression', 2, None, '___sec24'),
('Computing the Gini index', 2, None, '___sec25'),
('Simple Python Code to read in Data and perform Classification',
2,
None,
'___sec26'),
('Computing the Gini Factor', 2, None, '___sec27'),
('Entropy and the ID3 algorithm', 2, None, '___sec28'),
('Cancer Data again now with Decision Trees and other Methods',
2,
None,
'___sec29'),
('Another example, the moons again', 2, None, '___sec30'),
('Playing around with regions', 2, None, '___sec31'),
('Regression trees', 2, None, '___sec32'),
('Final regressor code', 2, None, '___sec33'),
('Pros and cons of trees, pros', 2, None, '___sec34'),
('Disadvantages', 2, None, '___sec35'),
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
'Boosting, Meet the Jungle of Methods',
2,
None,
'___sec36'),
('An Overview of Ensemble Methods', 2, None, '___sec37'),
('Bagging', 2, None, '___sec38'),
('More bagging', 2, None, '___sec39'),
('Making your own Bootstrap: Changing the Level of the Decision '
'Tree',
2,
None,
'___sec40'),
('Why Voting?', 2, None, '___sec41'),
('Tossing coins', 2, None, '___sec42'),
('Standard imports first', 2, None, '___sec43'),
('Simple Voting Example, head or tail', 2, None, '___sec44'),
('Using the Voting Classifier', 2, None, '___sec45'),
('Voting and Bagging', 2, None, '___sec46'),
('Random forests', 2, None, '___sec47'),
('Random Forest Algorithm', 2, None, '___sec48'),
('Random Forests Compared with other Methods on the Cancer Data',
2,
None,
'___sec49'),
('Compare Bagging on Trees with Random Forests',
2,
None,
'___sec50'),
("Boosting, a Bird's Eye View", 2, None, '___sec51'),
('What is boosting? Additive Modelling/Iterative Fitting',
2,
None,
'___sec52'),
('Iterative Fitting, Regression and Squared-error Cost Function',
2,
None,
'___sec53'),
('Squared-Error Example and Iterative Fitting',
2,
None,
'___sec54'),
('Iterative Fitting, Classification and AdaBoost',
2,
None,
'___sec55'),
('Adaptive Boosting, AdaBoost', 2, None, '___sec56'),
('Building up AdaBoost', 2, None, '___sec57'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
2,
None,
'___sec58'),
('Basic Steps of AdaBoost', 2, None, '___sec59'),
('AdaBoost Examples', 2, None, '___sec60'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
2,
None,
'___sec61'),
('The Squared-Error again! Steepest Descent',
2,
None,
'___sec62'),
('Steepest Descent Example', 2, None, '___sec63'),
('Gradient Boosting, algorithm', 2, None, '___sec64'),
('Gradient Boosting, Examples of Regression',
2,
None,
'___sec65'),
('Gradient Boosting, Classification Example',
2,
None,
'___sec66'),
('XGBoost: Extreme Gradient Boosting', 2, None, '___sec67'),
('Regression Case', 2, None, '___sec68'),
('Xgboost on the Cancer Data', 2, None, '___sec69')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
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>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#___sec0" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#___sec1" style="font-size: 80%;">Decision trees, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#___sec2" style="font-size: 80%;">Basics of a tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#___sec3" style="font-size: 80%;">A Sketch of a Tree, Regression problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#___sec4" style="font-size: 80%;">A Sketch of a Tree, Classification problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#___sec5" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#___sec6" style="font-size: 80%;">General Features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#___sec7" style="font-size: 80%;">How do we set it up?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#___sec8" style="font-size: 80%;">Decision trees and Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#___sec9" style="font-size: 80%;">Building a tree, regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#___sec10" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#___sec11" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#___sec12" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#___sec13" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#___sec14" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#___sec15" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#___sec16" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#___sec17" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#___sec18" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#___sec19" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#___sec20" style="font-size: 80%;">Other ways of visualizing the trees</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#___sec21" style="font-size: 80%;">Printing out as text</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#___sec22" style="font-size: 80%;">Algorithms for Setting up Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">The CART algorithm for Classification</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#___sec24" style="font-size: 80%;">The CART algorithm for Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#___sec25" style="font-size: 80%;">Computing the Gini index</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#___sec26" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#___sec27" style="font-size: 80%;">Computing the Gini Factor</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#___sec28" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#___sec29" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#___sec30" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#___sec31" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#___sec32" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#___sec33" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#___sec34" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#___sec35" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#___sec36" style="font-size: 80%;">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#___sec37" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#___sec38" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs040.html#___sec39" style="font-size: 80%;">More bagging</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs041.html#___sec40" style="font-size: 80%;">Making your own Bootstrap: Changing the Level of the Decision Tree</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs042.html#___sec41" style="font-size: 80%;">Why Voting?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs043.html#___sec42" style="font-size: 80%;">Tossing coins</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs044.html#___sec43" style="font-size: 80%;">Standard imports first</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs045.html#___sec44" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs046.html#___sec45" style="font-size: 80%;">Using the Voting Classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs047.html#___sec46" style="font-size: 80%;">Voting and Bagging</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs048.html#___sec47" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs049.html#___sec48" style="font-size: 80%;">Random Forest Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs050.html#___sec49" style="font-size: 80%;">Random Forests Compared with other Methods on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs051.html#___sec50" style="font-size: 80%;">Compare Bagging on Trees with Random Forests</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs052.html#___sec51" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs053.html#___sec52" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs054.html#___sec53" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs055.html#___sec54" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs056.html#___sec55" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs057.html#___sec56" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs058.html#___sec57" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs059.html#___sec58" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs060.html#___sec59" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs061.html#___sec60" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs062.html#___sec61" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs063.html#___sec62" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs064.html#___sec63" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs065.html#___sec64" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs066.html#___sec65" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs067.html#___sec66" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs068.html#___sec67" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs069.html#___sec68" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs070.html#___sec69" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0024"></a>
<!-- !split -->
<h2 id="___sec23" class="anchor">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>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._week45-bs023.html">&laquo;</a></li>
<li><a href="._week45-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs016.html">17</a></li>
<li><a href="._week45-bs017.html">18</a></li>
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li class="active"><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="._week45-bs026.html">27</a></li>
<li><a href="._week45-bs027.html">28</a></li>
<li><a href="._week45-bs028.html">29</a></li>
<li><a href="._week45-bs029.html">30</a></li>
<li><a href="._week45-bs030.html">31</a></li>
<li><a href="._week45-bs031.html">32</a></li>
<li><a href="._week45-bs032.html">33</a></li>
<li><a href="._week45-bs033.html">34</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs070.html">71</a></li>
<li><a href="._week45-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>