added example

This commit is contained in:
mhjensen
2019-10-25 06:00:25 +02:00
parent 20d45a6142
commit ddb39b03cf
38 changed files with 1564 additions and 2046 deletions
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -176,7 +174,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
<center><h4>Oct 25, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -200,7 +198,7 @@ MathJax.Hub.Config({
<li><a href="._DecisionTrees-bs008.html">9</a></li>
<li><a href="._DecisionTrees-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -205,7 +203,7 @@ and <b>leaf nodes</b> which are then connected by <b>branches</b>.
<li><a href="._DecisionTrees-bs009.html">10</a></li>
<li><a href="._DecisionTrees-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -190,7 +188,7 @@ Then we are essentially done!
<li><a href="._DecisionTrees-bs010.html">11</a></li>
<li><a href="._DecisionTrees-bs011.html">12</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -269,7 +267,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs011.html">12</a></li>
<li><a href="._DecisionTrees-bs012.html">13</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -163,12 +161,7 @@ MathJax.Hub.Config({
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 \)</li>
</ol>
distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).
<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>
@@ -176,15 +169,14 @@ 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 predic- tive model. The goal is to find boxes \( R_1,\dots,R_J \)
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,
$$
where \( \overline{y}_{R_j} \) is the mean response for the training observations
within the $j$th
box.
within box \( j \).
<p>
<p>
@@ -206,7 +198,7 @@ box.
<li><a href="._DecisionTrees-bs012.html">13</a></li>
<li><a href="._DecisionTrees-bs013.html">14</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -194,7 +192,7 @@ better tree in some future step.
<li><a href="._DecisionTrees-bs013.html">14</a></li>
<li><a href="._DecisionTrees-bs014.html">15</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -223,7 +221,7 @@ region contains more than five observations.
<li><a href="._DecisionTrees-bs014.html">15</a></li>
<li><a href="._DecisionTrees-bs015.html">16</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -196,7 +194,7 @@ parameter \( \alpha \).
<li><a href="._DecisionTrees-bs015.html">16</a></li>
<li><a href="._DecisionTrees-bs016.html">17</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -209,7 +207,7 @@ subtree corresponding to \( \alpha \).
<li><a href="._DecisionTrees-bs016.html">17</a></li>
<li><a href="._DecisionTrees-bs017.html">18</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,7 +155,7 @@ MathJax.Hub.Config({
<a name="part0009"></a>
<!-- !split -->
<h2 id="___sec8" class="anchor">A schematic procedure </h2>
<h2 id="___sec8" class="anchor">Schematic Regression Procedure </h2>
<p>
<div class="panel panel-default">
@@ -206,7 +204,7 @@ MathJax.Hub.Config({
<li><a href="._DecisionTrees-bs017.html">18</a></li>
<li><a href="._DecisionTrees-bs018.html">19</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,7 +155,7 @@ MathJax.Hub.Config({
<a name="part0010"></a>
<!-- !split -->
<h2 id="___sec9" class="anchor">A classification tree </h2>
<h2 id="___sec9" class="anchor">A Classification Tree </h2>
<p>
A classification tree is very similar to a regression tree, except
@@ -199,7 +197,7 @@ fall into that region.
<li><a href="._DecisionTrees-bs018.html">19</a></li>
<li><a href="._DecisionTrees-bs019.html">20</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs011.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -204,7 +202,7 @@ than is the classification error rate.
<li><a href="._DecisionTrees-bs019.html">20</a></li>
<li><a href="._DecisionTrees-bs020.html">21</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs012.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -158,6 +156,8 @@ MathJax.Hub.Config({
<!-- !split -->
<h2 id="___sec11" class="anchor">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.
@@ -222,7 +222,7 @@ $$
<li><a href="._DecisionTrees-bs020.html">21</a></li>
<li><a href="._DecisionTrees-bs021.html">22</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs013.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -160,7 +158,7 @@ MathJax.Hub.Config({
<h2 id="___sec12" class="anchor">Entropy and the ID3 algorithm </h2>
<p>
More text to come here.
More text and code to come here.
<p>
<p>
@@ -188,7 +186,7 @@ More text to come here.
<li><a href="._DecisionTrees-bs021.html">22</a></li>
<li><a href="._DecisionTrees-bs022.html">23</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs014.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,94 +155,51 @@ MathJax.Hub.Config({
<a name="part0014"></a>
<!-- !split -->
<h2 id="___sec13" class="anchor">Writing your own code for a classification tree </h2>
<h2 id="___sec13" class="anchor">Cancer Data again now with Decision Trees </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">entropy</span>(target_col):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Calculate the entropy of a dataset.</span>
<span style="color: #BA2121; font-style: italic"> The only parameter of this function is the target_col parameter which specifies the target column</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
elements,counts <span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(target_col,return_counts <span style="color: #666666">=</span> <span style="color: #008000">True</span>)
entropy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum([(<span style="color: #666666">-</span>counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts))<span style="color: #666666">*</span>np<span style="color: #666666">.</span>log2(counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts)) <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(elements))])
<span style="color: #008000; font-weight: bold">return</span> entropy
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">InfoGain</span>(data,split_attribute_name,target_name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;class&quot;</span>):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Calculate the information gain of a dataset. This function takes three parameters:</span>
<span style="color: #BA2121; font-style: italic"> 1. data = The dataset for whose feature the IG should be calculated</span>
<span style="color: #BA2121; font-style: italic"> 2. split_attribute_name = the name of the feature for which the information gain should be calculated</span>
<span style="color: #BA2121; font-style: italic"> 3. target_name = the name of the target feature. The default for this example is &quot;class&quot;</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
<span style="color: #408080; font-style: italic">#Calculate the entropy of the total dataset</span>
total_entropy <span style="color: #666666">=</span> entropy(data[target_name])
<span style="color: #408080; font-style: italic">##Calculate the entropy of the dataset</span>
<span style="color: #408080; font-style: italic">#Calculate the values and the corresponding counts for the split attribute </span>
vals,counts<span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(data[split_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #408080; font-style: italic">#Calculate the weighted entropy</span>
Weighted_Entropy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum([(counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts))<span style="color: #666666">*</span>entropy(data<span style="color: #666666">.</span>where(data[split_attribute_name]<span style="color: #666666">==</span>vals[i])<span style="color: #666666">.</span>dropna()[target_name]) <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(vals))])
<span style="color: #408080; font-style: italic">#Calculate the information gain</span>
Information_Gain <span style="color: #666666">=</span> total_entropy <span style="color: #666666">-</span> Weighted_Entropy
<span style="color: #008000; font-weight: bold">return</span> Information_Gain
<span style="color: #408080; font-style: italic"># Load the data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">ID3</span>(data,originaldata,features,target_attribute_name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;class&quot;</span>,parent_node_class <span style="color: #666666">=</span> <span style="color: #008000">None</span>):
<span style="color: #408080; font-style: italic">#Define the stopping criteria --&gt; If one of this is satisfied, we want to return a leaf node#</span>
<span style="color: #408080; font-style: italic">#If all target_values have the same value, return this value</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(np<span style="color: #666666">.</span>unique(data[target_attribute_name])) <span style="color: #666666">&lt;=</span> <span style="color: #666666">1</span>:
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>unique(data[target_attribute_name])[<span style="color: #666666">0</span>]
<span style="color: #408080; font-style: italic">#If the dataset is empty, return the mode target feature value in the original dataset</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #008000">len</span>(data)<span style="color: #666666">==0</span>:
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>unique(originaldata[target_attribute_name])[np<span style="color: #666666">.</span>argmax(np<span style="color: #666666">.</span>unique(originaldata[target_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)[<span style="color: #666666">1</span>])]
<span style="color: #408080; font-style: italic">#If the feature space is empty, return the mode target feature value of the direct parent node --&gt; Note that</span>
<span style="color: #408080; font-style: italic">#the direct parent node is that node which has called the current run of the ID3 algorithm and hence</span>
<span style="color: #408080; font-style: italic">#the mode target feature value is stored in the parent_node_class variable.</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #008000">len</span>(features) <span style="color: #666666">==0</span>:
<span style="color: #008000; font-weight: bold">return</span> parent_node_class
<span style="color: #408080; font-style: italic">#If none of the above holds true, grow the tree!</span>
<span style="color: #008000; font-weight: bold">else</span>:
<span style="color: #408080; font-style: italic">#Set the default value for this node --&gt; The mode target feature value of the current node</span>
parent_node_class <span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(data[target_attribute_name])[np<span style="color: #666666">.</span>argmax(np<span style="color: #666666">.</span>unique(data[target_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)[<span style="color: #666666">1</span>])]
<span style="color: #408080; font-style: italic">#Select the feature which best splits the dataset</span>
item_values <span style="color: #666666">=</span> [InfoGain(data,feature,target_attribute_name) <span style="color: #008000; font-weight: bold">for</span> feature <span style="color: #AA22FF; font-weight: bold">in</span> features] <span style="color: #408080; font-style: italic">#Return the information gain values for the features in the dataset</span>
best_feature_index <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argmax(item_values)
best_feature <span style="color: #666666">=</span> features[best_feature_index]
<span style="color: #408080; font-style: italic">#Create the tree structure. The root gets the name of the feature (best_feature) with the maximum information</span>
<span style="color: #408080; font-style: italic">#gain in the first run</span>
tree <span style="color: #666666">=</span> {best_feature:{}}
<span style="color: #408080; font-style: italic">#Remove the feature with the best inforamtion gain from the feature space</span>
features <span style="color: #666666">=</span> [i <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> features <span style="color: #008000; font-weight: bold">if</span> i <span style="color: #666666">!=</span> best_feature]
<span style="color: #408080; font-style: italic">#Grow a branch under the root node for each possible value of the root node feature</span>
<span style="color: #008000; font-weight: bold">for</span> value <span style="color: #AA22FF; font-weight: bold">in</span> np<span style="color: #666666">.</span>unique(data[best_feature]):
value <span style="color: #666666">=</span> value
<span style="color: #408080; font-style: italic">#Split the dataset along the value of the feature with the largest information gain and therwith create sub_datasets</span>
sub_data <span style="color: #666666">=</span> data<span style="color: #666666">.</span>where(data[best_feature] <span style="color: #666666">==</span> value)<span style="color: #666666">.</span>dropna()
<span style="color: #408080; font-style: italic">#Call the ID3 algorithm for each of those sub_datasets with the new parameters --&gt; Here the recursion comes in!</span>
subtree <span style="color: #666666">=</span> ID3(sub_data,dataset,features,target_attribute_name,parent_node_class)
<span style="color: #408080; font-style: italic">#Add the sub tree, grown from the sub_dataset to the tree under the root node</span>
tree[best_feature][value] <span style="color: #666666">=</span> subtree
<span style="color: #008000; font-weight: bold">return</span>(tree)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lbfgs&#39;</span>)
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Logistic Regression: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Support vector machine</span>
svm <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">&#39;auto&#39;</span>, C<span style="color: #666666">=100</span>)
svm<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with SVM: {:.2f}&quot;</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Decision Trees</span>
deep_tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=</span><span style="color: #008000">None</span>)
deep_tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Decision Trees: {:.2f}&quot;</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic">#now scale the data</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy Logistic Regression with scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy SVM with scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Decision Trees</span>
deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Decision Trees and scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
</pre></div>
<p>
<p>
@@ -272,7 +227,7 @@ MathJax.Hub.Config({
<li><a href="._DecisionTrees-bs022.html">23</a></li>
<li><a href="._DecisionTrees-bs023.html">24</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs015.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,7 +155,7 @@ MathJax.Hub.Config({
<a name="part0015"></a>
<!-- !split -->
<h2 id="___sec14" class="anchor">Back to moons again </h2>
<h2 id="___sec14" class="anchor">Another example, the moons again </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -252,7 +250,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs023.html">24</a></li>
<li><a href="._DecisionTrees-bs024.html">25</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs016.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -208,7 +206,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs024.html">25</a></li>
<li><a href="._DecisionTrees-bs025.html">26</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs017.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -202,7 +200,7 @@ tree_reg<span style="color: #666666">.</span>fit(X, y)
<li><a href="._DecisionTrees-bs025.html">26</a></li>
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs018.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -258,7 +256,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs019.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,32 +155,18 @@ MathJax.Hub.Config({
<a name="part0019"></a>
<!-- !split -->
<h2 id="___sec18" class="anchor">Classification again: The zoo data </h2>
<p>
<h2 id="___sec18" class="anchor">Pros and cons of trees, pros </h2>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pprint</span> <span style="color: #008000; font-weight: bold">import</span> pprint
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<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>
<span style="color: #408080; font-style: italic">#Import the dataset </span>
dataset <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_csv(<span style="color: #BA2121">&#39;data/zoo.csv&#39;</span>)
<span style="color: #408080; font-style: italic">#We drop the animal names since this is not a good feature to split the data on</span>
<span style="color: #408080; font-style: italic">#dataset=dataset.drop(&#39;animal_name&#39;,axis=1)</span>
<span style="color: #408080; font-style: italic">#Split the data into a training and a testing set</span>
train_features <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[:<span style="color: #666666">80</span>,:<span style="color: #666666">-1</span>]
test_features <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[<span style="color: #666666">80</span>:,:<span style="color: #666666">-1</span>]
train_targets <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[:<span style="color: #666666">80</span>,<span style="color: #666666">-1</span>]
test_targets <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[<span style="color: #666666">80</span>:,<span style="color: #666666">-1</span>]
<span style="color: #408080; font-style: italic">#Train the model</span>
tree <span style="color: #666666">=</span> DecisionTreeClassifier(criterion <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;entropy&#39;</span>)<span style="color: #666666">.</span>fit(train_features,train_targets)
<span style="color: #408080; font-style: italic">#Predict the classes of new, unseen data</span>
prediction <span style="color: #666666">=</span> tree<span style="color: #666666">.</span>predict(test_features)
<span style="color: #408080; font-style: italic">#Check the accuracy</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The prediction accuracy is: &quot;</span>,tree<span style="color: #666666">.</span>score(test_features,test_targets)<span style="color: #666666">*100</span>,<span style="color: #BA2121">&quot;%&quot;</span>)
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -207,8 +191,6 @@ prediction <span style="color: #666666">=</span> tree<span style="color: #666666
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs020.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,18 +155,21 @@ MathJax.Hub.Config({
<a name="part0020"></a>
<!-- !split -->
<h2 id="___sec19" class="anchor">Pros and cons of trees, pros </h2>
<h2 id="___sec19" class="anchor">Disadvantages </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>
<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>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -192,7 +193,6 @@ MathJax.Hub.Config({
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs021.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,19 +155,45 @@ MathJax.Hub.Config({
<a name="part0021"></a>
<!-- !split -->
<h2 id="___sec20" class="anchor">Disadvantages </h2>
<h2 id="___sec20" class="anchor">Bagging </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>
<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.
However, by aggregating many decision trees, using methods like bagging, random forests, and boosting, the predictive performance of trees can be substantially improved.
<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>
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>
<p>
@@ -194,7 +218,6 @@ However, by aggregating many decision trees, using methods like bagging, random
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs022.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,46 +155,23 @@ MathJax.Hub.Config({
<a name="part0022"></a>
<!-- !split -->
<h2 id="___sec21" class="anchor">Bagging </h2>
<h2 id="___sec21" class="anchor">Simple example, head or tail </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>
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.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>heads_proba <span style="color: #666666">=</span> <span style="color: #666666">0.51</span>
coin_tosses <span style="color: #666666">=</span> (np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">10000</span>, <span style="color: #666666">10</span>) <span style="color: #666666">&lt;</span> heads_proba)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>int32)
cumulative_heads_ratio <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cumsum(coin_tosses, axis<span style="color: #666666">=0</span>) <span style="color: #666666">/</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">1</span>, <span style="color: #666666">10001</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">8</span>,<span style="color: #666666">3.5</span>))
plt<span style="color: #666666">.</span>plot(cumulative_heads_ratio)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>], [<span style="color: #666666">0.51</span>, <span style="color: #666666">0.51</span>], <span style="color: #BA2121">&quot;k--&quot;</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;51%&quot;</span>)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>], [<span style="color: #666666">0.5</span>, <span style="color: #666666">0.5</span>], <span style="color: #BA2121">&quot;k-&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;50%&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;Number of coin tosses&quot;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&quot;Heads ratio&quot;</span>)
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">&quot;lower right&quot;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>, <span style="color: #666666">0.42</span>, <span style="color: #666666">0.58</span>])
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -219,7 +194,6 @@ predictor, averaged over all \( B \) trees.
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs023.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,23 +155,45 @@ MathJax.Hub.Config({
<a name="part0023"></a>
<!-- !split -->
<h2 id="___sec22" class="anchor">Simple example, head or tail </h2>
<p>
<h2 id="___sec22" class="anchor">Random forests </h2>
<p>
Random forests provide an improvement over bagged trees by way of a
small tweak that decorrelates the trees.
<p>
As in bagging, we build a
number of decision trees on bootstrapped training samples. But when
building these decision trees, each time a split in a tree is
considered, a random sample of \( m \) predictors is chosen as split
candidates from the full set of \( p \) predictors. The split is allowed to
use only one of those \( m \) predictors.
<p>
A fresh sample of \( m \) predictors is
taken at each split, and typically we choose
$$
m\approx \sqrt{p}.
$$
In building a random forest, at
each split in the tree, the algorithm is not even allowed to consider
a majority of the available predictors.
<p>
The reason for this is rather clever. Suppose that there is one very
strong predictor in the data set, along with a number of other
moderately strong predictors. Then in the collection of bagged
variable importance random forest trees, most or all of the trees will
use this strong predictor in the top split. Consequently, all of the
bagged trees will look quite similar to each other. Hence the
predictions from the bagged trees will be highly correlated.
Unfortunately, averaging many highly correlated quantities does not lead
to as large of a reduction in variance as averaging many uncorrelated
quanti- ties. In particular, this means that bagging will not lead to
a substantial reduction in variance over a single tree in this
setting.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>heads_proba <span style="color: #666666">=</span> <span style="color: #666666">0.51</span>
coin_tosses <span style="color: #666666">=</span> (np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">10000</span>, <span style="color: #666666">10</span>) <span style="color: #666666">&lt;</span> heads_proba)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>int32)
cumulative_heads_ratio <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cumsum(coin_tosses, axis<span style="color: #666666">=0</span>) <span style="color: #666666">/</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">1</span>, <span style="color: #666666">10001</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">8</span>,<span style="color: #666666">3.5</span>))
plt<span style="color: #666666">.</span>plot(cumulative_heads_ratio)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>], [<span style="color: #666666">0.51</span>, <span style="color: #666666">0.51</span>], <span style="color: #BA2121">&quot;k--&quot;</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;51%&quot;</span>)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>], [<span style="color: #666666">0.5</span>, <span style="color: #666666">0.5</span>], <span style="color: #BA2121">&quot;k-&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;50%&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;Number of coin tosses&quot;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&quot;Heads ratio&quot;</span>)
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">&quot;lower right&quot;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">10000</span>, <span style="color: #666666">0.42</span>, <span style="color: #666666">0.58</span>])
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -195,7 +215,6 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs024.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,45 +155,21 @@ MathJax.Hub.Config({
<a name="part0024"></a>
<!-- !split -->
<h2 id="___sec23" class="anchor">Random forests </h2>
<h2 id="___sec23" class="anchor">A simple scikit-learn example </h2>
<p>
Random forests provide an improvement over bagged trees by way of a
small tweak that decorrelates the trees.
<p>
As in bagging, we build a
number of decision trees on bootstrapped training samples. But when
building these decision trees, each time a split in a tree is
considered, a random sample of \( m \) predictors is chosen as split
candidates from the full set of \( p \) predictors. The split is allowed to
use only one of those \( m \) predictors.
<p>
A fresh sample of \( m \) predictors is
taken at each split, and typically we choose
$$
m\approx \sqrt{p}.
$$
In building a random forest, at
each split in the tree, the algorithm is not even allowed to consider
a majority of the available predictors.
<p>
The reason for this is rather clever. Suppose that there is one very
strong predictor in the data set, along with a number of other
moderately strong predictors. Then in the collection of bagged
variable importance random forest trees, most or all of the trees will
use this strong predictor in the top split. Consequently, all of the
bagged trees will look quite similar to each other. Hence the
predictions from the bagged trees will be highly correlated.
Unfortunately, averaging many highly correlated quantities does not lead
to as large of a reduction in variance as averaging many uncorrelated
quanti- ties. In particular, this means that bagging will not lead to
a substantial reduction in variance over a single tree in this
setting.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
<span style="color: #408080; font-style: italic"># Data set not specificied</span>
X <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>XXX
Y <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>YYY
<span style="color: #408080; font-style: italic">#Instantiate the model with 100 trees and entropy as splitting criteria</span>
Random_Forest_model <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=100</span>,criterion<span style="color: #666666">=</span><span style="color: #BA2121">&quot;entropy&quot;</span>)
<span style="color: #408080; font-style: italic">#Cross validation</span>
accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_model,X,Y,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">&#39;test_score&#39;</span>]
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -216,7 +190,6 @@ setting.
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,20 +155,60 @@ MathJax.Hub.Config({
<a name="part0025"></a>
<!-- !split -->
<h2 id="___sec24" class="anchor">A simple scikit-learn example </h2>
<h2 id="___sec24" class="anchor">Please, not the moons again! </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
<span style="color: #408080; font-style: italic"># Data set not specificied</span>
X <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>XXX
Y <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>YYY
<span style="color: #408080; font-style: italic">#Instantiate the model with 100 trees and entropy as splitting criteria</span>
Random_Forest_model <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=100</span>,criterion<span style="color: #666666">=</span><span style="color: #BA2121">&quot;entropy&quot;</span>)
<span style="color: #408080; font-style: italic">#Cross validation</span>
accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_model,X,Y,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">&#39;test_score&#39;</span>]
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=500</span>, noise<span style="color: #666666">=0.30</span>, random_state<span style="color: #666666">=42</span>)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, random_state<span style="color: #666666">=42</span>)
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> VotingClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
log_clf <span style="color: #666666">=</span> LogisticRegression(random_state<span style="color: #666666">=42</span>)
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(random_state<span style="color: #666666">=42</span>)
svm_clf <span style="color: #666666">=</span> SVC(random_state<span style="color: #666666">=42</span>)
voting_clf <span style="color: #666666">=</span> VotingClassifier(
estimators<span style="color: #666666">=</span>[(<span style="color: #BA2121">&#39;lr&#39;</span>, log_clf), (<span style="color: #BA2121">&#39;rf&#39;</span>, rnd_clf), (<span style="color: #BA2121">&#39;svc&#39;</span>, svm_clf)],
voting<span style="color: #666666">=</span><span style="color: #BA2121">&#39;hard&#39;</span>)
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
<span style="color: #008000; font-weight: bold">for</span> clf <span style="color: #AA22FF; font-weight: bold">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(clf<span style="color: #666666">.</span><span style="color: #19177C">__class__</span><span style="color: #666666">.</span><span style="color: #19177C">__name__</span>, accuracy_score(y_test, y_pred))
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>log_clf <span style="color: #666666">=</span> LogisticRegression(random_state<span style="color: #666666">=42</span>)
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(random_state<span style="color: #666666">=42</span>)
svm_clf <span style="color: #666666">=</span> SVC(probability<span style="color: #666666">=</span><span style="color: #008000">True</span>, random_state<span style="color: #666666">=42</span>)
voting_clf <span style="color: #666666">=</span> VotingClassifier(
estimators<span style="color: #666666">=</span>[(<span style="color: #BA2121">&#39;lr&#39;</span>, log_clf), (<span style="color: #BA2121">&#39;rf&#39;</span>, rnd_clf), (<span style="color: #BA2121">&#39;svc&#39;</span>, svm_clf)],
voting<span style="color: #666666">=</span><span style="color: #BA2121">&#39;soft&#39;</span>)
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
<span style="color: #008000; font-weight: bold">for</span> clf <span style="color: #AA22FF; font-weight: bold">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(clf<span style="color: #666666">.</span><span style="color: #19177C">__class__</span><span style="color: #666666">.</span><span style="color: #19177C">__name__</span>, accuracy_score(y_test, y_pred))
</pre></div>
<p>
<p>
@@ -191,7 +229,6 @@ accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_mode
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs026.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,60 +155,63 @@ MathJax.Hub.Config({
<a name="part0026"></a>
<!-- !split -->
<h2 id="___sec25" class="anchor">Please, not the moons again! </h2>
<h2 id="___sec25" class="anchor">Bagging examples </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> BaggingClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=500</span>, noise<span style="color: #666666">=0.30</span>, random_state<span style="color: #666666">=42</span>)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, random_state<span style="color: #666666">=42</span>)
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> VotingClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
log_clf <span style="color: #666666">=</span> LogisticRegression(random_state<span style="color: #666666">=42</span>)
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(random_state<span style="color: #666666">=42</span>)
svm_clf <span style="color: #666666">=</span> SVC(random_state<span style="color: #666666">=42</span>)
voting_clf <span style="color: #666666">=</span> VotingClassifier(
estimators<span style="color: #666666">=</span>[(<span style="color: #BA2121">&#39;lr&#39;</span>, log_clf), (<span style="color: #BA2121">&#39;rf&#39;</span>, rnd_clf), (<span style="color: #BA2121">&#39;svc&#39;</span>, svm_clf)],
voting<span style="color: #666666">=</span><span style="color: #BA2121">&#39;hard&#39;</span>)
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
bag_clf <span style="color: #666666">=</span> BaggingClassifier(
DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>), n_estimators<span style="color: #666666">=500</span>,
max_samples<span style="color: #666666">=100</span>, bootstrap<span style="color: #666666">=</span><span style="color: #008000">True</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
bag_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> bag_clf<span style="color: #666666">.</span>predict(X_test)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
<span style="color: #008000; font-weight: bold">for</span> clf <span style="color: #AA22FF; font-weight: bold">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(clf<span style="color: #666666">.</span><span style="color: #19177C">__class__</span><span style="color: #666666">.</span><span style="color: #19177C">__name__</span>, accuracy_score(y_test, y_pred))
<span style="color: #008000; font-weight: bold">print</span>(accuracy_score(y_test, y_pred))
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>log_clf <span style="color: #666666">=</span> LogisticRegression(random_state<span style="color: #666666">=42</span>)
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(random_state<span style="color: #666666">=42</span>)
svm_clf <span style="color: #666666">=</span> SVC(probability<span style="color: #666666">=</span><span style="color: #008000">True</span>, random_state<span style="color: #666666">=42</span>)
voting_clf <span style="color: #666666">=</span> VotingClassifier(
estimators<span style="color: #666666">=</span>[(<span style="color: #BA2121">&#39;lr&#39;</span>, log_clf), (<span style="color: #BA2121">&#39;rf&#39;</span>, rnd_clf), (<span style="color: #BA2121">&#39;svc&#39;</span>, svm_clf)],
voting<span style="color: #666666">=</span><span style="color: #BA2121">&#39;soft&#39;</span>)
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred_tree <span style="color: #666666">=</span> tree_clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(accuracy_score(y_test, y_pred_tree))
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.colors</span> <span style="color: #008000; font-weight: bold">import</span> ListedColormap
<span style="color: #008000; font-weight: bold">for</span> clf <span style="color: #AA22FF; font-weight: bold">in</span> (log_clf, rnd_clf, svm_clf, voting_clf):
clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(clf<span style="color: #666666">.</span><span style="color: #19177C">__class__</span><span style="color: #666666">.</span><span style="color: #19177C">__name__</span>, accuracy_score(y_test, y_pred))
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_decision_boundary</span>(clf, X, y, axes<span style="color: #666666">=</span>[<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>], alpha<span style="color: #666666">=0.5</span>, contour<span style="color: #666666">=</span><span style="color: #008000">True</span>):
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">0</span>], axes[<span style="color: #666666">1</span>], <span style="color: #666666">100</span>)
x2s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">2</span>], axes[<span style="color: #666666">3</span>], <span style="color: #666666">100</span>)
x1, x2 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>meshgrid(x1s, x2s)
X_new <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[x1<span style="color: #666666">.</span>ravel(), x2<span style="color: #666666">.</span>ravel()]
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_new)<span style="color: #666666">.</span>reshape(x1<span style="color: #666666">.</span>shape)
custom_cmap <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">&#39;#fafab0&#39;</span>,<span style="color: #BA2121">&#39;#9898ff&#39;</span>,<span style="color: #BA2121">&#39;#a0faa0&#39;</span>])
plt<span style="color: #666666">.</span>contourf(x1, x2, y_pred, alpha<span style="color: #666666">=0.3</span>, cmap<span style="color: #666666">=</span>custom_cmap)
<span style="color: #008000; font-weight: bold">if</span> contour:
custom_cmap2 <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">&#39;#7d7d58&#39;</span>,<span style="color: #BA2121">&#39;#4c4c7f&#39;</span>,<span style="color: #BA2121">&#39;#507d50&#39;</span>])
plt<span style="color: #666666">.</span>contour(x1, x2, y_pred, cmap<span style="color: #666666">=</span>custom_cmap2, alpha<span style="color: #666666">=0.8</span>)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;yo&quot;</span>, alpha<span style="color: #666666">=</span>alpha)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;bs&quot;</span>, alpha<span style="color: #666666">=</span>alpha)
plt<span style="color: #666666">.</span>axis(axes)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>,<span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plot_decision_boundary(tree_clf, X, y)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Decision Tree&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plot_decision_boundary(bag_clf, X, y)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Decision Trees with Bagging&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
@@ -230,7 +231,6 @@ voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<li class="active"><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs027.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -157,63 +155,24 @@ MathJax.Hub.Config({
<a name="part0027"></a>
<!-- !split -->
<h2 id="___sec26" class="anchor">Bagging examples </h2>
<h2 id="___sec26" class="anchor">Then random forests </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> BaggingClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>bag_clf <span style="color: #666666">=</span> BaggingClassifier(
DecisionTreeClassifier(splitter<span style="color: #666666">=</span><span style="color: #BA2121">&quot;random&quot;</span>, max_leaf_nodes<span style="color: #666666">=16</span>, random_state<span style="color: #666666">=42</span>),
n_estimators<span style="color: #666666">=500</span>, max_samples<span style="color: #666666">=1.0</span>, bootstrap<span style="color: #666666">=</span><span style="color: #008000">True</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
</pre></div>
<p>
bag_clf <span style="color: #666666">=</span> BaggingClassifier(
DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>), n_estimators<span style="color: #666666">=500</span>,
max_samples<span style="color: #666666">=100</span>, bootstrap<span style="color: #666666">=</span><span style="color: #008000">True</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
bag_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>bag_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> bag_clf<span style="color: #666666">.</span>predict(X_test)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
<span style="color: #008000; font-weight: bold">print</span>(accuracy_score(y_test, y_pred))
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred_tree <span style="color: #666666">=</span> tree_clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">print</span>(accuracy_score(y_test, y_pred_tree))
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.colors</span> <span style="color: #008000; font-weight: bold">import</span> ListedColormap
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_decision_boundary</span>(clf, X, y, axes<span style="color: #666666">=</span>[<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>], alpha<span style="color: #666666">=0.5</span>, contour<span style="color: #666666">=</span><span style="color: #008000">True</span>):
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">0</span>], axes[<span style="color: #666666">1</span>], <span style="color: #666666">100</span>)
x2s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">2</span>], axes[<span style="color: #666666">3</span>], <span style="color: #666666">100</span>)
x1, x2 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>meshgrid(x1s, x2s)
X_new <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[x1<span style="color: #666666">.</span>ravel(), x2<span style="color: #666666">.</span>ravel()]
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X_new)<span style="color: #666666">.</span>reshape(x1<span style="color: #666666">.</span>shape)
custom_cmap <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">&#39;#fafab0&#39;</span>,<span style="color: #BA2121">&#39;#9898ff&#39;</span>,<span style="color: #BA2121">&#39;#a0faa0&#39;</span>])
plt<span style="color: #666666">.</span>contourf(x1, x2, y_pred, alpha<span style="color: #666666">=0.3</span>, cmap<span style="color: #666666">=</span>custom_cmap)
<span style="color: #008000; font-weight: bold">if</span> contour:
custom_cmap2 <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">&#39;#7d7d58&#39;</span>,<span style="color: #BA2121">&#39;#4c4c7f&#39;</span>,<span style="color: #BA2121">&#39;#507d50&#39;</span>])
plt<span style="color: #666666">.</span>contour(x1, x2, y_pred, cmap<span style="color: #666666">=</span>custom_cmap2, alpha<span style="color: #666666">=0.8</span>)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;yo&quot;</span>, alpha<span style="color: #666666">=</span>alpha)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;bs&quot;</span>, alpha<span style="color: #666666">=</span>alpha)
plt<span style="color: #666666">.</span>axis(axes)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>,<span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plot_decision_boundary(tree_clf, X, y)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Decision Tree&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plot_decision_boundary(bag_clf, X, y)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Decision Trees with Bagging&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=500</span>, max_leaf_nodes<span style="color: #666666">=16</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
rnd_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred_rf <span style="color: #666666">=</span> rnd_clf<span style="color: #666666">.</span>predict(X_test)
np<span style="color: #666666">.</span>sum(y_pred <span style="color: #666666">==</span> y_pred_rf) <span style="color: #666666">/</span> <span style="color: #008000">len</span>(y_pred)
</pre></div>
<p>
<p>
@@ -232,7 +191,6 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li class="active"><a href="._DecisionTrees-bs027.html">28</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -155,28 +153,13 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0028"></a>
<!-- !split -->
<!-- !split -->
<h2 id="___sec27" class="anchor">Boosting and more </h2>
More material to come here.
<h2 id="___sec27" class="anchor">Then random forests </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>bag_clf <span style="color: #666666">=</span> BaggingClassifier(
DecisionTreeClassifier(splitter<span style="color: #666666">=</span><span style="color: #BA2121">&quot;random&quot;</span>, max_leaf_nodes<span style="color: #666666">=16</span>, random_state<span style="color: #666666">=42</span>),
n_estimators<span style="color: #666666">=500</span>, max_samples<span style="color: #666666">=1.0</span>, bootstrap<span style="color: #666666">=</span><span style="color: #008000">True</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>bag_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> bag_clf<span style="color: #666666">.</span>predict(X_test)
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=500</span>, max_leaf_nodes<span style="color: #666666">=16</span>, n_jobs<span style="color: #666666">=-1</span>, random_state<span style="color: #666666">=42</span>)
rnd_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred_rf <span style="color: #666666">=</span> rnd_clf<span style="color: #666666">.</span>predict(X_test)
np<span style="color: #666666">.</span>sum(y_pred <span style="color: #666666">==</span> y_pred_rf) <span style="color: #666666">/</span> <span style="color: #008000">len</span>(y_pred)
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -192,8 +175,6 @@ np<span style="color: #666666">.</span>sum(y_pred <span style="color: #666666">=
<li><a href="._DecisionTrees-bs026.html">27</a></li>
<li><a href="._DecisionTrees-bs027.html">28</a></li>
<li class="active"><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs029.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -52,30 +52,29 @@ Automatically generated HTML file from DocOnce source
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -121,27 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">A schematic procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Writing your own code for a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Back to moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Cancer Data again now with Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Classification again: The zoo data</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Boosting and more</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Simple example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">A simple scikit-learn example</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Please, not the moons again!</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Bagging examples</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Then random forests</a></li>
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Boosting and more</a></li>
</ul>
</li>
@@ -176,7 +174,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
<center><h4>Oct 25, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -200,7 +198,7 @@ MathJax.Hub.Config({
<li><a href="._DecisionTrees-bs008.html">9</a></li>
<li><a href="._DecisionTrees-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._DecisionTrees-bs029.html">30</a></li>
<li><a href="._DecisionTrees-bs028.html">29</a></li>
<li><a href="._DecisionTrees-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>&nbsp;<br>
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
<center><h4>Oct 25, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -315,13 +315,8 @@ plt.show()
There are mainly two steps
<ol>
<p><li> We split the predictor space (the set of possible values \( x_1,x_2,\dots, x_p \)) into \( J \)</li>
</ol>
<p>
<p><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>
distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).
<ol>
<p><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>
<p>
@@ -330,7 +325,7 @@ 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 predic- tive model. The goal is to find boxes \( R_1,\dots,R_J \)
resulting predictive model. The goal is to find boxes \( R_1,\dots,R_J \)
that minimize the MSE, given by
<p>&nbsp;<br>
$$
@@ -339,8 +334,7 @@ $$
<p>&nbsp;<br>
where \( \overline{y}_{R_j} \) is the mean response for the training observations
within the $j$th
box.
within box \( j \).
</section>
@@ -469,7 +463,7 @@ subtree corresponding to \( \alpha \).
<section>
<h2 id="___sec8">A schematic procedure </h2>
<h2 id="___sec8">Schematic Regression Procedure </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -494,7 +488,7 @@ subtree corresponding to \( \alpha \).
<section>
<h2 id="___sec9">A classification tree </h2>
<h2 id="___sec9">A Classification Tree </h2>
<p>
A classification tree is very similar to a regression tree, except
@@ -538,6 +532,8 @@ than is the classification error rate.
<section>
<h2 id="___sec11">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.
@@ -587,105 +583,62 @@ $$
<h2 id="___sec12">Entropy and the ID3 algorithm </h2>
<p>
More text to come here.
More text and code to come here.
</section>
<section>
<h2 id="___sec13">Writing your own code for a classification tree </h2>
<h2 id="___sec13">Cancer Data again now with Decision Trees </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">entropy</span>(target_col):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Calculate the entropy of a dataset.</span>
<span style="color: #CD5555"> The only parameter of this function is the target_col parameter which specifies the target column</span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
elements,counts = np.unique(target_col,return_counts = <span style="color: #658b00">True</span>)
entropy = np.sum([(-counts[i]/np.sum(counts))*np.log2(counts[i]/np.sum(counts)) <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(elements))])
<span style="color: #8B008B; font-weight: bold">return</span> entropy
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">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: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">InfoGain</span>(data,split_attribute_name,target_name=<span style="color: #CD5555">&quot;class&quot;</span>):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Calculate the information gain of a dataset. This function takes three parameters:</span>
<span style="color: #CD5555"> 1. data = The dataset for whose feature the IG should be calculated</span>
<span style="color: #CD5555"> 2. split_attribute_name = the name of the feature for which the information gain should be calculated</span>
<span style="color: #CD5555"> 3. target_name = the name of the target feature. The default for this example is &quot;class&quot;</span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
<span style="color: #228B22">#Calculate the entropy of the total dataset</span>
total_entropy = entropy(data[target_name])
<span style="color: #228B22">##Calculate the entropy of the dataset</span>
<span style="color: #228B22">#Calculate the values and the corresponding counts for the split attribute </span>
vals,counts= np.unique(data[split_attribute_name],return_counts=<span style="color: #658b00">True</span>)
<span style="color: #228B22">#Calculate the weighted entropy</span>
Weighted_Entropy = np.sum([(counts[i]/np.sum(counts))*entropy(data.where(data[split_attribute_name]==vals[i]).dropna()[target_name]) <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(vals))])
<span style="color: #228B22">#Calculate the information gain</span>
Information_Gain = total_entropy - Weighted_Entropy
<span style="color: #8B008B; font-weight: bold">return</span> Information_Gain
<span style="color: #228B22"># Load the data</span>
cancer = load_breast_cancer()
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">ID3</span>(data,originaldata,features,target_attribute_name=<span style="color: #CD5555">&quot;class&quot;</span>,parent_node_class = <span style="color: #658b00">None</span>):
<span style="color: #228B22">#Define the stopping criteria --&gt; If one of this is satisfied, we want to return a leaf node#</span>
<span style="color: #228B22">#If all target_values have the same value, return this value</span>
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(np.unique(data[target_attribute_name])) &lt;= <span style="color: #B452CD">1</span>:
<span style="color: #8B008B; font-weight: bold">return</span> np.unique(data[target_attribute_name])[<span style="color: #B452CD">0</span>]
<span style="color: #228B22">#If the dataset is empty, return the mode target feature value in the original dataset</span>
<span style="color: #8B008B; font-weight: bold">elif</span> <span style="color: #658b00">len</span>(data)==<span style="color: #B452CD">0</span>:
<span style="color: #8B008B; font-weight: bold">return</span> np.unique(originaldata[target_attribute_name])[np.argmax(np.unique(originaldata[target_attribute_name],return_counts=<span style="color: #658b00">True</span>)[<span style="color: #B452CD">1</span>])]
<span style="color: #228B22">#If the feature space is empty, return the mode target feature value of the direct parent node --&gt; Note that</span>
<span style="color: #228B22">#the direct parent node is that node which has called the current run of the ID3 algorithm and hence</span>
<span style="color: #228B22">#the mode target feature value is stored in the parent_node_class variable.</span>
<span style="color: #8B008B; font-weight: bold">elif</span> <span style="color: #658b00">len</span>(features) ==<span style="color: #B452CD">0</span>:
<span style="color: #8B008B; font-weight: bold">return</span> parent_node_class
<span style="color: #228B22">#If none of the above holds true, grow the tree!</span>
<span style="color: #8B008B; font-weight: bold">else</span>:
<span style="color: #228B22">#Set the default value for this node --&gt; The mode target feature value of the current node</span>
parent_node_class = np.unique(data[target_attribute_name])[np.argmax(np.unique(data[target_attribute_name],return_counts=<span style="color: #658b00">True</span>)[<span style="color: #B452CD">1</span>])]
<span style="color: #228B22">#Select the feature which best splits the dataset</span>
item_values = [InfoGain(data,feature,target_attribute_name) <span style="color: #8B008B; font-weight: bold">for</span> feature <span style="color: #8B008B">in</span> features] <span style="color: #228B22">#Return the information gain values for the features in the dataset</span>
best_feature_index = np.argmax(item_values)
best_feature = features[best_feature_index]
<span style="color: #228B22">#Create the tree structure. The root gets the name of the feature (best_feature) with the maximum information</span>
<span style="color: #228B22">#gain in the first run</span>
tree = {best_feature:{}}
<span style="color: #228B22">#Remove the feature with the best inforamtion gain from the feature space</span>
features = [i <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> features <span style="color: #8B008B; font-weight: bold">if</span> i != best_feature]
<span style="color: #228B22">#Grow a branch under the root node for each possible value of the root node feature</span>
<span style="color: #8B008B; font-weight: bold">for</span> value <span style="color: #8B008B">in</span> np.unique(data[best_feature]):
value = value
<span style="color: #228B22">#Split the dataset along the value of the feature with the largest information gain and therwith create sub_datasets</span>
sub_data = data.where(data[best_feature] == value).dropna()
<span style="color: #228B22">#Call the ID3 algorithm for each of those sub_datasets with the new parameters --&gt; Here the recursion comes in!</span>
subtree = ID3(sub_data,dataset,features,target_attribute_name,parent_node_class)
<span style="color: #228B22">#Add the sub tree, grown from the sub_dataset to the tree under the root node</span>
tree[best_feature][value] = subtree
<span style="color: #8B008B; font-weight: bold">return</span>(tree)
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: #8B008B; font-weight: bold">print</span>(X_train.shape)
<span style="color: #8B008B; font-weight: bold">print</span>(X_test.shape)
<span style="color: #228B22"># Logistic Regression</span>
logreg = LogisticRegression(solver=<span style="color: #CD5555">&#39;lbfgs&#39;</span>)
logreg.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Logistic Regression: {:.2f}&quot;</span>.format(logreg.score(X_test,y_test)))
<span style="color: #228B22"># Support vector machine</span>
svm = SVC(gamma=<span style="color: #CD5555">&#39;auto&#39;</span>, C=<span style="color: #B452CD">100</span>)
svm.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with SVM: {:.2f}&quot;</span>.format(svm.score(X_test,y_test)))
<span style="color: #228B22"># Decision Trees</span>
deep_tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #658b00">None</span>)
deep_tree_clf.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Decision Trees: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy Logistic Regression with scaled data: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy SVM with scaled data: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Decision Trees and scaled data: {:.2f}&quot;</span>.format(deep_tree_clf.score(X_test_scaled,y_test)))
</pre></div>
</section>
<section>
<h2 id="___sec14">Back to moons again </h2>
<h2 id="___sec14">Another example, the moons again </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -889,36 +842,7 @@ plt.show()
<section>
<h2 id="___sec18">Classification again: The zoo data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">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>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pprint</span> <span style="color: #8B008B; font-weight: bold">import</span> pprint
<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">#Import the dataset </span>
dataset = pd.read_csv(<span style="color: #CD5555">&#39;data/zoo.csv&#39;</span>)
<span style="color: #228B22">#We drop the animal names since this is not a good feature to split the data on</span>
<span style="color: #228B22">#dataset=dataset.drop(&#39;animal_name&#39;,axis=1)</span>
<span style="color: #228B22">#Split the data into a training and a testing set</span>
train_features = dataset.iloc[:<span style="color: #B452CD">80</span>,:-<span style="color: #B452CD">1</span>]
test_features = dataset.iloc[<span style="color: #B452CD">80</span>:,:-<span style="color: #B452CD">1</span>]
train_targets = dataset.iloc[:<span style="color: #B452CD">80</span>,-<span style="color: #B452CD">1</span>]
test_targets = dataset.iloc[<span style="color: #B452CD">80</span>:,-<span style="color: #B452CD">1</span>]
<span style="color: #228B22">#Train the model</span>
tree = DecisionTreeClassifier(criterion = <span style="color: #CD5555">&#39;entropy&#39;</span>).fit(train_features,train_targets)
<span style="color: #228B22">#Predict the classes of new, unseen data</span>
prediction = tree.predict(test_features)
<span style="color: #228B22">#Check the accuracy</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;The prediction accuracy is: &quot;</span>,tree.score(test_features,test_targets)*<span style="color: #B452CD">100</span>,<span style="color: #CD5555">&quot;%&quot;</span>)
</pre></div>
</section>
<section>
<h2 id="___sec19">Pros and cons of trees, pros </h2>
<h2 id="___sec18">Pros and cons of trees, pros </h2>
<ul>
<p><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>
@@ -933,7 +857,7 @@ prediction = tree.predict(test_features)
<section>
<h2 id="___sec20">Disadvantages </h2>
<h2 id="___sec19">Disadvantages </h2>
<ul>
<p><li> Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches</li>
@@ -951,7 +875,7 @@ However, by aggregating many decision trees, using methods like bagging, random
<section>
<h2 id="___sec21">Bagging </h2>
<h2 id="___sec20">Bagging </h2>
<p>
The <b>plain</b> decision trees suffer from high
@@ -994,7 +918,7 @@ predictor, averaged over all \( B \) trees.
<section>
<h2 id="___sec22">Simple example, head or tail </h2>
<h2 id="___sec21">Simple example, head or tail </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1015,7 +939,7 @@ plt.show()
<section>
<h2 id="___sec23">Random forests </h2>
<h2 id="___sec22">Random forests </h2>
<p>
Random forests provide an improvement over bagged trees by way of a
@@ -1059,7 +983,7 @@ setting.
<section>
<h2 id="___sec24">A simple scikit-learn example </h2>
<h2 id="___sec23">A simple scikit-learn example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1078,7 +1002,7 @@ accuracy = cross_validate(Random_Forest_model,X,Y,cv=<span style="color: #B452CD
<section>
<h2 id="___sec25">Please, not the moons again! </h2>
<h2 id="___sec24">Please, not the moons again! </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1137,7 +1061,7 @@ voting_clf.fit(X_train, y_train)
<section>
<h2 id="___sec26">Bagging examples </h2>
<h2 id="___sec25">Bagging examples </h2>
<p>
@@ -1199,7 +1123,7 @@ plt.show()
<section>
<h2 id="___sec27">Then random forests </h2>
<h2 id="___sec26">Then random forests </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1222,7 +1146,7 @@ np.sum(y_pred == y_pred_rf) / <span style="color: #658b00">len</span>(y_pred)
<section>
<h2 id="___sec28">Boosting and more </h2>
<h2 id="___sec27">Boosting and more </h2>
More material to come here.
</section>
@@ -72,30 +72,29 @@ div { text-align: justify; text-justify: inter-word; }
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -137,7 +136,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
<center><h4>Oct 25, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -296,12 +295,7 @@ plt.show()
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 \)</li>
</ol>
distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).
<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>
@@ -309,15 +303,14 @@ 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 predic- tive model. The goal is to find boxes \( R_1,\dots,R_J \)
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,
$$
where \( \overline{y}_{R_j} \) is the mean response for the training observations
within the $j$th
box.
within box \( j \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -438,7 +431,7 @@ subtree corresponding to \( \alpha \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">A schematic procedure </h2>
<h2 id="___sec8">Schematic Regression Procedure </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -464,7 +457,7 @@ subtree corresponding to \( \alpha \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">A classification tree </h2>
<h2 id="___sec9">A Classification Tree </h2>
<p>
A classification tree is very similar to a regression tree, except
@@ -508,6 +501,8 @@ than is the classification error rate.
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">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.
@@ -552,104 +547,61 @@ $$
<h2 id="___sec12">Entropy and the ID3 algorithm </h2>
<p>
More text to come here.
More text and code to come here.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Writing your own code for a classification tree </h2>
<h2 id="___sec13">Cancer Data again now with Decision 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: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">entropy</span>(target_col):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Calculate the entropy of a dataset.</span>
<span style="color: #CD5555"> The only parameter of this function is the target_col parameter which specifies the target column</span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
elements,counts = np.unique(target_col,return_counts = <span style="color: #658b00">True</span>)
entropy = np.sum([(-counts[i]/np.sum(counts))*np.log2(counts[i]/np.sum(counts)) <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(elements))])
<span style="color: #8B008B; font-weight: bold">return</span> entropy
<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: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">InfoGain</span>(data,split_attribute_name,target_name=<span style="color: #CD5555">&quot;class&quot;</span>):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Calculate the information gain of a dataset. This function takes three parameters:</span>
<span style="color: #CD5555"> 1. data = The dataset for whose feature the IG should be calculated</span>
<span style="color: #CD5555"> 2. split_attribute_name = the name of the feature for which the information gain should be calculated</span>
<span style="color: #CD5555"> 3. target_name = the name of the target feature. The default for this example is &quot;class&quot;</span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
<span style="color: #228B22">#Calculate the entropy of the total dataset</span>
total_entropy = entropy(data[target_name])
<span style="color: #228B22">##Calculate the entropy of the dataset</span>
<span style="color: #228B22">#Calculate the values and the corresponding counts for the split attribute </span>
vals,counts= np.unique(data[split_attribute_name],return_counts=<span style="color: #658b00">True</span>)
<span style="color: #228B22">#Calculate the weighted entropy</span>
Weighted_Entropy = np.sum([(counts[i]/np.sum(counts))*entropy(data.where(data[split_attribute_name]==vals[i]).dropna()[target_name]) <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(vals))])
<span style="color: #228B22">#Calculate the information gain</span>
Information_Gain = total_entropy - Weighted_Entropy
<span style="color: #8B008B; font-weight: bold">return</span> Information_Gain
<span style="color: #228B22"># Load the data</span>
cancer = load_breast_cancer()
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">ID3</span>(data,originaldata,features,target_attribute_name=<span style="color: #CD5555">&quot;class&quot;</span>,parent_node_class = <span style="color: #658b00">None</span>):
<span style="color: #228B22">#Define the stopping criteria --&gt; If one of this is satisfied, we want to return a leaf node#</span>
<span style="color: #228B22">#If all target_values have the same value, return this value</span>
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(np.unique(data[target_attribute_name])) &lt;= <span style="color: #B452CD">1</span>:
<span style="color: #8B008B; font-weight: bold">return</span> np.unique(data[target_attribute_name])[<span style="color: #B452CD">0</span>]
<span style="color: #228B22">#If the dataset is empty, return the mode target feature value in the original dataset</span>
<span style="color: #8B008B; font-weight: bold">elif</span> <span style="color: #658b00">len</span>(data)==<span style="color: #B452CD">0</span>:
<span style="color: #8B008B; font-weight: bold">return</span> np.unique(originaldata[target_attribute_name])[np.argmax(np.unique(originaldata[target_attribute_name],return_counts=<span style="color: #658b00">True</span>)[<span style="color: #B452CD">1</span>])]
<span style="color: #228B22">#If the feature space is empty, return the mode target feature value of the direct parent node --&gt; Note that</span>
<span style="color: #228B22">#the direct parent node is that node which has called the current run of the ID3 algorithm and hence</span>
<span style="color: #228B22">#the mode target feature value is stored in the parent_node_class variable.</span>
<span style="color: #8B008B; font-weight: bold">elif</span> <span style="color: #658b00">len</span>(features) ==<span style="color: #B452CD">0</span>:
<span style="color: #8B008B; font-weight: bold">return</span> parent_node_class
<span style="color: #228B22">#If none of the above holds true, grow the tree!</span>
<span style="color: #8B008B; font-weight: bold">else</span>:
<span style="color: #228B22">#Set the default value for this node --&gt; The mode target feature value of the current node</span>
parent_node_class = np.unique(data[target_attribute_name])[np.argmax(np.unique(data[target_attribute_name],return_counts=<span style="color: #658b00">True</span>)[<span style="color: #B452CD">1</span>])]
<span style="color: #228B22">#Select the feature which best splits the dataset</span>
item_values = [InfoGain(data,feature,target_attribute_name) <span style="color: #8B008B; font-weight: bold">for</span> feature <span style="color: #8B008B">in</span> features] <span style="color: #228B22">#Return the information gain values for the features in the dataset</span>
best_feature_index = np.argmax(item_values)
best_feature = features[best_feature_index]
<span style="color: #228B22">#Create the tree structure. The root gets the name of the feature (best_feature) with the maximum information</span>
<span style="color: #228B22">#gain in the first run</span>
tree = {best_feature:{}}
<span style="color: #228B22">#Remove the feature with the best inforamtion gain from the feature space</span>
features = [i <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> features <span style="color: #8B008B; font-weight: bold">if</span> i != best_feature]
<span style="color: #228B22">#Grow a branch under the root node for each possible value of the root node feature</span>
<span style="color: #8B008B; font-weight: bold">for</span> value <span style="color: #8B008B">in</span> np.unique(data[best_feature]):
value = value
<span style="color: #228B22">#Split the dataset along the value of the feature with the largest information gain and therwith create sub_datasets</span>
sub_data = data.where(data[best_feature] == value).dropna()
<span style="color: #228B22">#Call the ID3 algorithm for each of those sub_datasets with the new parameters --&gt; Here the recursion comes in!</span>
subtree = ID3(sub_data,dataset,features,target_attribute_name,parent_node_class)
<span style="color: #228B22">#Add the sub tree, grown from the sub_dataset to the tree under the root node</span>
tree[best_feature][value] = subtree
<span style="color: #8B008B; font-weight: bold">return</span>(tree)
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: #8B008B; font-weight: bold">print</span>(X_train.shape)
<span style="color: #8B008B; font-weight: bold">print</span>(X_test.shape)
<span style="color: #228B22"># Logistic Regression</span>
logreg = LogisticRegression(solver=<span style="color: #CD5555">&#39;lbfgs&#39;</span>)
logreg.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Logistic Regression: {:.2f}&quot;</span>.format(logreg.score(X_test,y_test)))
<span style="color: #228B22"># Support vector machine</span>
svm = SVC(gamma=<span style="color: #CD5555">&#39;auto&#39;</span>, C=<span style="color: #B452CD">100</span>)
svm.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with SVM: {:.2f}&quot;</span>.format(svm.score(X_test,y_test)))
<span style="color: #228B22"># Decision Trees</span>
deep_tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #658b00">None</span>)
deep_tree_clf.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Decision Trees: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy Logistic Regression with scaled data: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy SVM with scaled data: {:.2f}&quot;</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: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy with Decision Trees and scaled data: {:.2f}&quot;</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="___sec14">Back to moons again </h2>
<h2 id="___sec14">Another example, the moons again </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -849,35 +801,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec18">Classification again: The zoo data </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">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>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pprint</span> <span style="color: #8B008B; font-weight: bold">import</span> pprint
<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">#Import the dataset </span>
dataset = pd.read_csv(<span style="color: #CD5555">&#39;data/zoo.csv&#39;</span>)
<span style="color: #228B22">#We drop the animal names since this is not a good feature to split the data on</span>
<span style="color: #228B22">#dataset=dataset.drop(&#39;animal_name&#39;,axis=1)</span>
<span style="color: #228B22">#Split the data into a training and a testing set</span>
train_features = dataset.iloc[:<span style="color: #B452CD">80</span>,:-<span style="color: #B452CD">1</span>]
test_features = dataset.iloc[<span style="color: #B452CD">80</span>:,:-<span style="color: #B452CD">1</span>]
train_targets = dataset.iloc[:<span style="color: #B452CD">80</span>,-<span style="color: #B452CD">1</span>]
test_targets = dataset.iloc[<span style="color: #B452CD">80</span>:,-<span style="color: #B452CD">1</span>]
<span style="color: #228B22">#Train the model</span>
tree = DecisionTreeClassifier(criterion = <span style="color: #CD5555">&#39;entropy&#39;</span>).fit(train_features,train_targets)
<span style="color: #228B22">#Predict the classes of new, unseen data</span>
prediction = tree.predict(test_features)
<span style="color: #228B22">#Check the accuracy</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;The prediction accuracy is: &quot;</span>,tree.score(test_features,test_targets)*<span style="color: #B452CD">100</span>,<span style="color: #CD5555">&quot;%&quot;</span>)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec19">Pros and cons of trees, pros </h2>
<h2 id="___sec18">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>
@@ -891,7 +815,7 @@ prediction = tree.predict(test_features)
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">Disadvantages </h2>
<h2 id="___sec19">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>
@@ -908,7 +832,7 @@ However, by aggregating many decision trees, using methods like bagging, random
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">Bagging </h2>
<h2 id="___sec20">Bagging </h2>
<p>
The <b>plain</b> decision trees suffer from high
@@ -951,7 +875,7 @@ predictor, averaged over all \( B \) trees.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">Simple example, head or tail </h2>
<h2 id="___sec21">Simple example, head or tail </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -971,7 +895,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec23">Random forests </h2>
<h2 id="___sec22">Random forests </h2>
<p>
Random forests provide an improvement over bagged trees by way of a
@@ -1013,7 +937,7 @@ setting.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">A simple scikit-learn example </h2>
<h2 id="___sec23">A simple scikit-learn example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1031,7 +955,7 @@ accuracy = cross_validate(Random_Forest_model,X,Y,cv=<span style="color: #B452CD
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">Please, not the moons again! </h2>
<h2 id="___sec24">Please, not the moons again! </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1089,7 +1013,7 @@ voting_clf.fit(X_train, y_train)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec26">Bagging examples </h2>
<h2 id="___sec25">Bagging examples </h2>
<p>
@@ -1150,7 +1074,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec27">Then random forests </h2>
<h2 id="___sec26">Then random forests </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1172,7 +1096,7 @@ np.sum(y_pred == y_pred_rf) / <span style="color: #658b00">len</span>(y_pred)
<p>
<!-- !split -->
<h2 id="___sec28">Boosting and more </h2>
<h2 id="___sec27">Boosting and more </h2>
More material to come here.
<p>
+74 -150
View File
@@ -77,30 +77,29 @@ div { text-align: justify; text-justify: inter-word; }
('Making a tree', 2, None, '___sec5'),
('Pruning the tree', 2, None, '___sec6'),
('Cost complexity pruning', 2, None, '___sec7'),
('A schematic procedure', 2, None, '___sec8'),
('A classification tree', 2, None, '___sec9'),
('Schematic Regression Procedure', 2, None, '___sec8'),
('A Classification Tree', 2, None, '___sec9'),
('Growing a classification tree', 2, None, '___sec10'),
('Classification tree, how to split nodes', 2, None, '___sec11'),
('Entropy and the ID3 algorithm', 2, None, '___sec12'),
('Writing your own code for a classification tree',
('Cancer Data again now with Decision Trees',
2,
None,
'___sec13'),
('Back to moons again', 2, None, '___sec14'),
('Another example, the moons again', 2, None, '___sec14'),
('Playing around with regions', 2, None, '___sec15'),
('Regression trees', 2, None, '___sec16'),
('Final regressor code', 2, None, '___sec17'),
('Classification again: The zoo data', 2, None, '___sec18'),
('Pros and cons of trees, pros', 2, None, '___sec19'),
('Disadvantages', 2, None, '___sec20'),
('Bagging', 2, None, '___sec21'),
('Simple example, head or tail', 2, None, '___sec22'),
('Random forests', 2, None, '___sec23'),
('A simple scikit-learn example', 2, None, '___sec24'),
('Please, not the moons again!', 2, None, '___sec25'),
('Bagging examples', 2, None, '___sec26'),
('Then random forests', 2, None, '___sec27'),
('Boosting and more', 2, None, '___sec28')]}
('Pros and cons of trees, pros', 2, None, '___sec18'),
('Disadvantages', 2, None, '___sec19'),
('Bagging', 2, None, '___sec20'),
('Simple example, head or tail', 2, None, '___sec21'),
('Random forests', 2, None, '___sec22'),
('A simple scikit-learn example', 2, None, '___sec23'),
('Please, not the moons again!', 2, None, '___sec24'),
('Bagging examples', 2, None, '___sec25'),
('Then random forests', 2, None, '___sec26'),
('Boosting and more', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -142,7 +141,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
<center><h4>Oct 25, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -301,12 +300,7 @@ plt<span style="color: #666666">.</span>show()
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 \)</li>
</ol>
distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).
<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>
@@ -314,15 +308,14 @@ 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 predic- tive model. The goal is to find boxes \( R_1,\dots,R_J \)
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,
$$
where \( \overline{y}_{R_j} \) is the mean response for the training observations
within the $j$th
box.
within box \( j \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -443,7 +436,7 @@ subtree corresponding to \( \alpha \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">A schematic procedure </h2>
<h2 id="___sec8">Schematic Regression Procedure </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -469,7 +462,7 @@ subtree corresponding to \( \alpha \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">A classification tree </h2>
<h2 id="___sec9">A Classification Tree </h2>
<p>
A classification tree is very similar to a regression tree, except
@@ -513,6 +506,8 @@ than is the classification error rate.
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">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.
@@ -557,104 +552,61 @@ $$
<h2 id="___sec12">Entropy and the ID3 algorithm </h2>
<p>
More text to come here.
More text and code to come here.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Writing your own code for a classification tree </h2>
<h2 id="___sec13">Cancer Data again now with Decision Trees </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">entropy</span>(target_col):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Calculate the entropy of a dataset.</span>
<span style="color: #BA2121; font-style: italic"> The only parameter of this function is the target_col parameter which specifies the target column</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
elements,counts <span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(target_col,return_counts <span style="color: #666666">=</span> <span style="color: #008000">True</span>)
entropy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum([(<span style="color: #666666">-</span>counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts))<span style="color: #666666">*</span>np<span style="color: #666666">.</span>log2(counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts)) <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(elements))])
<span style="color: #008000; font-weight: bold">return</span> entropy
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">InfoGain</span>(data,split_attribute_name,target_name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;class&quot;</span>):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Calculate the information gain of a dataset. This function takes three parameters:</span>
<span style="color: #BA2121; font-style: italic"> 1. data = The dataset for whose feature the IG should be calculated</span>
<span style="color: #BA2121; font-style: italic"> 2. split_attribute_name = the name of the feature for which the information gain should be calculated</span>
<span style="color: #BA2121; font-style: italic"> 3. target_name = the name of the target feature. The default for this example is &quot;class&quot;</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
<span style="color: #408080; font-style: italic">#Calculate the entropy of the total dataset</span>
total_entropy <span style="color: #666666">=</span> entropy(data[target_name])
<span style="color: #408080; font-style: italic">##Calculate the entropy of the dataset</span>
<span style="color: #408080; font-style: italic">#Calculate the values and the corresponding counts for the split attribute </span>
vals,counts<span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(data[split_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #408080; font-style: italic">#Calculate the weighted entropy</span>
Weighted_Entropy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum([(counts[i]<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sum(counts))<span style="color: #666666">*</span>entropy(data<span style="color: #666666">.</span>where(data[split_attribute_name]<span style="color: #666666">==</span>vals[i])<span style="color: #666666">.</span>dropna()[target_name]) <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(vals))])
<span style="color: #408080; font-style: italic">#Calculate the information gain</span>
Information_Gain <span style="color: #666666">=</span> total_entropy <span style="color: #666666">-</span> Weighted_Entropy
<span style="color: #008000; font-weight: bold">return</span> Information_Gain
<span style="color: #408080; font-style: italic"># Load the data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">ID3</span>(data,originaldata,features,target_attribute_name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;class&quot;</span>,parent_node_class <span style="color: #666666">=</span> <span style="color: #008000">None</span>):
<span style="color: #408080; font-style: italic">#Define the stopping criteria --&gt; If one of this is satisfied, we want to return a leaf node#</span>
<span style="color: #408080; font-style: italic">#If all target_values have the same value, return this value</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(np<span style="color: #666666">.</span>unique(data[target_attribute_name])) <span style="color: #666666">&lt;=</span> <span style="color: #666666">1</span>:
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>unique(data[target_attribute_name])[<span style="color: #666666">0</span>]
<span style="color: #408080; font-style: italic">#If the dataset is empty, return the mode target feature value in the original dataset</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #008000">len</span>(data)<span style="color: #666666">==0</span>:
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>unique(originaldata[target_attribute_name])[np<span style="color: #666666">.</span>argmax(np<span style="color: #666666">.</span>unique(originaldata[target_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)[<span style="color: #666666">1</span>])]
<span style="color: #408080; font-style: italic">#If the feature space is empty, return the mode target feature value of the direct parent node --&gt; Note that</span>
<span style="color: #408080; font-style: italic">#the direct parent node is that node which has called the current run of the ID3 algorithm and hence</span>
<span style="color: #408080; font-style: italic">#the mode target feature value is stored in the parent_node_class variable.</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #008000">len</span>(features) <span style="color: #666666">==0</span>:
<span style="color: #008000; font-weight: bold">return</span> parent_node_class
<span style="color: #408080; font-style: italic">#If none of the above holds true, grow the tree!</span>
<span style="color: #008000; font-weight: bold">else</span>:
<span style="color: #408080; font-style: italic">#Set the default value for this node --&gt; The mode target feature value of the current node</span>
parent_node_class <span style="color: #666666">=</span> np<span style="color: #666666">.</span>unique(data[target_attribute_name])[np<span style="color: #666666">.</span>argmax(np<span style="color: #666666">.</span>unique(data[target_attribute_name],return_counts<span style="color: #666666">=</span><span style="color: #008000">True</span>)[<span style="color: #666666">1</span>])]
<span style="color: #408080; font-style: italic">#Select the feature which best splits the dataset</span>
item_values <span style="color: #666666">=</span> [InfoGain(data,feature,target_attribute_name) <span style="color: #008000; font-weight: bold">for</span> feature <span style="color: #AA22FF; font-weight: bold">in</span> features] <span style="color: #408080; font-style: italic">#Return the information gain values for the features in the dataset</span>
best_feature_index <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argmax(item_values)
best_feature <span style="color: #666666">=</span> features[best_feature_index]
<span style="color: #408080; font-style: italic">#Create the tree structure. The root gets the name of the feature (best_feature) with the maximum information</span>
<span style="color: #408080; font-style: italic">#gain in the first run</span>
tree <span style="color: #666666">=</span> {best_feature:{}}
<span style="color: #408080; font-style: italic">#Remove the feature with the best inforamtion gain from the feature space</span>
features <span style="color: #666666">=</span> [i <span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> features <span style="color: #008000; font-weight: bold">if</span> i <span style="color: #666666">!=</span> best_feature]
<span style="color: #408080; font-style: italic">#Grow a branch under the root node for each possible value of the root node feature</span>
<span style="color: #008000; font-weight: bold">for</span> value <span style="color: #AA22FF; font-weight: bold">in</span> np<span style="color: #666666">.</span>unique(data[best_feature]):
value <span style="color: #666666">=</span> value
<span style="color: #408080; font-style: italic">#Split the dataset along the value of the feature with the largest information gain and therwith create sub_datasets</span>
sub_data <span style="color: #666666">=</span> data<span style="color: #666666">.</span>where(data[best_feature] <span style="color: #666666">==</span> value)<span style="color: #666666">.</span>dropna()
<span style="color: #408080; font-style: italic">#Call the ID3 algorithm for each of those sub_datasets with the new parameters --&gt; Here the recursion comes in!</span>
subtree <span style="color: #666666">=</span> ID3(sub_data,dataset,features,target_attribute_name,parent_node_class)
<span style="color: #408080; font-style: italic">#Add the sub tree, grown from the sub_dataset to the tree under the root node</span>
tree[best_feature][value] <span style="color: #666666">=</span> subtree
<span style="color: #008000; font-weight: bold">return</span>(tree)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lbfgs&#39;</span>)
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Logistic Regression: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Support vector machine</span>
svm <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">&#39;auto&#39;</span>, C<span style="color: #666666">=100</span>)
svm<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with SVM: {:.2f}&quot;</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Decision Trees</span>
deep_tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=</span><span style="color: #008000">None</span>)
deep_tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Decision Trees: {:.2f}&quot;</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic">#now scale the data</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy Logistic Regression with scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy SVM with scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Decision Trees</span>
deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Decision Trees and scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">Back to moons again </h2>
<h2 id="___sec14">Another example, the moons again </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -854,35 +806,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec18">Classification again: The zoo data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pprint</span> <span style="color: #008000; font-weight: bold">import</span> pprint
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<span style="color: #408080; font-style: italic">#Import the dataset </span>
dataset <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_csv(<span style="color: #BA2121">&#39;data/zoo.csv&#39;</span>)
<span style="color: #408080; font-style: italic">#We drop the animal names since this is not a good feature to split the data on</span>
<span style="color: #408080; font-style: italic">#dataset=dataset.drop(&#39;animal_name&#39;,axis=1)</span>
<span style="color: #408080; font-style: italic">#Split the data into a training and a testing set</span>
train_features <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[:<span style="color: #666666">80</span>,:<span style="color: #666666">-1</span>]
test_features <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[<span style="color: #666666">80</span>:,:<span style="color: #666666">-1</span>]
train_targets <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[:<span style="color: #666666">80</span>,<span style="color: #666666">-1</span>]
test_targets <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>iloc[<span style="color: #666666">80</span>:,<span style="color: #666666">-1</span>]
<span style="color: #408080; font-style: italic">#Train the model</span>
tree <span style="color: #666666">=</span> DecisionTreeClassifier(criterion <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;entropy&#39;</span>)<span style="color: #666666">.</span>fit(train_features,train_targets)
<span style="color: #408080; font-style: italic">#Predict the classes of new, unseen data</span>
prediction <span style="color: #666666">=</span> tree<span style="color: #666666">.</span>predict(test_features)
<span style="color: #408080; font-style: italic">#Check the accuracy</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The prediction accuracy is: &quot;</span>,tree<span style="color: #666666">.</span>score(test_features,test_targets)<span style="color: #666666">*100</span>,<span style="color: #BA2121">&quot;%&quot;</span>)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec19">Pros and cons of trees, pros </h2>
<h2 id="___sec18">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>
@@ -896,7 +820,7 @@ prediction <span style="color: #666666">=</span> tree<span style="color: #666666
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">Disadvantages </h2>
<h2 id="___sec19">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>
@@ -913,7 +837,7 @@ However, by aggregating many decision trees, using methods like bagging, random
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">Bagging </h2>
<h2 id="___sec20">Bagging </h2>
<p>
The <b>plain</b> decision trees suffer from high
@@ -956,7 +880,7 @@ predictor, averaged over all \( B \) trees.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">Simple example, head or tail </h2>
<h2 id="___sec21">Simple example, head or tail </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -976,7 +900,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec23">Random forests </h2>
<h2 id="___sec22">Random forests </h2>
<p>
Random forests provide an improvement over bagged trees by way of a
@@ -1018,7 +942,7 @@ setting.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">A simple scikit-learn example </h2>
<h2 id="___sec23">A simple scikit-learn example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -1036,7 +960,7 @@ accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_mode
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">Please, not the moons again! </h2>
<h2 id="___sec24">Please, not the moons again! </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -1094,7 +1018,7 @@ voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec26">Bagging examples </h2>
<h2 id="___sec25">Bagging examples </h2>
<p>
@@ -1155,7 +1079,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec27">Then random forests </h2>
<h2 id="___sec26">Then random forests </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -1177,7 +1101,7 @@ np<span style="color: #666666">.</span>sum(y_pred <span style="color: #666666">=
<p>
<!-- !split -->
<h2 id="___sec28">Boosting and more </h2>
<h2 id="___sec27">Boosting and more </h2>
More material to come here.
<p>
File diff suppressed because one or more lines are too long
Binary file not shown.
+49 -117
View File
@@ -155,15 +155,14 @@ plt.show()
===== Building a tree, regression =====
There are mainly two steps
o 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$.
o 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$.
o 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$.
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 predic- tive model. The goal is to find boxes $R_1,\dots,R_J$
resulting predictive model. The goal is to find boxes $R_1,\dots,R_J$
that minimize the MSE, given by
!bt
\[
@@ -171,8 +170,7 @@ that minimize the MSE, given by
\]
!et
where $\overline{y}_{R_j}$ is the mean response for the training observations
within the $j$th
box.
within box $j$.
!split
===== A top-down approach, recursive binary splitting =====
@@ -279,7 +277,7 @@ subtree corresponding to $\alpha$.
!split
===== A schematic procedure =====
===== Schematic Regression Procedure =====
!bblock Building a Regression Tree
o 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.
@@ -293,7 +291,7 @@ o Return the subtree from Step 2 that corresponds to the chosen value of $\alpha
!split
===== A classification tree =====
===== A Classification Tree =====
A classification tree is very similar to a regression tree, except
that it is used to predict a qualitative response rather than a
@@ -331,6 +329,7 @@ than is the classification error rate.
!split
===== Classification tree, how to split nodes =====
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.
@@ -365,100 +364,58 @@ s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}.
!split
===== Entropy and the ID3 algorithm =====
More text to come here.
More text and code to come here.
!split
===== Writing your own code for a classification tree =====
===== Cancer Data again now with Decision Trees =====
!bc pycod
def entropy(target_col):
"""
Calculate the entropy of a dataset.
The only parameter of this function is the target_col parameter which specifies the target column
"""
elements,counts = np.unique(target_col,return_counts = True)
entropy = np.sum([(-counts[i]/np.sum(counts))*np.log2(counts[i]/np.sum(counts)) for i in range(len(elements))])
return entropy
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
def InfoGain(data,split_attribute_name,target_name="class"):
"""
Calculate the information gain of a dataset. This function takes three parameters:
1. data = The dataset for whose feature the IG should be calculated
2. split_attribute_name = the name of the feature for which the information gain should be calculated
3. target_name = the name of the target feature. The default for this example is "class"
"""
#Calculate the entropy of the total dataset
total_entropy = entropy(data[target_name])
##Calculate the entropy of the dataset
#Calculate the values and the corresponding counts for the split attribute
vals,counts= np.unique(data[split_attribute_name],return_counts=True)
#Calculate the weighted entropy
Weighted_Entropy = np.sum([(counts[i]/np.sum(counts))*entropy(data.where(data[split_attribute_name]==vals[i]).dropna()[target_name]) for i in range(len(vals))])
#Calculate the information gain
Information_Gain = total_entropy - Weighted_Entropy
return Information_Gain
# Load the data
cancer = load_breast_cancer()
def ID3(data,originaldata,features,target_attribute_name="class",parent_node_class = None):
#Define the stopping criteria --> If one of this is satisfied, we want to return a leaf node#
#If all target_values have the same value, return this value
if len(np.unique(data[target_attribute_name])) <= 1:
return np.unique(data[target_attribute_name])[0]
#If the dataset is empty, return the mode target feature value in the original dataset
elif len(data)==0:
return np.unique(originaldata[target_attribute_name])[np.argmax(np.unique(originaldata[target_attribute_name],return_counts=True)[1])]
#If the feature space is empty, return the mode target feature value of the direct parent node --> Note that
#the direct parent node is that node which has called the current run of the ID3 algorithm and hence
#the mode target feature value is stored in the parent_node_class variable.
elif len(features) ==0:
return parent_node_class
#If none of the above holds true, grow the tree!
else:
#Set the default value for this node --> The mode target feature value of the current node
parent_node_class = np.unique(data[target_attribute_name])[np.argmax(np.unique(data[target_attribute_name],return_counts=True)[1])]
#Select the feature which best splits the dataset
item_values = [InfoGain(data,feature,target_attribute_name) for feature in features] #Return the information gain values for the features in the dataset
best_feature_index = np.argmax(item_values)
best_feature = features[best_feature_index]
#Create the tree structure. The root gets the name of the feature (best_feature) with the maximum information
#gain in the first run
tree = {best_feature:{}}
#Remove the feature with the best inforamtion gain from the feature space
features = [i for i in features if i != best_feature]
#Grow a branch under the root node for each possible value of the root node feature
for value in np.unique(data[best_feature]):
value = value
#Split the dataset along the value of the feature with the largest information gain and therwith create sub_datasets
sub_data = data.where(data[best_feature] == value).dropna()
#Call the ID3 algorithm for each of those sub_datasets with the new parameters --> Here the recursion comes in!
subtree = ID3(sub_data,dataset,features,target_attribute_name,parent_node_class)
#Add the sub tree, grown from the sub_dataset to the tree under the root node
tree[best_feature][value] = subtree
return(tree)
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
print(X_train.shape)
print(X_test.shape)
# Logistic Regression
logreg = LogisticRegression(solver='lbfgs')
logreg.fit(X_train, y_train)
print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
# Support vector machine
svm = SVC(gamma='auto', C=100)
svm.fit(X_train, y_train)
print("Test set accuracy with SVM: {:.2f}".format(svm.score(X_test,y_test)))
# Decision Trees
deep_tree_clf = DecisionTreeClassifier(max_depth=None)
deep_tree_clf.fit(X_train, y_train)
print("Test set accuracy with Decision Trees: {:.2f}".format(deep_tree_clf.score(X_test,y_test)))
#now scale the data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Logistic Regression
logreg.fit(X_train_scaled, y_train)
print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
# Support Vector Machine
svm.fit(X_train_scaled, y_train)
print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
# Decision Trees
deep_tree_clf.fit(X_train_scaled, y_train)
print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test)))
!ec
!split
===== Back to moons again =====
===== Another example, the moons again =====
!bc pycod
from __future__ import division, print_function, unicode_literals
@@ -645,31 +602,6 @@ plt.show()
!ec
!split
===== Classification again: The zoo data =====
!bc pycod
import pandas as pd
import numpy as np
from pprint import pprint
from sklearn.tree import DecisionTreeClassifier
#Import the dataset
dataset = pd.read_csv('data/zoo.csv')
#We drop the animal names since this is not a good feature to split the data on
#dataset=dataset.drop('animal_name',axis=1)
#Split the data into a training and a testing set
train_features = dataset.iloc[:80,:-1]
test_features = dataset.iloc[80:,:-1]
train_targets = dataset.iloc[:80,-1]
test_targets = dataset.iloc[80:,-1]
#Train the model
tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)
#Predict the classes of new, unseen data
prediction = tree.predict(test_features)
#Check the accuracy
print("The prediction accuracy is: ",tree.score(test_features,test_targets)*100,"%")
!ec
!split
===== Pros and cons of trees, pros =====
+41
View File
@@ -0,0 +1,41 @@
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
# Load the data
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
print(X_train.shape)
print(X_test.shape)
# Logistic Regression
logreg = LogisticRegression(solver='lbfgs')
logreg.fit(X_train, y_train)
print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
# Support vector machine
svm = SVC(gamma='auto', C=100)
svm.fit(X_train, y_train)
print("Test set accuracy with SVM: {:.2f}".format(svm.score(X_test,y_test)))
# Decision Trees
deep_tree_clf = DecisionTreeClassifier(max_depth=None)
deep_tree_clf.fit(X_train, y_train)
print("Test set accuracy with Decision Trees: {:.2f}".format(deep_tree_clf.score(X_test,y_test)))
#now scale the data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Logistic Regression
logreg.fit(X_train_scaled, y_train)
print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
# Support Vector Machine
svm.fit(X_train_scaled, y_train)
print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
# Decision Trees
deep_tree_clf.fit(X_train_scaled, y_train)
print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test)))