typos
This commit is contained in:
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -226,7 +220,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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -231,7 +225,7 @@ given some assumptions, make predictions about the target feature value
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs002.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -185,9 +179,6 @@ MathJax.Hub.Config({
|
||||
|
||||
<h2 id="___sec1" class="anchor">A typical Decision Tree with its pertinent Jargon, Classification Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure here we present a decision tree obtained from a classification problem
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -206,7 +197,7 @@ In the figure here we present a decision tree obtained from a classification pro
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs003.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,10 +177,20 @@ MathJax.Hub.Config({
|
||||
<a name="part0003"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec2" class="anchor">A typical Decision Tree with its pertinent Jargon, Regeression Problem </h2>
|
||||
<h2 id="___sec2" class="anchor">General Features </h2>
|
||||
|
||||
<p>
|
||||
In the figure we present a decision tree obtained from a simple regression problem
|
||||
The overarching approach to decision trees is a top-down approach.
|
||||
|
||||
<ul>
|
||||
<li> A leaf provides the classification of a given instance.</li>
|
||||
<li> A node specifies a test of some attribute of the instance.</li>
|
||||
<li> A branch corresponds to a possible values of an attribute.</li>
|
||||
<li> An instance is classified by starting at the root node of the tree, testing the attribute specified by this node, then moving down the tree branch corresponding to the value of the attribute in the given example.</li>
|
||||
</ul>
|
||||
|
||||
This process is then repeated for the subtree rooted at the new
|
||||
node.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -207,7 +211,7 @@ In the figure we present a decision tree obtained from a simple regression prob
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs004.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,20 +177,20 @@ MathJax.Hub.Config({
|
||||
<a name="part0004"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec3" class="anchor">General Features </h2>
|
||||
<h2 id="___sec3" class="anchor">How do we set it up? </h2>
|
||||
|
||||
<p>
|
||||
The overarching approach to decision trees is a top-down approach.
|
||||
In simplified terms, the process of training a decision tree and
|
||||
predicting the target features of query instances is as follows:
|
||||
|
||||
<ul>
|
||||
<li> A leaf provides the classification of a given instance.</li>
|
||||
<li> A node specifies a test of some attribute of the instance.</li>
|
||||
<li> A branch corresponds to a possible values of an attribute.</li>
|
||||
<li> An instance is classified by starting at the root node of the tree, testing the attribute specified by this node, then moving down the tree branch corresponding to the value of the attribute in the given example.</li>
|
||||
</ul>
|
||||
<ol>
|
||||
<li> Present a dataset containing of a number of training instances characterized by a number of descriptive features and a target feature</li>
|
||||
<li> Train the decision tree model by continuously splitting the target feature along the values of the descriptive features using a measure of information gain during the training process</li>
|
||||
<li> Grow the tree until we accomplish a stopping criteria create leaf nodes which represent the <em>predictions</em> we want to make for new query instances</li>
|
||||
<li> Show query instances to the tree and run down the tree until we arrive at leaf nodes</li>
|
||||
</ol>
|
||||
|
||||
This process is then repeated for the subtree rooted at the new
|
||||
node.
|
||||
Then we are essentially done!
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -218,7 +212,7 @@ node.
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs005.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,21 +177,99 @@ MathJax.Hub.Config({
|
||||
<a name="part0005"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4" class="anchor">How do we set it up? </h2>
|
||||
|
||||
<h2 id="___sec4" class="anchor">Decision trees and Regression </h2>
|
||||
<p>
|
||||
In simplified terms, the process of training a decision tree and
|
||||
predicting the target features of query instances is as follows:
|
||||
|
||||
<ol>
|
||||
<li> Present a dataset containing of a number of training instances characterized by a number of descriptive features and a target feature</li>
|
||||
<li> Train the decision tree model by continuously splitting the target feature along the values of the descriptive features using a measure of information gain during the training process</li>
|
||||
<li> Grow the tree until we accomplish a stopping criteria create leaf nodes which represent the <em>predictions</em> we want to make for new query instances</li>
|
||||
<li> Show query instances to the tree and run down the tree until we arrive at leaf nodes</li>
|
||||
</ol>
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
|
||||
|
||||
Then we are essentially done!
|
||||
steps<span style="color: #666666">=250</span>
|
||||
|
||||
distance<span style="color: #666666">=0</span>
|
||||
x<span style="color: #666666">=0</span>
|
||||
distance_list<span style="color: #666666">=</span>[]
|
||||
steps_list<span style="color: #666666">=</span>[]
|
||||
<span style="color: #008000; font-weight: bold">while</span> x<span style="color: #666666"><</span>steps:
|
||||
distance<span style="color: #666666">+=</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randint(<span style="color: #666666">-1</span>,<span style="color: #666666">2</span>)
|
||||
distance_list<span style="color: #666666">.</span>append(distance)
|
||||
x<span style="color: #666666">+=1</span>
|
||||
steps_list<span style="color: #666666">.</span>append(x)
|
||||
plt<span style="color: #666666">.</span>plot(steps_list,distance_list, color<span style="color: #666666">=</span><span style="color: #BA2121">'green'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Random Walk Data"</span>)
|
||||
|
||||
steps_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(steps_list)
|
||||
distance_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(distance_list)
|
||||
|
||||
X<span style="color: #666666">=</span>steps_list[:,np<span style="color: #666666">.</span>newaxis]
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Polynomial fits</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 2</span>
|
||||
poly_features<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=2</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly<span style="color: #666666">=</span>poly_features<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
lin_reg<span style="color: #666666">=</span>LinearRegression()
|
||||
poly_fit<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly,distance_list)
|
||||
b<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>coef_
|
||||
c<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>intercept_
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"2nd degree coefficients:"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"zero power: "</span>,c)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"first power: "</span>, b[<span style="color: #666666">0</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"second power: "</span>,b[<span style="color: #666666">1</span>])
|
||||
|
||||
z <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0</span>, steps, <span style="color: #666666">.01</span>)
|
||||
z_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>z<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>z<span style="color: #666666">+</span>c
|
||||
|
||||
fit_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>X<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>X<span style="color: #666666">+</span>c
|
||||
plt<span style="color: #666666">.</span>plot(z, z_mod, color<span style="color: #666666">=</span><span style="color: #BA2121">'r'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"2nd Degree Fit"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Polynomial Regression"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Steps"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Distance"</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 10</span>
|
||||
poly_features10<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=10</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly10<span style="color: #666666">=</span>poly_features10<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
poly_fit10<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly10,distance_list)
|
||||
|
||||
y_plot<span style="color: #666666">=</span>poly_fit10<span style="color: #666666">.</span>predict(X_poly10)
|
||||
plt<span style="color: #666666">.</span>plot(X, y_plot, color<span style="color: #666666">=</span><span style="color: #BA2121">'black'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"10th Degree Fit"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Decision Tree Regression</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
regr_1<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=2</span>)
|
||||
regr_2<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=5</span>)
|
||||
regr_3<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=7</span>)
|
||||
regr_1<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_2<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_3<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
|
||||
X_test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0.0</span>, steps, <span style="color: #666666">0.01</span>)[:, np<span style="color: #666666">.</span>newaxis]
|
||||
y_1 <span style="color: #666666">=</span> regr_1<span style="color: #666666">.</span>predict(X_test)
|
||||
y_2 <span style="color: #666666">=</span> regr_2<span style="color: #666666">.</span>predict(X_test)
|
||||
y_3<span style="color: #666666">=</span>regr_3<span style="color: #666666">.</span>predict(X_test)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>scatter(X, distance_list, s<span style="color: #666666">=2.5</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">"black"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"data"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_1, color<span style="color: #666666">=</span><span style="color: #BA2121">"red"</span>,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=2"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_2, color<span style="color: #666666">=</span><span style="color: #BA2121">"green"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=5"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_3, color<span style="color: #666666">=</span><span style="color: #BA2121">"m"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=7"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Data"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Darget"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Decision Tree Regression"</span>)
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -219,7 +291,7 @@ Then we are essentially done!
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs006.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,99 +177,31 @@ MathJax.Hub.Config({
|
||||
<a name="part0006"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec5" class="anchor">Decision trees and Regression </h2>
|
||||
<h2 id="___sec5" class="anchor">Building a tree, regression </h2>
|
||||
|
||||
<p>
|
||||
There are mainly two steps
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
|
||||
<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>
|
||||
|
||||
steps<span style="color: #666666">=250</span>
|
||||
How do we construct the regions \( R_1,\dots,R_J \)? In theory, the
|
||||
regions could have any shape. However, we choose to divide the
|
||||
predictor space into high-dimensional rectangles, or boxes, for
|
||||
simplicity and for ease of interpretation of the resulting predictive
|
||||
model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the
|
||||
MSE, given by
|
||||
|
||||
distance<span style="color: #666666">=0</span>
|
||||
x<span style="color: #666666">=0</span>
|
||||
distance_list<span style="color: #666666">=</span>[]
|
||||
steps_list<span style="color: #666666">=</span>[]
|
||||
<span style="color: #008000; font-weight: bold">while</span> x<span style="color: #666666"><</span>steps:
|
||||
distance<span style="color: #666666">+=</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randint(<span style="color: #666666">-1</span>,<span style="color: #666666">2</span>)
|
||||
distance_list<span style="color: #666666">.</span>append(distance)
|
||||
x<span style="color: #666666">+=1</span>
|
||||
steps_list<span style="color: #666666">.</span>append(x)
|
||||
plt<span style="color: #666666">.</span>plot(steps_list,distance_list, color<span style="color: #666666">=</span><span style="color: #BA2121">'green'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Random Walk Data"</span>)
|
||||
$$
|
||||
\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2,
|
||||
$$
|
||||
|
||||
steps_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(steps_list)
|
||||
distance_list<span style="color: #666666">=</span>np<span style="color: #666666">.</span>asarray(distance_list)
|
||||
<p>
|
||||
where \( \overline{y}_{R_j} \) is the mean response for the training observations
|
||||
within box \( j \).
|
||||
|
||||
X<span style="color: #666666">=</span>steps_list[:,np<span style="color: #666666">.</span>newaxis]
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Polynomial fits</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 2</span>
|
||||
poly_features<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=2</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly<span style="color: #666666">=</span>poly_features<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
lin_reg<span style="color: #666666">=</span>LinearRegression()
|
||||
poly_fit<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly,distance_list)
|
||||
b<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>coef_
|
||||
c<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>intercept_
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"2nd degree coefficients:"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"zero power: "</span>,c)
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"first power: "</span>, b[<span style="color: #666666">0</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"second power: "</span>,b[<span style="color: #666666">1</span>])
|
||||
|
||||
z <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0</span>, steps, <span style="color: #666666">.01</span>)
|
||||
z_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>z<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>z<span style="color: #666666">+</span>c
|
||||
|
||||
fit_mod<span style="color: #666666">=</span>b[<span style="color: #666666">1</span>]<span style="color: #666666">*</span>X<span style="color: #666666">**2+</span>b[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>X<span style="color: #666666">+</span>c
|
||||
plt<span style="color: #666666">.</span>plot(z, z_mod, color<span style="color: #666666">=</span><span style="color: #BA2121">'r'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"2nd Degree Fit"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Polynomial Regression"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Steps"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Distance"</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Degree 10</span>
|
||||
poly_features10<span style="color: #666666">=</span>PolynomialFeatures(degree<span style="color: #666666">=10</span>, include_bias<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
X_poly10<span style="color: #666666">=</span>poly_features10<span style="color: #666666">.</span>fit_transform(X)
|
||||
|
||||
poly_fit10<span style="color: #666666">=</span>lin_reg<span style="color: #666666">.</span>fit(X_poly10,distance_list)
|
||||
|
||||
y_plot<span style="color: #666666">=</span>poly_fit10<span style="color: #666666">.</span>predict(X_poly10)
|
||||
plt<span style="color: #666666">.</span>plot(X, y_plot, color<span style="color: #666666">=</span><span style="color: #BA2121">'black'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"10th Degree Fit"</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic">#Decision Tree Regression</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
regr_1<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=2</span>)
|
||||
regr_2<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=5</span>)
|
||||
regr_3<span style="color: #666666">=</span>DecisionTreeRegressor(max_depth<span style="color: #666666">=7</span>)
|
||||
regr_1<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_2<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
regr_3<span style="color: #666666">.</span>fit(X, distance_list)
|
||||
|
||||
X_test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">0.0</span>, steps, <span style="color: #666666">0.01</span>)[:, np<span style="color: #666666">.</span>newaxis]
|
||||
y_1 <span style="color: #666666">=</span> regr_1<span style="color: #666666">.</span>predict(X_test)
|
||||
y_2 <span style="color: #666666">=</span> regr_2<span style="color: #666666">.</span>predict(X_test)
|
||||
y_3<span style="color: #666666">=</span>regr_3<span style="color: #666666">.</span>predict(X_test)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>scatter(X, distance_list, s<span style="color: #666666">=2.5</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">"black"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"data"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_1, color<span style="color: #666666">=</span><span style="color: #BA2121">"red"</span>,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=2"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_2, color<span style="color: #666666">=</span><span style="color: #BA2121">"green"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=5"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X_test, y_3, color<span style="color: #666666">=</span><span style="color: #BA2121">"m"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"max_depth=7"</span>, linewidth<span style="color: #666666">=2</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Data"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Darget"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Decision Tree Regression"</span>)
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -298,7 +224,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs007.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,30 +177,21 @@ MathJax.Hub.Config({
|
||||
<a name="part0007"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec6" class="anchor">Building a tree, regression </h2>
|
||||
<h2 id="___sec6" class="anchor">A top-down approach, recursive binary splitting </h2>
|
||||
|
||||
<p>
|
||||
There are mainly two steps
|
||||
|
||||
<ol>
|
||||
<li> We split the predictor space (the set of possible values \( x_1,x_2,\dots, x_p \)) into \( J \) distinct and non-non-overlapping regions, \( R_1,R_2,\dots,R_J \).</li>
|
||||
<li> For every observation that falls into the region \( R_j \) , we make the same prediction, which is simply the mean of the response values for the training observations in \( R_j \).</li>
|
||||
</ol>
|
||||
|
||||
How do we construct the regions \( R_1,\dots,R_J \)? In theory, the
|
||||
regions could have any shape. However, we choose to divide the
|
||||
predictor space into high-dimensional rectangles, or boxes, for
|
||||
simplicity and for ease of interpretation of the resulting predictive
|
||||
model. The goal is to find boxes \( R_1,\dots,R_J \) that minimize the
|
||||
MSE, given by
|
||||
|
||||
$$
|
||||
\sum_{j=1}^J\sum_{i\in R_j}(y_i-\overline{y}_{R_j})^2,
|
||||
$$
|
||||
Unfortunately, it is computationally infeasible to consider every
|
||||
possible partition of the feature space into \( J \) boxes. The common
|
||||
strategy is to take a top-down approach
|
||||
|
||||
<p>
|
||||
where \( \overline{y}_{R_j} \) is the mean response for the training observations
|
||||
within box \( j \).
|
||||
The approach is top-down because it begins at the top of the tree (all
|
||||
observations belong to a single region) and then successively splits
|
||||
the predictor space; each split is indicated via two new branches
|
||||
further down on the tree. It is greedy because at each step of the
|
||||
tree-building process, the best split is made at that particular step,
|
||||
rather than looking ahead and picking a split that will lead to a
|
||||
better tree in some future step.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -231,7 +216,7 @@ within box \( j \).
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs008.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,21 +177,53 @@ MathJax.Hub.Config({
|
||||
<a name="part0008"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec7" class="anchor">A top-down approach, recursive binary splitting </h2>
|
||||
<h2 id="___sec7" class="anchor">Making a tree </h2>
|
||||
|
||||
<p>
|
||||
Unfortunately, it is computationally infeasible to consider every
|
||||
possible partition of the feature space into \( J \) boxes. The common
|
||||
strategy is to take a top-down approach
|
||||
In order to implement the recursive binary splitting we start by selecting
|
||||
the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \)
|
||||
$$
|
||||
\left\{X\vert x_j < s\right\},
|
||||
$$
|
||||
|
||||
and
|
||||
$$
|
||||
\left\{X\vert x_j \geq s\right\},
|
||||
$$
|
||||
|
||||
so that we obtain the lowest MSE, that is
|
||||
$$
|
||||
\sum_{i:x_i\in R_j}(y_i-\overline{y}_{R_1})^2+\sum_{i:x_i\in R_2}(y_i-\overline{y}_{R_2})^2,
|
||||
$$
|
||||
|
||||
<p>
|
||||
The approach is top-down because it begins at the top of the tree (all
|
||||
observations belong to a single region) and then successively splits
|
||||
the predictor space; each split is indicated via two new branches
|
||||
further down on the tree. It is greedy because at each step of the
|
||||
tree-building process, the best split is made at that particular step,
|
||||
rather than looking ahead and picking a split that will lead to a
|
||||
better tree in some future step.
|
||||
which we want to minimize by considering all predictors
|
||||
\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for
|
||||
each predictor. These values could be determined by randomly assigned
|
||||
numbers or by starting at the midpoint and then proceed till we find
|
||||
an optimal value.
|
||||
|
||||
<p>
|
||||
For any \( j \) and \( s \), we define the pair of half-planes where
|
||||
\( \overline{y}_{R_1} \) is the mean response for the training
|
||||
observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean
|
||||
response for the training observations in \( R_2(j,s) \).
|
||||
|
||||
<p>
|
||||
Finding the values of \( j \) and \( s \) that minimize the above equation can be
|
||||
done quite quickly, especially when the number of features \( p \) is not
|
||||
too large.
|
||||
|
||||
<p>
|
||||
Next, we repeat the process, looking
|
||||
for the best predictor and best cutpoint in order to split the data
|
||||
further so as to minimize the MSE within each of the resulting
|
||||
regions. However, this time, instead of splitting the entire predictor
|
||||
space, we split one of the two previously identified regions. We now
|
||||
have three regions. Again, we look to split one of these three regions
|
||||
further, so as to minimize the MSE. The process continues until a
|
||||
stopping criterion is reached; for instance, we may continue until no
|
||||
region contains more than five observations.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -223,7 +249,7 @@ better tree in some future step.
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs009.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -181,55 +175,23 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0009"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec8" class="anchor">Making a tree </h2>
|
||||
<h2 id="___sec8" class="anchor">Pruning the tree </h2>
|
||||
|
||||
<p>
|
||||
In order to implement the recursive binary splitting we start by selecting
|
||||
the predictor \( x_j \) and a cutpoint \( s \) that splits the predictor space into two regions \( R_1 \) and \( R_2 \)
|
||||
$$
|
||||
\left\{X\vert x_j < s\right\},
|
||||
$$
|
||||
|
||||
and
|
||||
$$
|
||||
\left\{X\vert x_j \geq s\right\},
|
||||
$$
|
||||
|
||||
so that we obtain the lowest MSE, that is
|
||||
$$
|
||||
\sum_{i:x_i\in R_j}(y_i-\overline{y}_{R_1})^2+\sum_{i:x_i\in R_2}(y_i-\overline{y}_{R_2})^2,
|
||||
$$
|
||||
The above procedure is rather straightforward, but leads often to
|
||||
overfitting and unnecessarily large and complicated trees. The basic
|
||||
idea is to grow a large tree \( T_0 \) and then prune it back in order to
|
||||
obtain a subtree. A smaller tree with fewer splits (fewer regions) can
|
||||
lead to smaller variance and better interpretation at the cost of a
|
||||
little more bias.
|
||||
|
||||
<p>
|
||||
which we want to minimize by considering all predictors
|
||||
\( x_1,x_2,\dots,x_p \). We consider also all possible values of \( s \) for
|
||||
each predictor. These values could be determined by randomly assigned
|
||||
numbers or by starting at the midpoint and then proceed till we find
|
||||
an optimal value.
|
||||
|
||||
<p>
|
||||
For any \( j \) and \( s \), we define the pair of half-planes where
|
||||
\( \overline{y}_{R_1} \) is the mean response for the training
|
||||
observations in \( R_1(j,s) \), and \( \overline{y}_{R_2} \) is the mean
|
||||
response for the training observations in \( R_2(j,s) \).
|
||||
|
||||
<p>
|
||||
Finding the values of \( j \) and \( s \) that minimize the above equation can be
|
||||
done quite quickly, especially when the number of features \( p \) is not
|
||||
too large.
|
||||
|
||||
<p>
|
||||
Next, we repeat the process, looking
|
||||
for the best predictor and best cutpoint in order to split the data
|
||||
further so as to minimize the MSE within each of the resulting
|
||||
regions. However, this time, instead of splitting the entire predictor
|
||||
space, we split one of the two previously identified regions. We now
|
||||
have three regions. Again, we look to split one of these three regions
|
||||
further, so as to minimize the MSE. The process continues until a
|
||||
stopping criterion is reached; for instance, we may continue until no
|
||||
region contains more than five observations.
|
||||
The so-called Cost complexity pruning algorithm gives us a
|
||||
way to do just this. Rather than considering every possible subtree,
|
||||
we consider a sequence of trees indexed by a nonnegative tuning
|
||||
parameter \( \alpha \).
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -256,7 +218,7 @@ region contains more than five observations.
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs010.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -181,23 +175,35 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0010"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec9" class="anchor">Pruning the tree </h2>
|
||||
<h2 id="___sec9" class="anchor">Cost complexity pruning </h2>
|
||||
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
||||
$$
|
||||
\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T},
|
||||
$$
|
||||
|
||||
is as small as possible. Here \( \overline{T} \) is
|
||||
the number of terminal nodes of the tree \( T \) , \( R_m \) is the
|
||||
rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node.
|
||||
|
||||
<p>
|
||||
The above procedure is rather straightforward, but leads often to
|
||||
overfitting and unnecessarily large and complicated trees. The basic
|
||||
idea is to grow a large tree \( T_0 \) and then prune it back in order to
|
||||
obtain a subtree. A smaller tree with fewer splits (fewer regions) can
|
||||
lead to smaller variance and better interpretation at the cost of a
|
||||
little more bias.
|
||||
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s
|
||||
com- plexity and its fit to the training data. When \( \alpha = 0 \), then the
|
||||
subtree \( T \) will simply equal \( T_0 \),
|
||||
because then the above equation just measures the
|
||||
training error.
|
||||
However, as \( \alpha \) increases, there is a price to pay for
|
||||
having a tree with many terminal nodes. The above equation will
|
||||
tend to be minimized for a smaller subtree.
|
||||
|
||||
<p>
|
||||
The so-called Cost complexity pruning algorithm gives us a
|
||||
way to do just this. Rather than considering every possible subtree,
|
||||
we consider a sequence of trees indexed by a nonnegative tuning
|
||||
parameter \( \alpha \).
|
||||
It turns out that as we increase \( \alpha \) from zero
|
||||
branches get pruned from the tree in a nested and predictable fashion,
|
||||
so obtaining the whole sequence of subtrees as a function of \( \alpha \) is
|
||||
easy. We can select a value of \( \alpha \) using a validation set or using
|
||||
cross-validation. We then return to the full data set and obtain the
|
||||
subtree corresponding to \( \alpha \).
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -225,7 +231,7 @@ parameter \( \alpha \).
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs011.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,33 +177,29 @@ MathJax.Hub.Config({
|
||||
<a name="part0011"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec10" class="anchor">Cost complexity pruning </h2>
|
||||
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
||||
$$
|
||||
\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T},
|
||||
$$
|
||||
|
||||
is as small as possible. Here \( \overline{T} \) is
|
||||
the number of terminal nodes of the tree \( T \) , \( R_m \) is the
|
||||
rectangle (i.e. the subset of predictor space) corresponding to the \( m \)-th terminal node.
|
||||
<h2 id="___sec10" class="anchor">Schematic Regression Procedure </h2>
|
||||
|
||||
<p>
|
||||
The tuning parameter \( \alpha \) controls a trade-off between the subtree’s
|
||||
com- plexity and its fit to the training data. When \( \alpha = 0 \), then the
|
||||
subtree \( T \) will simply equal \( T_0 \),
|
||||
because then the above equation just measures the
|
||||
training error.
|
||||
However, as \( \alpha \) increases, there is a price to pay for
|
||||
having a tree with many terminal nodes. The above equation will
|
||||
tend to be minimized for a smaller subtree.
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
|
||||
<ol>
|
||||
<li> Use recursive binary splitting to grow a large tree on the training data, stopping only when each terminal node has fewer than some minimum number of observations.</li>
|
||||
<li> Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).</li>
|
||||
<li> Use for example \( K \)-fold cross-validation to choose \( \alpha \). Divide the training observations into \( K \) folds. For each \( k=1,2,\dots,K \) we:</li>
|
||||
|
||||
<ul>
|
||||
<li> repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.</li>
|
||||
<li> Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).</li>
|
||||
<li> Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.</li>
|
||||
</ul>
|
||||
|
||||
<li> Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
It turns out that as we increase \( \alpha \) from zero
|
||||
branches get pruned from the tree in a nested and predictable fashion,
|
||||
so obtaining the whole sequence of subtrees as a function of \( \alpha \) is
|
||||
easy. We can select a value of \( \alpha \) using a validation set or using
|
||||
cross-validation. We then return to the full data set and obtain the
|
||||
subtree corresponding to \( \alpha \).
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -237,7 +227,7 @@ subtree corresponding to \( \alpha \).
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs012.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,29 +177,21 @@ MathJax.Hub.Config({
|
||||
<a name="part0012"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec11" class="anchor">Schematic Regression Procedure </h2>
|
||||
<h2 id="___sec11" class="anchor">A Classification Tree </h2>
|
||||
|
||||
<p>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
|
||||
<ol>
|
||||
<li> Use recursive binary splitting to grow a large tree on the training data, stopping only when each terminal node has fewer than some minimum number of observations.</li>
|
||||
<li> Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \( \alpha \).</li>
|
||||
<li> Use for example \( K \)-fold cross-validation to choose \( \alpha \). Divide the training observations into \( K \) folds. For each \( k=1,2,\dots,K \) we:</li>
|
||||
|
||||
<ul>
|
||||
<li> repeat steps 1 and 2 on all but the \( k \)-th fold of the training data.</li>
|
||||
<li> Then we valuate the mean squared prediction error on the data in the left-out \( k \)-th fold, as a function of \( \alpha \).</li>
|
||||
<li> Finally we average the results for each value of \( \alpha \), and pick \( \alpha \) to minimize the average error.</li>
|
||||
</ul>
|
||||
|
||||
<li> Return the subtree from Step 2 that corresponds to the chosen value of \( \alpha \).</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
A classification tree is very similar to a regression tree, except
|
||||
that it is used to predict a qualitative response rather than a
|
||||
quantitative one. Recall that for a regression tree, the predicted
|
||||
response for an observation is given by the mean response of the
|
||||
training observations that belong to the same terminal node. In
|
||||
contrast, for a classification tree, we predict that each observation
|
||||
belongs to the most commonly occurring class of training observations
|
||||
in the region to which it belongs. In interpreting the results of a
|
||||
classification tree, we are often interested not only in the class
|
||||
prediction corresponding to a particular terminal node region, but
|
||||
also in the class proportions among the training observations that
|
||||
fall into that region.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -233,7 +219,7 @@ MathJax.Hub.Config({
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs013.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,21 +177,26 @@ MathJax.Hub.Config({
|
||||
<a name="part0013"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec12" class="anchor">A Classification Tree </h2>
|
||||
<h2 id="___sec12" class="anchor">Growing a classification tree </h2>
|
||||
|
||||
<p>
|
||||
A classification tree is very similar to a regression tree, except
|
||||
that it is used to predict a qualitative response rather than a
|
||||
quantitative one. Recall that for a regression tree, the predicted
|
||||
response for an observation is given by the mean response of the
|
||||
training observations that belong to the same terminal node. In
|
||||
contrast, for a classification tree, we predict that each observation
|
||||
belongs to the most commonly occurring class of training observations
|
||||
in the region to which it belongs. In interpreting the results of a
|
||||
classification tree, we are often interested not only in the class
|
||||
prediction corresponding to a particular terminal node region, but
|
||||
also in the class proportions among the training observations that
|
||||
fall into that region.
|
||||
The task of growing a
|
||||
classification tree is quite similar to the task of growing a
|
||||
regression tree. Just as in the regression setting, we use recursive
|
||||
binary splitting to grow a classification tree. However, in the
|
||||
classification setting, the MSE cannot be used as a criterion for making
|
||||
the binary splits. A natural alternative to MSE is the <b>classification
|
||||
error rate</b>. Since we plan to assign an observation in a given region
|
||||
to the most commonly occurring error rate class of training
|
||||
observations in that region, the classification error rate is simply
|
||||
the fraction of the training observations in that region that do not
|
||||
belong to the most common class.
|
||||
|
||||
<p>
|
||||
When building a classification tree, either the Gini index or the
|
||||
entropy are typically used to evaluate the quality of a particular
|
||||
split, since these two approaches are more sensitive to node purity
|
||||
than is the classification error rate.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -225,7 +224,7 @@ fall into that region.
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs014.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,26 +177,52 @@ MathJax.Hub.Config({
|
||||
<a name="part0014"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec13" class="anchor">Growing a classification tree </h2>
|
||||
<h2 id="___sec13" class="anchor">Classification tree, how to split nodes </h2>
|
||||
|
||||
<p>
|
||||
The task of growing a
|
||||
classification tree is quite similar to the task of growing a
|
||||
regression tree. Just as in the regression setting, we use recursive
|
||||
binary splitting to grow a classification tree. However, in the
|
||||
classification setting, the MSE cannot be used as a criterion for making
|
||||
the binary splits. A natural alternative to MSE is the <b>classification
|
||||
error rate</b>. Since we plan to assign an observation in a given region
|
||||
to the most commonly occurring error rate class of training
|
||||
observations in that region, the classification error rate is simply
|
||||
the fraction of the training observations in that region that do not
|
||||
belong to the most common class.
|
||||
If our targets are the outcome of a classification process that takes
|
||||
for example \( k=1,2,\dots,K \) values, the only thing we need to think of
|
||||
is to set up the splitting criteria for each node.
|
||||
|
||||
<p>
|
||||
When building a classification tree, either the Gini index or the
|
||||
entropy are typically used to evaluate the quality of a particular
|
||||
split, since these two approaches are more sensitive to node purity
|
||||
than is the classification error rate.
|
||||
We define a PDF \( p_{mk} \) that represents the number of observations of
|
||||
a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent
|
||||
this likelihood function in terms of the proportion \( I(y_i=k) \) of
|
||||
observations of this class in the region \( R_m \) as
|
||||
|
||||
$$
|
||||
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k).
|
||||
$$
|
||||
|
||||
<p>
|
||||
We let \( p_{mk} \) represent the majority class of observations in region
|
||||
\( m \). The three most common ways of splitting a node are given by
|
||||
|
||||
<ul>
|
||||
<li> Misclassification error</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}.
|
||||
$$
|
||||
|
||||
|
||||
<ul>
|
||||
<li> Gini index \( g \)</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
g = \sum_{k=1}^K p_{mk}(1-p_{mk}).
|
||||
$$
|
||||
|
||||
|
||||
<ul>
|
||||
<li> Information entropy or just entropy \( s \)</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -230,7 +250,7 @@ than is the classification error rate.
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs015.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,53 +177,44 @@ MathJax.Hub.Config({
|
||||
<a name="part0015"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec14" class="anchor">Classification tree, how to split nodes </h2>
|
||||
|
||||
<h2 id="___sec14" class="anchor">Visualizing the Tree, Classification </h2>
|
||||
<p>
|
||||
If our targets are the outcome of a classification process that takes
|
||||
for example \( k=1,2,\dots,K \) values, the only thing we need to think of
|
||||
is to set up the splitting criteria for each node.
|
||||
|
||||
<p>
|
||||
We define a PDF \( p_{mk} \) that represents the number of observations of
|
||||
a class \( k \) in a region \( R_m \) with \( N_m \) observations. We represent
|
||||
this likelihood function in terms of the proportion \( I(y_i=k) \) of
|
||||
observations of this class in the region \( R_m \) as
|
||||
<!-- 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">os</span>
|
||||
<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.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<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.metrics</span> <span style="color: #008000; font-weight: bold">import</span> confusion_matrix
|
||||
<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> export_graphviz
|
||||
|
||||
$$
|
||||
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i=k).
|
||||
$$
|
||||
|
||||
<p>
|
||||
We let \( p_{mk} \) represent the majority class of observations in region
|
||||
\( m \). The three most common ways of splitting a node are given by
|
||||
|
||||
<ul>
|
||||
<li> Misclassification error</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
p_{mk} = \frac{1}{N_m}\sum_{x_i\in R_m}I(y_i\ne k) = 1-p_{mk}.
|
||||
$$
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<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>
|
||||
|
||||
|
||||
<ul>
|
||||
<li> Gini index \( g \)</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
g = \sum_{k=1}^K p_{mk}(1-p_{mk}).
|
||||
$$
|
||||
|
||||
|
||||
<ul>
|
||||
<li> Information entropy or just entropy \( s \)</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
s = -\sum_{k=1}^K p_{mk}\log{p_{mk}}.
|
||||
$$
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
X <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X)
|
||||
y <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>Categorical<span style="color: #666666">.</span>from_codes(cancer<span style="color: #666666">.</span>target, cancer<span style="color: #666666">.</span>target_names)
|
||||
y <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>get_dummies(y)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(y)
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, random_state<span style="color: #666666">=1</span>)
|
||||
tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=5</span>)
|
||||
tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
export_graphviz(
|
||||
tree_clf,
|
||||
out_file<span style="color: #666666">=</span><span style="color: #BA2121">"DataFiles/cancer.dot"</span>,
|
||||
feature_names<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names,
|
||||
class_names<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>target_names,
|
||||
rounded<span style="color: #666666">=</span><span style="color: #008000">True</span>,
|
||||
filled<span style="color: #666666">=</span><span style="color: #008000">True</span>
|
||||
)
|
||||
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'</span>
|
||||
os<span style="color: #666666">.</span>system(cmd)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -256,7 +241,7 @@ $$
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs016.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,42 +177,33 @@ MathJax.Hub.Config({
|
||||
<a name="part0016"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec15" class="anchor">Visualizing the Tree, Classification </h2>
|
||||
<h2 id="___sec15" class="anchor">Visualizing the Tree, The Moons </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">os</span>
|
||||
<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
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</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.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<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.metrics</span> <span style="color: #008000; font-weight: bold">import</span> confusion_matrix
|
||||
<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
|
||||
<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> export_graphviz
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<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">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
|
||||
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
X <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X)
|
||||
y <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>Categorical<span style="color: #666666">.</span>from_codes(cancer<span style="color: #666666">.</span>target, cancer<span style="color: #666666">.</span>target_names)
|
||||
y <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>get_dummies(y)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(y)
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, random_state<span style="color: #666666">=1</span>)
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.25</span>, random_state<span style="color: #666666">=53</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">=0</span>)
|
||||
tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=5</span>)
|
||||
tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
export_graphviz(
|
||||
tree_clf,
|
||||
out_file<span style="color: #666666">=</span><span style="color: #BA2121">"DataFiles/cancer.dot"</span>,
|
||||
feature_names<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names,
|
||||
class_names<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>target_names,
|
||||
out_file<span style="color: #666666">=</span><span style="color: #BA2121">"DataFiles/moons.dot"</span>,
|
||||
rounded<span style="color: #666666">=</span><span style="color: #008000">True</span>,
|
||||
filled<span style="color: #666666">=</span><span style="color: #008000">True</span>
|
||||
)
|
||||
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'</span>
|
||||
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'</span>
|
||||
os<span style="color: #666666">.</span>system(cmd)
|
||||
</pre></div>
|
||||
<p>
|
||||
@@ -247,7 +232,7 @@ os<span style="color: #666666">.</span>system(cmd)
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs017.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,35 +177,48 @@ MathJax.Hub.Config({
|
||||
<a name="part0017"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec16" class="anchor">Visualizing the Tree, The Moons </h2>
|
||||
<h2 id="___sec16" class="anchor">Computing the Gini index </h2>
|
||||
|
||||
<p>
|
||||
The example we will look at is a classical one in many Machine
|
||||
Learning applications. Based on various meteorological features, we
|
||||
have several so-called attributes which decide whether we at the end
|
||||
will do some outdoor activity like skiing, going for a bike ride etc
|
||||
etc. The table here contains the feautures <b>outlook</b>, <b>temperature</b>,
|
||||
<b>humidity</b> and <b>wind</b>. The target or output is whether we ride
|
||||
(True=1) or whether we do something else that day (False=0). The
|
||||
attributes for each feature are then sunny, overcast and rain for the
|
||||
outlook, hot, cold and mild for temperature, high and normal for
|
||||
humidity and weak and strong for wind.
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</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.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
|
||||
<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
|
||||
<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> export_graphviz
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<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">os</span>
|
||||
<p>
|
||||
The table here summarizes the various attributes and
|
||||
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.25</span>, random_state<span style="color: #666666">=53</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">=0</span>)
|
||||
tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=5</span>)
|
||||
tree_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
export_graphviz(
|
||||
tree_clf,
|
||||
out_file<span style="color: #666666">=</span><span style="color: #BA2121">"DataFiles/moons.dot"</span>,
|
||||
rounded<span style="color: #666666">=</span><span style="color: #008000">True</span>,
|
||||
filled<span style="color: #666666">=</span><span style="color: #008000">True</span>
|
||||
)
|
||||
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">'dot -Tpng DataFiles/moons.dot -o DataFiles/moons.png'</span>
|
||||
os<span style="color: #666666">.</span>system(cmd)
|
||||
</pre></div>
|
||||
<div class="row">
|
||||
<div class="col-xs-4">
|
||||
<table class="table table-striped table-hover table-condensed">
|
||||
<thead>
|
||||
<tr><td align="center"><b>Day</b></td> <td align="center"><b>Outlook </b></td> <td align="center"><b>Temperature</b></td> <td align="center"><b>Humidity</b></td> <td align="center"><b> Wind </b></td> <td align="center"><b>Ride</b></td> </tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td align="center"> 1 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 2 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 3 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 4 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 5 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 6 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 7 </td> <td align="center"> Overcast </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 8 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 9 </td> <td align="center"> Sunny </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 10 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 11 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 12 </td> <td align="center"> Overcast </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 13 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 14 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div> <!-- col-xs-4 -->
|
||||
</div> <!-- cell row -->
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -238,7 +245,7 @@ os<span style="color: #666666">.</span>system(cmd)
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs018.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,48 +177,65 @@ MathJax.Hub.Config({
|
||||
<a name="part0018"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec17" class="anchor">Computing the Gini index </h2>
|
||||
<h2 id="___sec17" class="anchor">Simple Python Code to read in Data </h2>
|
||||
|
||||
<p>
|
||||
The example we will look at is a classical one in many Machine
|
||||
Learning applications. Based on various meteorological features, we
|
||||
have several so-called attributes which decide whether we at the end
|
||||
will do some outdoor activity like skiing, going for a bike ride etc
|
||||
etc. The table here contains the feautures <b>outlook</b>, <b>temperature</b>,
|
||||
<b>humidity</b> and <b>wind</b>. The target or output is whether we ride
|
||||
(True=1) or whether we do something else that day (False=0). The
|
||||
attributes for each feature are then sunny, overcast and rain for the
|
||||
outlook, hot, cold and mild for temperature, high and normal for
|
||||
humidity and weak and strong for wind.
|
||||
|
||||
<p>
|
||||
The table here summarizes the various attributes and
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">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">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-xs-4">
|
||||
<table class="table table-striped table-hover table-condensed">
|
||||
<thead>
|
||||
<tr><td align="center"><b>Day</b></td> <td align="center"><b>Outlook </b></td> <td align="center"><b>Temperature</b></td> <td align="center"><b>Humidity</b></td> <td align="center"><b> Wind </b></td> <td align="center"><b>Ride</b></td> </tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td align="center"> 1 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 2 </td> <td align="center"> Sunny </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 3 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 4 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 5 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 6 </td> <td align="center"> Rain </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 7 </td> <td align="center"> Overcast </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 8 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Weak </td> <td align="center"> 0 </td> </tr>
|
||||
<tr><td align="center"> 9 </td> <td align="center"> Sunny </td> <td align="center"> Cool </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 10 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 11 </td> <td align="center"> Sunny </td> <td align="center"> Mild </td> <td align="center"> Normal </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 12 </td> <td align="center"> Overcast </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 13 </td> <td align="center"> Overcast </td> <td align="center"> Hot </td> <td align="center"> Normal </td> <td align="center"> Weak </td> <td align="center"> 1 </td> </tr>
|
||||
<tr><td align="center"> 14 </td> <td align="center"> Rain </td> <td align="center"> Mild </td> <td align="center"> High </td> <td align="center"> Strong </td> <td align="center"> 0 </td> </tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div> <!-- col-xs-4 -->
|
||||
</div> <!-- cell row -->
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR <span style="color: #666666">=</span> <span style="color: #BA2121">"Results"</span>
|
||||
FIGURE_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"Results/FigureFiles"</span>
|
||||
DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"DataFiles/"</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(PROJECT_ROOT_DIR):
|
||||
os<span style="color: #666666">.</span>mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(FIGURE_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(DATA_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">image_path</span>(fig_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">data_path</span>(dat_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, format<span style="color: #666666">=</span><span style="color: #BA2121">'png'</span>)
|
||||
|
||||
infile <span style="color: #666666">=</span> <span style="color: #008000">open</span>(data_path(<span style="color: #BA2121">"ride.csv"</span>),<span style="color: #BA2121">'r'</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Read the experimental data with Pandas</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
ridedata <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_csv(infile,names <span style="color: #666666">=</span> (<span style="color: #BA2121">'Outlook'</span>,<span style="color: #BA2121">'Temperature'</span>,<span style="color: #BA2121">'Humidity'</span>,<span style="color: #BA2121">'Wind'</span>,<span style="color: #BA2121">'Ride'</span>))
|
||||
ridedata <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(ridedata)
|
||||
display(ridedata)
|
||||
<span style="color: #408080; font-style: italic"># Features and targets</span>
|
||||
X <span style="color: #666666">=</span> ridedata<span style="color: #666666">.</span>loc[:, ridedata<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">'Ride'</span>]<span style="color: #666666">.</span>values
|
||||
display(X)
|
||||
y <span style="color: #666666">=</span> ridedata<span style="color: #666666">.</span>loc[:, ridedata<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">'Ride'</span>]<span style="color: #666666">.</span>values
|
||||
display(y)
|
||||
<span style="color: #408080; font-style: italic"># Categorical variables to one-hot's</span>
|
||||
onehotencoder <span style="color: #666666">=</span> OneHotEncoder(categories<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</span>)
|
||||
|
||||
X <span style="color: #666666">=</span> ColumnTransformer([(<span style="color: #BA2121">""</span>, onehotencoder)])<span style="color: #666666">.</span>fit_transform(X)
|
||||
y<span style="color: #666666">.</span>shape
|
||||
|
||||
display(X)
|
||||
display(y)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -251,7 +262,7 @@ The table here summarizes the various attributes and
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs019.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,64 +177,80 @@ MathJax.Hub.Config({
|
||||
<a name="part0019"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec18" class="anchor">Simple Python Code to read in Data </h2>
|
||||
<h2 id="___sec18" class="anchor">Computing the Gini Factor </h2>
|
||||
|
||||
<p>
|
||||
The above functions (gini, entropy and misclassification error) are
|
||||
important components of the so-called CART algorithm. We will discuss
|
||||
this algorithm below after we have discussed the information gain
|
||||
algorithm ID3.
|
||||
|
||||
<p>
|
||||
In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">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">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Split a dataset based on an attribute and an attribute value</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_split</span>(index, value, dataset):
|
||||
left, right <span style="color: #666666">=</span> <span style="color: #008000">list</span>(), <span style="color: #008000">list</span>()
|
||||
<span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset:
|
||||
<span style="color: #008000; font-weight: bold">if</span> row[index] <span style="color: #666666"><</span> value:
|
||||
left<span style="color: #666666">.</span>append(row)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
right<span style="color: #666666">.</span>append(row)
|
||||
<span style="color: #008000; font-weight: bold">return</span> left, right
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Calculate the Gini index for a split dataset</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gini_index</span>(groups, classes):
|
||||
<span style="color: #408080; font-style: italic"># count all samples at split point</span>
|
||||
n_instances <span style="color: #666666">=</span> <span style="color: #008000">float</span>(<span style="color: #008000">sum</span>([<span style="color: #008000">len</span>(group) <span style="color: #008000; font-weight: bold">for</span> group <span style="color: #AA22FF; font-weight: bold">in</span> groups]))
|
||||
<span style="color: #408080; font-style: italic"># sum weighted Gini index for each group</span>
|
||||
gini <span style="color: #666666">=</span> <span style="color: #666666">0.0</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> group <span style="color: #AA22FF; font-weight: bold">in</span> groups:
|
||||
size <span style="color: #666666">=</span> <span style="color: #008000">float</span>(<span style="color: #008000">len</span>(group))
|
||||
<span style="color: #408080; font-style: italic"># avoid divide by zero</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> size <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
<span style="color: #008000; font-weight: bold">continue</span>
|
||||
score <span style="color: #666666">=</span> <span style="color: #666666">0.0</span>
|
||||
<span style="color: #408080; font-style: italic"># score the group based on the score for each class</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> class_val <span style="color: #AA22FF; font-weight: bold">in</span> classes:
|
||||
p <span style="color: #666666">=</span> [row[<span style="color: #666666">-1</span>] <span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> group]<span style="color: #666666">.</span>count(class_val) <span style="color: #666666">/</span> size
|
||||
score <span style="color: #666666">+=</span> p <span style="color: #666666">*</span> p
|
||||
<span style="color: #408080; font-style: italic"># weight the group score by its relative size</span>
|
||||
gini <span style="color: #666666">+=</span> (<span style="color: #666666">1.0</span> <span style="color: #666666">-</span> score) <span style="color: #666666">*</span> (size <span style="color: #666666">/</span> n_instances)
|
||||
<span style="color: #008000; font-weight: bold">return</span> gini
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR <span style="color: #666666">=</span> <span style="color: #BA2121">"Results"</span>
|
||||
FIGURE_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"Results/FigureFiles"</span>
|
||||
DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"DataFiles/"</span>
|
||||
<span style="color: #408080; font-style: italic"># Select the best split point for a dataset</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">get_split</span>(dataset):
|
||||
class_values <span style="color: #666666">=</span> <span style="color: #008000">list</span>(<span style="color: #008000">set</span>(row[<span style="color: #666666">-1</span>] <span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset))
|
||||
b_index, b_value, b_score, b_groups <span style="color: #666666">=</span> <span style="color: #666666">999</span>, <span style="color: #666666">999</span>, <span style="color: #666666">999</span>, <span style="color: #008000">None</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> index <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(dataset[<span style="color: #666666">0</span>])<span style="color: #666666">-1</span>):
|
||||
<span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset:
|
||||
groups <span style="color: #666666">=</span> test_split(index, row[index], dataset)
|
||||
gini <span style="color: #666666">=</span> gini_index(groups, class_values)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'X</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121"> < </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121"> Gini=</span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> ((index<span style="color: #666666">+1</span>), row[index], gini))
|
||||
<span style="color: #008000; font-weight: bold">if</span> gini <span style="color: #666666"><</span> b_score:
|
||||
b_index, b_value, b_score, b_groups <span style="color: #666666">=</span> index, row[index], gini, groups
|
||||
<span style="color: #008000; font-weight: bold">return</span> {<span style="color: #BA2121">'index'</span>:b_index, <span style="color: #BA2121">'value'</span>:b_value, <span style="color: #BA2121">'groups'</span>:b_groups}
|
||||
|
||||
dataset <span style="color: #666666">=</span> [[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]]
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(PROJECT_ROOT_DIR):
|
||||
os<span style="color: #666666">.</span>mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(FIGURE_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(DATA_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">image_path</span>(fig_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">data_path</span>(dat_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, format<span style="color: #666666">=</span><span style="color: #BA2121">'png'</span>)
|
||||
|
||||
infile <span style="color: #666666">=</span> <span style="color: #008000">open</span>(data_path(<span style="color: #BA2121">"ride.csv"</span>),<span style="color: #BA2121">'r'</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Read the experimental data with Pandas</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
ridedata <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_csv(infile,names <span style="color: #666666">=</span> (<span style="color: #BA2121">'Outlook'</span>,<span style="color: #BA2121">'Temperature'</span>,<span style="color: #BA2121">'Humidity'</span>,<span style="color: #BA2121">'Wind'</span>,<span style="color: #BA2121">'Ride'</span>))
|
||||
ridedata <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(ridedata)
|
||||
display(ridedata)
|
||||
<span style="color: #408080; font-style: italic"># Features and targets</span>
|
||||
X <span style="color: #666666">=</span> ridedata<span style="color: #666666">.</span>loc[:, ridedata<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">'Ride'</span>]<span style="color: #666666">.</span>values
|
||||
display(X)
|
||||
y <span style="color: #666666">=</span> ridedata<span style="color: #666666">.</span>loc[:, ridedata<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">'Ride'</span>]<span style="color: #666666">.</span>values
|
||||
display(y)
|
||||
<span style="color: #408080; font-style: italic"># Categorical variables to one-hot's</span>
|
||||
onehotencoder <span style="color: #666666">=</span> OneHotEncoder(categories<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</span>)
|
||||
|
||||
X <span style="color: #666666">=</span> ColumnTransformer([(<span style="color: #BA2121">""</span>, onehotencoder)])<span style="color: #666666">.</span>fit_transform(X)
|
||||
y<span style="color: #666666">.</span>shape
|
||||
|
||||
display(X)
|
||||
display(y)
|
||||
split <span style="color: #666666">=</span> get_split(dataset)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Split: [X</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121"> < </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">]'</span> <span style="color: #666666">%</span> ((split[<span style="color: #BA2121">'index'</span>]<span style="color: #666666">+1</span>), split[<span style="color: #BA2121">'value'</span>]))
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -268,7 +278,7 @@ display(y)
|
||||
<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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs020.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,81 +177,39 @@ MathJax.Hub.Config({
|
||||
<a name="part0020"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec19" class="anchor">Computing the Gini Factor </h2>
|
||||
<h2 id="___sec19" class="anchor">Entropy and the ID3 algorithm </h2>
|
||||
|
||||
<p>
|
||||
The above functions (gini, entropy and misclassification error) are
|
||||
important components of the so-called CART algorithm. We will discuss
|
||||
this algorithm below after we have discussed the information gain
|
||||
algorithm ID3.
|
||||
ID3, learns decision trees by constructing
|
||||
them topdown, beginning with the question <b>which attribute should be tested at the root of the tree</b>?
|
||||
|
||||
<ol>
|
||||
<li> Each instance attribute is evaluated using a statistical test to determine how well it alone classifies the training examples.</li>
|
||||
<li> The best attribute is selected and used as the test at the root node of the tree.</li>
|
||||
<li> A descendant of the root node is then created for each possible value of this attribute.</li>
|
||||
<li> Training examples are sorted to the appropriate descendant node.</li>
|
||||
<li> The entire process is then repeated using the training examples associated with each descendant node to select the best attribute to test at that point in the tree.</li>
|
||||
<li> This forms a greedy search for an acceptable decision tree, in which the algorithm never backtracks to reconsider earlier choices.</li>
|
||||
</ol>
|
||||
|
||||
The ID3 algorithm selects, which attribute to test at each node in the
|
||||
tree.
|
||||
|
||||
<p>
|
||||
In the example here we have converted all our attributes into numerical values \( 0,1,2 \) etc.
|
||||
We would like to select the attribute that is most useful for classifying
|
||||
examples.
|
||||
|
||||
<p>
|
||||
What is a good quantitative measure of the worth of an attribute?
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Split a dataset based on an attribute and an attribute value</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_split</span>(index, value, dataset):
|
||||
left, right <span style="color: #666666">=</span> <span style="color: #008000">list</span>(), <span style="color: #008000">list</span>()
|
||||
<span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset:
|
||||
<span style="color: #008000; font-weight: bold">if</span> row[index] <span style="color: #666666"><</span> value:
|
||||
left<span style="color: #666666">.</span>append(row)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
right<span style="color: #666666">.</span>append(row)
|
||||
<span style="color: #008000; font-weight: bold">return</span> left, right
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Calculate the Gini index for a split dataset</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gini_index</span>(groups, classes):
|
||||
<span style="color: #408080; font-style: italic"># count all samples at split point</span>
|
||||
n_instances <span style="color: #666666">=</span> <span style="color: #008000">float</span>(<span style="color: #008000">sum</span>([<span style="color: #008000">len</span>(group) <span style="color: #008000; font-weight: bold">for</span> group <span style="color: #AA22FF; font-weight: bold">in</span> groups]))
|
||||
<span style="color: #408080; font-style: italic"># sum weighted Gini index for each group</span>
|
||||
gini <span style="color: #666666">=</span> <span style="color: #666666">0.0</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> group <span style="color: #AA22FF; font-weight: bold">in</span> groups:
|
||||
size <span style="color: #666666">=</span> <span style="color: #008000">float</span>(<span style="color: #008000">len</span>(group))
|
||||
<span style="color: #408080; font-style: italic"># avoid divide by zero</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> size <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
<span style="color: #008000; font-weight: bold">continue</span>
|
||||
score <span style="color: #666666">=</span> <span style="color: #666666">0.0</span>
|
||||
<span style="color: #408080; font-style: italic"># score the group based on the score for each class</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> class_val <span style="color: #AA22FF; font-weight: bold">in</span> classes:
|
||||
p <span style="color: #666666">=</span> [row[<span style="color: #666666">-1</span>] <span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> group]<span style="color: #666666">.</span>count(class_val) <span style="color: #666666">/</span> size
|
||||
score <span style="color: #666666">+=</span> p <span style="color: #666666">*</span> p
|
||||
<span style="color: #408080; font-style: italic"># weight the group score by its relative size</span>
|
||||
gini <span style="color: #666666">+=</span> (<span style="color: #666666">1.0</span> <span style="color: #666666">-</span> score) <span style="color: #666666">*</span> (size <span style="color: #666666">/</span> n_instances)
|
||||
<span style="color: #008000; font-weight: bold">return</span> gini
|
||||
<p>
|
||||
Information gain measures how well a given attribute separates the
|
||||
training examples according to their target classification.
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Select the best split point for a dataset</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">get_split</span>(dataset):
|
||||
class_values <span style="color: #666666">=</span> <span style="color: #008000">list</span>(<span style="color: #008000">set</span>(row[<span style="color: #666666">-1</span>] <span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset))
|
||||
b_index, b_value, b_score, b_groups <span style="color: #666666">=</span> <span style="color: #666666">999</span>, <span style="color: #666666">999</span>, <span style="color: #666666">999</span>, <span style="color: #008000">None</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> index <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(dataset[<span style="color: #666666">0</span>])<span style="color: #666666">-1</span>):
|
||||
<span style="color: #008000; font-weight: bold">for</span> row <span style="color: #AA22FF; font-weight: bold">in</span> dataset:
|
||||
groups <span style="color: #666666">=</span> test_split(index, row[index], dataset)
|
||||
gini <span style="color: #666666">=</span> gini_index(groups, class_values)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'X</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121"> < </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121"> Gini=</span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> ((index<span style="color: #666666">+1</span>), row[index], gini))
|
||||
<span style="color: #008000; font-weight: bold">if</span> gini <span style="color: #666666"><</span> b_score:
|
||||
b_index, b_value, b_score, b_groups <span style="color: #666666">=</span> index, row[index], gini, groups
|
||||
<span style="color: #008000; font-weight: bold">return</span> {<span style="color: #BA2121">'index'</span>:b_index, <span style="color: #BA2121">'value'</span>:b_value, <span style="color: #BA2121">'groups'</span>:b_groups}
|
||||
|
||||
dataset <span style="color: #666666">=</span> [[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">0</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>],
|
||||
[<span style="color: #666666">2</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]]
|
||||
<p>
|
||||
The ID3 algorithm uses this information gain measure to select among the candidate
|
||||
attributes at each step while growing the tree.
|
||||
|
||||
split <span style="color: #666666">=</span> get_split(dataset)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Split: [X</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121"> < </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">]'</span> <span style="color: #666666">%</span> ((split[<span style="color: #BA2121">'index'</span>]<span style="color: #666666">+1</span>), split[<span style="color: #BA2121">'value'</span>]))
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -284,7 +236,7 @@ split <span style="color: #666666">=</span> get_split(dataset)
|
||||
<li><a href="._DecisionTrees-bs028.html">29</a></li>
|
||||
<li><a href="._DecisionTrees-bs029.html">30</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs021.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,39 +177,199 @@ MathJax.Hub.Config({
|
||||
<a name="part0021"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec20" class="anchor">Entropy and the ID3 algorithm </h2>
|
||||
<h2 id="___sec20" class="anchor">Implementing the ID3 Algorithm </h2>
|
||||
|
||||
<p>
|
||||
ID3, learns decision trees by constructing
|
||||
them topdown, beginning with the question <b>which attribute should be tested at the root of the tree</b>?
|
||||
|
||||
<ol>
|
||||
<li> Each instance attribute is evaluated using a statistical test to determine how well it alone classifies the training examples.</li>
|
||||
<li> The best attribute is selected and used as the test at the root node of the tree.</li>
|
||||
<li> A descendant of the root node is then created for each possible value of this attribute.</li>
|
||||
<li> Training examples are sorted to the appropriate descendant node.</li>
|
||||
<li> The entire process is then repeated using the training examples associated with each descendant node to select the best attribute to test at that point in the tree.</li>
|
||||
<li> This forms a greedy search for an acceptable decision tree, in which the algorithm never backtracks to reconsider earlier choices.</li>
|
||||
</ol>
|
||||
<!-- 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">re</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">math</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">collections</span> <span style="color: #008000; font-weight: bold">import</span> deque
|
||||
|
||||
The ID3 algorithm selects, which attribute to test at each node in the
|
||||
tree.
|
||||
<span style="color: #408080; font-style: italic"># x is examples in training set</span>
|
||||
<span style="color: #408080; font-style: italic"># y is set of targets</span>
|
||||
<span style="color: #408080; font-style: italic"># label is target attributes</span>
|
||||
<span style="color: #408080; font-style: italic"># Node is a class which has properties values, childs, and next</span>
|
||||
<span style="color: #408080; font-style: italic"># root is top node in the decision tree</span>
|
||||
|
||||
<p>
|
||||
We would like to select the attribute that is most useful for classifying
|
||||
examples.
|
||||
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Node</span>(<span style="color: #008000">object</span>):
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>childs <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
|
||||
<p>
|
||||
What is a good quantitative measure of the worth of an attribute?
|
||||
<span style="color: #408080; font-style: italic"># Simple class of Decision Tree</span>
|
||||
<span style="color: #408080; font-style: italic"># Aimed for who want to learn Decision Tree, so it is not optimized</span>
|
||||
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">DecisionTree</span>(<span style="color: #008000">object</span>):
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, sample, attributes, labels):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>sample <span style="color: #666666">=</span> sample
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes <span style="color: #666666">=</span> attributes
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labels <span style="color: #666666">=</span> labels
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>initLabelCodes()
|
||||
<span style="color: #408080; font-style: italic"># print(self.labelCodes)</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>entropy <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy([x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels))])
|
||||
|
||||
<p>
|
||||
Information gain measures how well a given attribute separates the
|
||||
training examples according to their target classification.
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">initLabelCodes</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels:
|
||||
<span style="color: #008000; font-weight: bold">if</span> l <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes:
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>append(l)
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(l)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
|
||||
<p>
|
||||
The ID3 algorithm uses this information gain measure to select among the candidate
|
||||
attributes at each step while growing the tree.
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabelCodeId</span>(<span style="color: #008000">self</span>, sampleId):
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId])
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeValues</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
|
||||
vals <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
|
||||
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> vals:
|
||||
vals<span style="color: #666666">.</span>append(val)
|
||||
<span style="color: #408080; font-style: italic"># print(vals)</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> vals
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getEntropy</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
entropy <span style="color: #666666">=</span> <span style="color: #666666">0</span>
|
||||
labelCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
labelCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>getLabelCodeId(sid)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #408080; font-style: italic"># print("-ge", labelCount)</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> lv <span style="color: #AA22FF; font-weight: bold">in</span> labelCount:
|
||||
<span style="color: #408080; font-style: italic"># print(lv)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> lv <span style="color: #666666">!=</span> <span style="color: #666666">0</span>:
|
||||
entropy <span style="color: #666666">+=</span> <span style="color: #666666">-</span>lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> math<span style="color: #666666">.</span>log(lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds), <span style="color: #666666">2</span>)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
entropy <span style="color: #666666">+=</span> <span style="color: #666666">0</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> entropy
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getDominantLabel</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
labelCodesCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid])] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes[labelCodesCount<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(labelCodesCount))]
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
|
||||
gain <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(sampleIds)
|
||||
attributeVals <span style="color: #666666">=</span> []
|
||||
attributeValsCount <span style="color: #666666">=</span> []
|
||||
attributeValsIds <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
|
||||
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> attributeVals:
|
||||
attributeVals<span style="color: #666666">.</span>append(val)
|
||||
attributeValsCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
|
||||
attributeValsIds<span style="color: #666666">.</span>append([])
|
||||
vid <span style="color: #666666">=</span> attributeVals<span style="color: #666666">.</span>index(val)
|
||||
attributeValsCount[vid] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
attributeValsIds[vid]<span style="color: #666666">.</span>append(sid)
|
||||
<span style="color: #408080; font-style: italic"># print("-gig", self.attributes[attributeId])</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> vc, vids <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(attributeValsCount, attributeValsIds):
|
||||
<span style="color: #408080; font-style: italic"># print("-gig", vids)</span>
|
||||
gain <span style="color: #666666">-=</span> vc<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(vids)
|
||||
<span style="color: #008000; font-weight: bold">return</span> gain
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeMaxInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds):
|
||||
attributesEntropy <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(attributeIds)
|
||||
<span style="color: #008000; font-weight: bold">for</span> i, attId <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(<span style="color: #008000">range</span>(<span style="color: #008000">len</span>(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getInformationGain(sampleIds, attId)
|
||||
maxId <span style="color: #666666">=</span> attributeIds[attributesEntropy<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(attributesEntropy))]
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>attributes[maxId], maxId
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">isSingleLabeled</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
label <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid] <span style="color: #666666">!=</span> label:
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">False</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">True</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabel</span>(<span style="color: #008000">self</span>, sampleId):
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId]
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3</span>(<span style="color: #008000">self</span>):
|
||||
sampleIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>sample))]
|
||||
attributeIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes))]
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(sampleIds, attributeIds, <span style="color: #008000">self</span><span style="color: #666666">.</span>root)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3Recv</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds, root):
|
||||
root <span style="color: #666666">=</span> Node() <span style="color: #408080; font-style: italic"># Initialize current root</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>isSingleLabeled(sampleIds):
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
bestAttrName, bestAttrId <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeMaxInformationGain(
|
||||
sampleIds, attributeIds)
|
||||
<span style="color: #408080; font-style: italic"># print(bestAttrName)</span>
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> bestAttrName
|
||||
root<span style="color: #666666">.</span>childs <span style="color: #666666">=</span> [] <span style="color: #408080; font-style: italic"># Create list of children</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> value <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeValues(sampleIds, bestAttrId):
|
||||
<span style="color: #408080; font-style: italic"># print(value)</span>
|
||||
child <span style="color: #666666">=</span> Node()
|
||||
child<span style="color: #666666">.</span>value <span style="color: #666666">=</span> value
|
||||
root<span style="color: #666666">.</span>childs<span style="color: #666666">.</span>append(child) <span style="color: #408080; font-style: italic"># Append new child node to current</span>
|
||||
<span style="color: #408080; font-style: italic"># root</span>
|
||||
childSampleIds <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][bestAttrId] <span style="color: #666666">==</span> value:
|
||||
childSampleIds<span style="color: #666666">.</span>append(sid)
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(childSampleIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
<span style="color: #408080; font-style: italic"># print(bestAttrName, bestAttrId)</span>
|
||||
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #AA22FF; font-weight: bold">and</span> bestAttrId <span style="color: #AA22FF; font-weight: bold">in</span> attributeIds:
|
||||
toRemove <span style="color: #666666">=</span> attributeIds<span style="color: #666666">.</span>index(bestAttrId)
|
||||
attributeIds<span style="color: #666666">.</span>pop(toRemove)
|
||||
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(
|
||||
childSampleIds, attributeIds, child<span style="color: #666666">.</span>next)
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">printTree</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>root:
|
||||
roots <span style="color: #666666">=</span> deque()
|
||||
roots<span style="color: #666666">.</span>append(<span style="color: #008000">self</span><span style="color: #666666">.</span>root)
|
||||
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #008000">len</span>(roots) <span style="color: #666666">></span> <span style="color: #666666">0</span>:
|
||||
root <span style="color: #666666">=</span> roots<span style="color: #666666">.</span>popleft()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>value)
|
||||
<span style="color: #008000; font-weight: bold">if</span> root<span style="color: #666666">.</span>childs:
|
||||
<span style="color: #008000; font-weight: bold">for</span> child <span style="color: #AA22FF; font-weight: bold">in</span> root<span style="color: #666666">.</span>childs:
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'({})'</span><span style="color: #666666">.</span>format(child<span style="color: #666666">.</span>value))
|
||||
roots<span style="color: #666666">.</span>append(child<span style="color: #666666">.</span>next)
|
||||
<span style="color: #008000; font-weight: bold">elif</span> root<span style="color: #666666">.</span>next:
|
||||
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>next)
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test</span>():
|
||||
f <span style="color: #666666">=</span> <span style="color: #008000">open</span>(<span style="color: #BA2121">'DataFiles/rideclass.csv'</span>)
|
||||
attributes <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readline()<span style="color: #666666">.</span>split(<span style="color: #BA2121">','</span>)
|
||||
attributes <span style="color: #666666">=</span> attributes[<span style="color: #666666">1</span>:<span style="color: #008000">len</span>(attributes)<span style="color: #666666">-1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(attributes)
|
||||
sample <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readlines()
|
||||
f<span style="color: #666666">.</span>close()
|
||||
<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>(sample)):
|
||||
sample[i] <span style="color: #666666">=</span> re<span style="color: #666666">.</span>sub(<span style="color: #BA2121">'\d+,'</span>, <span style="color: #BA2121">''</span>, sample[i])
|
||||
sample[i] <span style="color: #666666">=</span> sample[i]<span style="color: #666666">.</span>strip()<span style="color: #666666">.</span>split(<span style="color: #BA2121">','</span>)
|
||||
labels <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> s <span style="color: #AA22FF; font-weight: bold">in</span> sample:
|
||||
labels<span style="color: #666666">.</span>append(s<span style="color: #666666">.</span>pop())
|
||||
<span style="color: #408080; font-style: italic"># print(sample)</span>
|
||||
<span style="color: #408080; font-style: italic"># print(labels)</span>
|
||||
decisionTree <span style="color: #666666">=</span> DecisionTree(sample, attributes, labels)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"System entropy {}"</span><span style="color: #666666">.</span>format(decisionTree<span style="color: #666666">.</span>entropy))
|
||||
decisionTree<span style="color: #666666">.</span>id3()
|
||||
decisionTree<span style="color: #666666">.</span>printTree()
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #19177C">__name__</span> <span style="color: #666666">==</span> <span style="color: #BA2121">'__main__'</span>:
|
||||
test()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -242,7 +396,7 @@ attributes at each step while growing the tree.
|
||||
<li><a href="._DecisionTrees-bs029.html">30</a></li>
|
||||
<li><a href="._DecisionTrees-bs030.html">31</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs022.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,212 +177,52 @@ MathJax.Hub.Config({
|
||||
<a name="part0022"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec21" class="anchor">Implementing the ID3 Algorithm </h2>
|
||||
|
||||
<h2 id="___sec21" class="anchor">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<p>
|
||||
import re
|
||||
import math
|
||||
from collections import deque
|
||||
|
||||
<p>
|
||||
<!-- x is examples in training set -->
|
||||
<!-- y is set of attributes -->
|
||||
<!-- label is target attributes -->
|
||||
<!-- Node is a class which has properties values, childs, and next -->
|
||||
<!-- root is top node in the decision tree -->
|
||||
<!-- 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">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
|
||||
|
||||
<p>
|
||||
class Node(object):
|
||||
def __init__(self):
|
||||
self.value = None
|
||||
self.next = None
|
||||
self.childs = None
|
||||
|
||||
<p>
|
||||
<!-- Simple class of Decision Tree -->
|
||||
<!-- Aimed for who want to learn Decision Tree, so it is not optimized -->
|
||||
class DecisionTree(object):
|
||||
def __init__(self, sample, attributes, labels):
|
||||
self.sample = sample
|
||||
self.attributes = attributes
|
||||
self.labels = labels
|
||||
self.labelCodes = None
|
||||
self.labelCodesCount = None
|
||||
self.initLabelCodes()
|
||||
# print(self.labelCodes)
|
||||
self.root = None
|
||||
self.entropy = self.getEntropy([x for x in range(len(self.labels))])
|
||||
|
||||
<p>
|
||||
def initLabelCodes(self):
|
||||
self.labelCodes = []
|
||||
self.labelCodesCount = []
|
||||
for l in self.labels:
|
||||
if l not in self.labelCodes:
|
||||
self.labelCodes.append(l)
|
||||
self.labelCodesCount.append(0)
|
||||
self.labelCodesCount[self.labelCodes.index(l)] += 1
|
||||
|
||||
<p>
|
||||
def getLabelCodeId(self, sampleId):
|
||||
return self.labelCodes.index(self.labels[sampleId])
|
||||
|
||||
<p>
|
||||
def getAttributeValues(self, sampleIds, attributeId):
|
||||
vals = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in vals:
|
||||
vals.append(val)
|
||||
# print(vals)
|
||||
return vals
|
||||
|
||||
<p>
|
||||
def getEntropy(self, sampleIds):
|
||||
entropy = 0
|
||||
labelCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCount[self.getLabelCodeId(sid)] += 1
|
||||
# print("-ge", labelCount)
|
||||
for lv in labelCount:
|
||||
# print(lv)
|
||||
if lv != 0:
|
||||
entropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)
|
||||
else:
|
||||
entropy += 0
|
||||
return entropy
|
||||
|
||||
<p>
|
||||
def getDominantLabel(self, sampleIds):
|
||||
labelCodesCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCodesCount[self.labelCodes.index(self.labels[sid])] += 1
|
||||
return self.labelCodes[labelCodesCount.index(max(labelCodesCount))]
|
||||
|
||||
<p>
|
||||
def getInformationGain(self, sampleIds, attributeId):
|
||||
gain = self.getEntropy(sampleIds)
|
||||
attributeVals = []
|
||||
attributeValsCount = []
|
||||
attributeValsIds = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in attributeVals:
|
||||
attributeVals.append(val)
|
||||
attributeValsCount.append(0)
|
||||
attributeValsIds.append([])
|
||||
vid = attributeVals.index(val)
|
||||
attributeValsCount[vid] += 1
|
||||
attributeValsIds[vid].append(sid)
|
||||
# print("-gig", self.attributes[attributeId])
|
||||
for vc, vids in zip(attributeValsCount, attributeValsIds):
|
||||
# print("-gig", vids)
|
||||
gain -= vc/len(sampleIds) * self.getEntropy(vids)
|
||||
return gain
|
||||
|
||||
<p>
|
||||
def getAttributeMaxInformationGain(self, sampleIds, attributeIds):
|
||||
attributesEntropy = [0] * len(attributeIds)
|
||||
for i, attId in zip(range(len(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = self.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
|
||||
return self.attributes[maxId], maxId
|
||||
|
||||
<p>
|
||||
def isSingleLabeled(self, sampleIds):
|
||||
label = self.labels[sampleIds[0]]
|
||||
for sid in sampleIds:
|
||||
if self.labels[sid] != label:
|
||||
return False
|
||||
return True
|
||||
|
||||
<p>
|
||||
def getLabel(self, sampleId):
|
||||
return self.labels[sampleId]
|
||||
|
||||
<p>
|
||||
def id3(self):
|
||||
sampleIds = [x for x in range(len(self.sample))]
|
||||
attributeIds = [x for x in range(len(self.attributes))]
|
||||
self.root = self.id3Recv(sampleIds, attributeIds, self.root)
|
||||
|
||||
<p>
|
||||
def id3Recv(self, sampleIds, attributeIds, root):
|
||||
root = Node() # Initialize current root
|
||||
if self.isSingleLabeled(sampleIds):
|
||||
root.value = self.labels[sampleIds[0]]
|
||||
return root
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) == 0:
|
||||
root.value = self.getDominantLabel(sampleIds)
|
||||
return root
|
||||
bestAttrName, bestAttrId = self.getAttributeMaxInformationGain(
|
||||
sampleIds, attributeIds)
|
||||
# print(bestAttrName)
|
||||
root.value = bestAttrName
|
||||
root.childs = [] # Create list of children
|
||||
for value in self.getAttributeValues(sampleIds, bestAttrId):
|
||||
# print(value)
|
||||
child = Node()
|
||||
child.value = value
|
||||
root.childs.append(child) # Append new child node to current
|
||||
# root
|
||||
childSampleIds = []
|
||||
for sid in sampleIds:
|
||||
if self.sample[sid][bestAttrId] == value:
|
||||
childSampleIds.append(sid)
|
||||
if len(childSampleIds) == 0:
|
||||
child.next = self.getDominantLabel(sampleIds)
|
||||
else:
|
||||
# print(bestAttrName, bestAttrId)
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) > 0 and bestAttrId in attributeIds:
|
||||
toRemove = attributeIds.index(bestAttrId)
|
||||
attributeIds.pop(toRemove)
|
||||
child.next = self.id3Recv(
|
||||
childSampleIds, attributeIds, child.next)
|
||||
return root
|
||||
|
||||
<p>
|
||||
def printTree(self):
|
||||
if self.root:
|
||||
roots = deque()
|
||||
roots.append(self.root)
|
||||
while len(roots) > 0:
|
||||
root = roots.popleft()
|
||||
print(root.value)
|
||||
if root.childs:
|
||||
for child in root.childs:
|
||||
print('({})'.format(child.value))
|
||||
roots.append(child.next)
|
||||
elif root.next:
|
||||
print(root.next)
|
||||
|
||||
<p>
|
||||
def test():
|
||||
f = open('DataFiles/rideclass.csv')
|
||||
attributes = f.readline().split(',')
|
||||
attributes = attributes[1:len(attributes)-1]
|
||||
print(attributes)
|
||||
sample = f.readlines()
|
||||
f.close()
|
||||
for i in range(len(sample)):
|
||||
sample[i] = re.sub('\d+,', '', sample[i])
|
||||
sample[i] = sample[i].strip().split(',')
|
||||
labels = []
|
||||
for s in sample:
|
||||
labels.append(s.pop())
|
||||
# print(sample)
|
||||
# print(labels)
|
||||
decisionTree = DecisionTree(sample, attributes, labels)
|
||||
print("System entropy {}".format(decisionTree.entropy))
|
||||
decisionTree.id3()
|
||||
decisionTree.printTree()
|
||||
|
||||
<p>
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
<span style="color: #408080; font-style: italic"># Load the data</span>
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
|
||||
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">'lbfgs'</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">"Test set accuracy with Logistic Regression: {:.2f}"</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">'auto'</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">"Test set accuracy with SVM: {:.2f}"</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">"Test set accuracy with Decision Trees: {:.2f}"</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">"Test set accuracy Logistic Regression with scaled data: {:.2f}"</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">"Test set accuracy SVM with scaled data: {:.2f}"</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">"Test set accuracy with Decision Trees and scaled data: {:.2f}"</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>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -415,7 +249,7 @@ if __name__ == '__main__':
|
||||
<li><a href="._DecisionTrees-bs030.html">31</a></li>
|
||||
<li><a href="._DecisionTrees-bs031.html">32</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs023.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,51 +177,74 @@ MathJax.Hub.Config({
|
||||
<a name="part0023"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec22" class="anchor">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<h2 id="___sec22" class="anchor">Another example, 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">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>
|
||||
<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">__future__</span> <span style="color: #008000; font-weight: bold">import</span> division, print_function, unicode_literals
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Common imports</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">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># to make this notebook's output stable across runs</span>
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># To plot pretty figures</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.colors</span> <span style="color: #008000; font-weight: bold">import</span> ListedColormap
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'axes.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'xtick.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'ytick.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
|
||||
|
||||
|
||||
<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</span> <span style="color: #008000; font-weight: bold">import</span> datasets
|
||||
<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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
|
||||
<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> export_graphviz
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Load the data</span>
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
Xm, ym <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.25</span>, random_state<span style="color: #666666">=53</span>)
|
||||
|
||||
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">'lbfgs'</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">"Test set accuracy with Logistic Regression: {:.2f}"</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">'auto'</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">"Test set accuracy with SVM: {:.2f}"</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">"Test set accuracy with Decision Trees: {:.2f}"</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">"Test set accuracy Logistic Regression with scaled data: {:.2f}"</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">"Test set accuracy SVM with scaled data: {:.2f}"</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">"Test set accuracy with Decision Trees and scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
deep_tree_clf1 <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
deep_tree_clf2 <span style="color: #666666">=</span> DecisionTreeClassifier(min_samples_leaf<span style="color: #666666">=4</span>, random_state<span style="color: #666666">=42</span>)
|
||||
deep_tree_clf1<span style="color: #666666">.</span>fit(Xm, ym)
|
||||
deep_tree_clf2<span style="color: #666666">.</span>fit(Xm, ym)
|
||||
|
||||
|
||||
<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">0</span>, <span style="color: #666666">7.5</span>, <span style="color: #666666">0</span>, <span style="color: #666666">3</span>], iris<span style="color: #666666">=</span><span style="color: #008000">True</span>, legend<span style="color: #666666">=</span><span style="color: #008000">False</span>, plot_training<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">'#fafab0'</span>,<span style="color: #BA2121">'#9898ff'</span>,<span style="color: #BA2121">'#a0faa0'</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> <span style="color: #AA22FF; font-weight: bold">not</span> iris:
|
||||
custom_cmap2 <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">'#7d7d58'</span>,<span style="color: #BA2121">'#4c4c7f'</span>,<span style="color: #BA2121">'#507d50'</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>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> plot_training:
|
||||
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">"yo"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Setosa"</span>)
|
||||
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">"bs"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Versicolor"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==2</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==2</span>], <span style="color: #BA2121">"g^"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Virginica"</span>)
|
||||
plt<span style="color: #666666">.</span>axis(axes)
|
||||
<span style="color: #008000; font-weight: bold">if</span> iris:
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Petal length"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Petal width"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r"$x_2$"</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> legend:
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"lower right"</span>, fontsize<span style="color: #666666">=14</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(deep_tree_clf1, Xm, ym, 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>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"No restrictions"</span>, fontsize<span style="color: #666666">=16</span>)
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_decision_boundary(deep_tree_clf2, Xm, ym, 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>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"min_samples_leaf = {}"</span><span style="color: #666666">.</span>format(deep_tree_clf2<span style="color: #666666">.</span>min_samples_leaf), fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -255,7 +272,7 @@ deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<li><a href="._DecisionTrees-bs031.html">32</a></li>
|
||||
<li><a href="._DecisionTrees-bs032.html">33</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs024.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,73 +177,29 @@ MathJax.Hub.Config({
|
||||
<a name="part0024"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec23" class="anchor">Another example, the moons again </h2>
|
||||
<h2 id="___sec23" class="anchor">Playing around with regions </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">__future__</span> <span style="color: #008000; font-weight: bold">import</span> division, print_function, unicode_literals
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">6</span>)
|
||||
Xs <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>, <span style="color: #666666">2</span>) <span style="color: #666666">-</span> <span style="color: #666666">0.5</span>
|
||||
ys <span style="color: #666666">=</span> (Xs[:, <span style="color: #666666">0</span>] <span style="color: #666666">></span> <span style="color: #666666">0</span>)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>float32) <span style="color: #666666">*</span> <span style="color: #666666">2</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
|
||||
angle <span style="color: #666666">=</span> np<span style="color: #666666">.</span>pi<span style="color: #666666">/4</span>
|
||||
rotation_matrix <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[np<span style="color: #666666">.</span>cos(angle), <span style="color: #666666">-</span>np<span style="color: #666666">.</span>sin(angle)], [np<span style="color: #666666">.</span>sin(angle), np<span style="color: #666666">.</span>cos(angle)]])
|
||||
Xsr <span style="color: #666666">=</span> Xs<span style="color: #666666">.</span>dot(rotation_matrix)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># to make this notebook's output stable across runs</span>
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
tree_clf_s <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
tree_clf_s<span style="color: #666666">.</span>fit(Xs, ys)
|
||||
tree_clf_sr <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
tree_clf_sr<span style="color: #666666">.</span>fit(Xsr, ys)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># To plot pretty figures</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib.colors</span> <span style="color: #008000; font-weight: bold">import</span> ListedColormap
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'axes.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'xtick.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'ytick.labelsize'</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
|
||||
|
||||
|
||||
<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</span> <span style="color: #008000; font-weight: bold">import</span> datasets
|
||||
<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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
|
||||
<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> export_graphviz
|
||||
|
||||
Xm, ym <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.25</span>, random_state<span style="color: #666666">=53</span>)
|
||||
|
||||
deep_tree_clf1 <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
deep_tree_clf2 <span style="color: #666666">=</span> DecisionTreeClassifier(min_samples_leaf<span style="color: #666666">=4</span>, random_state<span style="color: #666666">=42</span>)
|
||||
deep_tree_clf1<span style="color: #666666">.</span>fit(Xm, ym)
|
||||
deep_tree_clf2<span style="color: #666666">.</span>fit(Xm, ym)
|
||||
|
||||
|
||||
<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">0</span>, <span style="color: #666666">7.5</span>, <span style="color: #666666">0</span>, <span style="color: #666666">3</span>], iris<span style="color: #666666">=</span><span style="color: #008000">True</span>, legend<span style="color: #666666">=</span><span style="color: #008000">False</span>, plot_training<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">'#fafab0'</span>,<span style="color: #BA2121">'#9898ff'</span>,<span style="color: #BA2121">'#a0faa0'</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> <span style="color: #AA22FF; font-weight: bold">not</span> iris:
|
||||
custom_cmap2 <span style="color: #666666">=</span> ListedColormap([<span style="color: #BA2121">'#7d7d58'</span>,<span style="color: #BA2121">'#4c4c7f'</span>,<span style="color: #BA2121">'#507d50'</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>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> plot_training:
|
||||
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">"yo"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Setosa"</span>)
|
||||
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">"bs"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Versicolor"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==2</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==2</span>], <span style="color: #BA2121">"g^"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"Iris-Virginica"</span>)
|
||||
plt<span style="color: #666666">.</span>axis(axes)
|
||||
<span style="color: #008000; font-weight: bold">if</span> iris:
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Petal length"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Petal width"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r"$x_2$"</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> legend:
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"lower right"</span>, fontsize<span style="color: #666666">=14</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(deep_tree_clf1, Xm, ym, 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>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"No restrictions"</span>, fontsize<span style="color: #666666">=16</span>)
|
||||
plot_decision_boundary(tree_clf_s, Xs, ys, axes<span style="color: #666666">=</span>[<span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_decision_boundary(deep_tree_clf2, Xm, ym, 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>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"min_samples_leaf = {}"</span><span style="color: #666666">.</span>format(deep_tree_clf2<span style="color: #666666">.</span>min_samples_leaf), fontsize<span style="color: #666666">=14</span>)
|
||||
plot_decision_boundary(tree_clf_sr, Xsr, ys, axes<span style="color: #666666">=</span>[<span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
@@ -278,7 +228,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._DecisionTrees-bs032.html">33</a></li>
|
||||
<li><a href="._DecisionTrees-bs033.html">34</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs025.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,30 +177,24 @@ MathJax.Hub.Config({
|
||||
<a name="part0025"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec24" class="anchor">Playing around with regions </h2>
|
||||
<h2 id="___sec24" class="anchor">Regression 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>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">6</span>)
|
||||
Xs <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>, <span style="color: #666666">2</span>) <span style="color: #666666">-</span> <span style="color: #666666">0.5</span>
|
||||
ys <span style="color: #666666">=</span> (Xs[:, <span style="color: #666666">0</span>] <span style="color: #666666">></span> <span style="color: #666666">0</span>)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>float32) <span style="color: #666666">*</span> <span style="color: #666666">2</span>
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Quadratic training set + noise</span>
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
m <span style="color: #666666">=</span> <span style="color: #666666">200</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(m, <span style="color: #666666">1</span>)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4</span> <span style="color: #666666">*</span> (X <span style="color: #666666">-</span> <span style="color: #666666">0.5</span>) <span style="color: #666666">**</span> <span style="color: #666666">2</span>
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(m, <span style="color: #666666">1</span>) <span style="color: #666666">/</span> <span style="color: #666666">10</span>
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
angle <span style="color: #666666">=</span> np<span style="color: #666666">.</span>pi<span style="color: #666666">/4</span>
|
||||
rotation_matrix <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[np<span style="color: #666666">.</span>cos(angle), <span style="color: #666666">-</span>np<span style="color: #666666">.</span>sin(angle)], [np<span style="color: #666666">.</span>sin(angle), np<span style="color: #666666">.</span>cos(angle)]])
|
||||
Xsr <span style="color: #666666">=</span> Xs<span style="color: #666666">.</span>dot(rotation_matrix)
|
||||
<!-- 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.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
|
||||
tree_clf_s <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
tree_clf_s<span style="color: #666666">.</span>fit(Xs, ys)
|
||||
tree_clf_sr <span style="color: #666666">=</span> DecisionTreeClassifier(random_state<span style="color: #666666">=42</span>)
|
||||
tree_clf_sr<span style="color: #666666">.</span>fit(Xsr, ys)
|
||||
|
||||
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_s, Xs, ys, axes<span style="color: #666666">=</span>[<span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_decision_boundary(tree_clf_sr, Xsr, ys, axes<span style="color: #666666">=</span>[<span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">-0.7</span>, <span style="color: #666666">0.7</span>], iris<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
tree_reg <span style="color: #666666">=</span> DecisionTreeRegressor(max_depth<span style="color: #666666">=2</span>, random_state<span style="color: #666666">=42</span>)
|
||||
tree_reg<span style="color: #666666">.</span>fit(X, y)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -234,7 +222,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._DecisionTrees-bs033.html">34</a></li>
|
||||
<li><a href="._DecisionTrees-bs034.html">35</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs026.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,24 +177,80 @@ MathJax.Hub.Config({
|
||||
<a name="part0026"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec25" class="anchor">Regression 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: #408080; font-style: italic"># Quadratic training set + noise</span>
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
|
||||
m <span style="color: #666666">=</span> <span style="color: #666666">200</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(m, <span style="color: #666666">1</span>)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4</span> <span style="color: #666666">*</span> (X <span style="color: #666666">-</span> <span style="color: #666666">0.5</span>) <span style="color: #666666">**</span> <span style="color: #666666">2</span>
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(m, <span style="color: #666666">1</span>) <span style="color: #666666">/</span> <span style="color: #666666">10</span>
|
||||
</pre></div>
|
||||
<h2 id="___sec25" class="anchor">Final regressor code </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.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
|
||||
tree_reg <span style="color: #666666">=</span> DecisionTreeRegressor(max_depth<span style="color: #666666">=2</span>, random_state<span style="color: #666666">=42</span>)
|
||||
tree_reg<span style="color: #666666">.</span>fit(X, y)
|
||||
tree_reg1 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, max_depth<span style="color: #666666">=2</span>)
|
||||
tree_reg2 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, max_depth<span style="color: #666666">=3</span>)
|
||||
tree_reg1<span style="color: #666666">.</span>fit(X, y)
|
||||
tree_reg2<span style="color: #666666">.</span>fit(X, y)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_regression_predictions</span>(tree_reg, X, y, axes<span style="color: #666666">=</span>[<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], ylabel<span style="color: #666666">=</span><span style="color: #BA2121">"$y$"</span>):
|
||||
x1 <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">500</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
y_pred <span style="color: #666666">=</span> tree_reg<span style="color: #666666">.</span>predict(x1)
|
||||
plt<span style="color: #666666">.</span>axis(axes)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> ylabel:
|
||||
plt<span style="color: #666666">.</span>ylabel(ylabel, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</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_regression_predictions(tree_reg1, X, y)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split, style <span style="color: #AA22FF; font-weight: bold">in</span> ((<span style="color: #666666">0.1973</span>, <span style="color: #BA2121">"k-"</span>), (<span style="color: #666666">0.0917</span>, <span style="color: #BA2121">"k--"</span>), (<span style="color: #666666">0.7718</span>, <span style="color: #BA2121">"k--"</span>)):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], style, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.21</span>, <span style="color: #666666">0.65</span>, <span style="color: #BA2121">"Depth=0"</span>, fontsize<span style="color: #666666">=15</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.01</span>, <span style="color: #666666">0.2</span>, <span style="color: #BA2121">"Depth=1"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.65</span>, <span style="color: #666666">0.8</span>, <span style="color: #BA2121">"Depth=1"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"upper center"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"max_depth=2"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_regression_predictions(tree_reg2, X, y, ylabel<span style="color: #666666">=</span><span style="color: #008000">None</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split, style <span style="color: #AA22FF; font-weight: bold">in</span> ((<span style="color: #666666">0.1973</span>, <span style="color: #BA2121">"k-"</span>), (<span style="color: #666666">0.0917</span>, <span style="color: #BA2121">"k--"</span>), (<span style="color: #666666">0.7718</span>, <span style="color: #BA2121">"k--"</span>)):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], style, linewidth<span style="color: #666666">=2</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split <span style="color: #AA22FF; font-weight: bold">in</span> (<span style="color: #666666">0.0458</span>, <span style="color: #666666">0.1298</span>, <span style="color: #666666">0.2873</span>, <span style="color: #666666">0.9040</span>):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], <span style="color: #BA2121">"k:"</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.3</span>, <span style="color: #666666">0.5</span>, <span style="color: #BA2121">"Depth=2"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"max_depth=3"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</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_reg1 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>)
|
||||
tree_reg2 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, min_samples_leaf<span style="color: #666666">=10</span>)
|
||||
tree_reg1<span style="color: #666666">.</span>fit(X, y)
|
||||
tree_reg2<span style="color: #666666">.</span>fit(X, y)
|
||||
|
||||
x1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">500</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
y_pred1 <span style="color: #666666">=</span> tree_reg1<span style="color: #666666">.</span>predict(x1)
|
||||
y_pred2 <span style="color: #666666">=</span> tree_reg2<span style="color: #666666">.</span>predict(x1)
|
||||
|
||||
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>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred1, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1.1</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"$y$"</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"upper center"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"No restrictions"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred2, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1.1</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"min_samples_leaf={}"</span><span style="color: #666666">.</span>format(tree_reg2<span style="color: #666666">.</span>min_samples_leaf), fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -228,7 +278,7 @@ tree_reg<span style="color: #666666">.</span>fit(X, y)
|
||||
<li><a href="._DecisionTrees-bs034.html">35</a></li>
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs027.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,82 +177,18 @@ MathJax.Hub.Config({
|
||||
<a name="part0027"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec26" class="anchor">Final regressor code </h2>
|
||||
<p>
|
||||
<h2 id="___sec26" 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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
|
||||
<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>
|
||||
|
||||
tree_reg1 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, max_depth<span style="color: #666666">=2</span>)
|
||||
tree_reg2 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, max_depth<span style="color: #666666">=3</span>)
|
||||
tree_reg1<span style="color: #666666">.</span>fit(X, y)
|
||||
tree_reg2<span style="color: #666666">.</span>fit(X, y)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_regression_predictions</span>(tree_reg, X, y, axes<span style="color: #666666">=</span>[<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], ylabel<span style="color: #666666">=</span><span style="color: #BA2121">"$y$"</span>):
|
||||
x1 <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">500</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
y_pred <span style="color: #666666">=</span> tree_reg<span style="color: #666666">.</span>predict(x1)
|
||||
plt<span style="color: #666666">.</span>axis(axes)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> ylabel:
|
||||
plt<span style="color: #666666">.</span>ylabel(ylabel, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</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_regression_predictions(tree_reg1, X, y)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split, style <span style="color: #AA22FF; font-weight: bold">in</span> ((<span style="color: #666666">0.1973</span>, <span style="color: #BA2121">"k-"</span>), (<span style="color: #666666">0.0917</span>, <span style="color: #BA2121">"k--"</span>), (<span style="color: #666666">0.7718</span>, <span style="color: #BA2121">"k--"</span>)):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], style, linewidth<span style="color: #666666">=2</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.21</span>, <span style="color: #666666">0.65</span>, <span style="color: #BA2121">"Depth=0"</span>, fontsize<span style="color: #666666">=15</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.01</span>, <span style="color: #666666">0.2</span>, <span style="color: #BA2121">"Depth=1"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.65</span>, <span style="color: #666666">0.8</span>, <span style="color: #BA2121">"Depth=1"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"upper center"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"max_depth=2"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_regression_predictions(tree_reg2, X, y, ylabel<span style="color: #666666">=</span><span style="color: #008000">None</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split, style <span style="color: #AA22FF; font-weight: bold">in</span> ((<span style="color: #666666">0.1973</span>, <span style="color: #BA2121">"k-"</span>), (<span style="color: #666666">0.0917</span>, <span style="color: #BA2121">"k--"</span>), (<span style="color: #666666">0.7718</span>, <span style="color: #BA2121">"k--"</span>)):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], style, linewidth<span style="color: #666666">=2</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> split <span style="color: #AA22FF; font-weight: bold">in</span> (<span style="color: #666666">0.0458</span>, <span style="color: #666666">0.1298</span>, <span style="color: #666666">0.2873</span>, <span style="color: #666666">0.9040</span>):
|
||||
plt<span style="color: #666666">.</span>plot([split, split], [<span style="color: #666666">-0.2</span>, <span style="color: #666666">1</span>], <span style="color: #BA2121">"k:"</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>text(<span style="color: #666666">0.3</span>, <span style="color: #666666">0.5</span>, <span style="color: #BA2121">"Depth=2"</span>, fontsize<span style="color: #666666">=13</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"max_depth=3"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</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_reg1 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>)
|
||||
tree_reg2 <span style="color: #666666">=</span> DecisionTreeRegressor(random_state<span style="color: #666666">=42</span>, min_samples_leaf<span style="color: #666666">=10</span>)
|
||||
tree_reg1<span style="color: #666666">.</span>fit(X, y)
|
||||
tree_reg2<span style="color: #666666">.</span>fit(X, y)
|
||||
|
||||
x1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">500</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
y_pred1 <span style="color: #666666">=</span> tree_reg1<span style="color: #666666">.</span>predict(x1)
|
||||
y_pred2 <span style="color: #666666">=</span> tree_reg2<span style="color: #666666">.</span>predict(x1)
|
||||
|
||||
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>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred1, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1.1</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"$y$"</span>, fontsize<span style="color: #666666">=18</span>, rotation<span style="color: #666666">=0</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"upper center"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"No restrictions"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plt<span style="color: #666666">.</span>plot(X, y, <span style="color: #BA2121">"b."</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x1, y_pred2, <span style="color: #BA2121">"r.-"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">r"$\hat{y}$"</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">1.1</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"min_samples_leaf={}"</span><span style="color: #666666">.</span>format(tree_reg2<span style="color: #666666">.</span>min_samples_leaf), fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -284,7 +214,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs028.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,18 +177,21 @@ MathJax.Hub.Config({
|
||||
<a name="part0028"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec27" class="anchor">Pros and cons of trees, pros </h2>
|
||||
<h2 id="___sec27" 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">
|
||||
@@ -219,8 +216,6 @@ MathJax.Hub.Config({
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs029.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,19 +177,21 @@ MathJax.Hub.Config({
|
||||
<a name="part0029"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec28" class="anchor">Disadvantages </h2>
|
||||
<h2 id="___sec28" 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>
|
||||
<p>
|
||||
@@ -221,7 +217,6 @@ However, by aggregating many decision trees, using methods like bagging, random
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs030.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,21 +177,31 @@ MathJax.Hub.Config({
|
||||
<a name="part0030"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec29" class="anchor">Bagging </h2>
|
||||
<h2 id="___sec29" class="anchor">More bagging </h2>
|
||||
|
||||
<p>
|
||||
The <b>plain</b> decision trees suffer from high
|
||||
variance. This means that if we split the training data into two parts
|
||||
at random, and fit a decision tree to both halves, the results that we
|
||||
get could be quite different. In contrast, a procedure with low
|
||||
variance will yield similar results if applied repeatedly to distinct
|
||||
data sets; linear regression tends to have low variance, if the ratio
|
||||
of \( n \) to \( p \) is moderately large.
|
||||
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>
|
||||
<b>Bootstrap aggregation</b>, or just <b>bagging</b>, is a
|
||||
general-purpose procedure for reducing the variance of a statistical
|
||||
learning method.
|
||||
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>
|
||||
@@ -222,7 +226,6 @@ learning method.
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs031.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,32 +177,23 @@ MathJax.Hub.Config({
|
||||
<a name="part0031"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec30" class="anchor">More bagging </h2>
|
||||
|
||||
<h2 id="___sec30" class="anchor">Simple example, head or tail </h2>
|
||||
<p>
|
||||
Bagging typically results in improved accuracy
|
||||
over prediction using a single tree. Unfortunately, however, it can be
|
||||
difficult to interpret the resulting model. Recall that one of the
|
||||
advantages of decision trees is the attractive and easily interpreted
|
||||
diagram that results.
|
||||
|
||||
<p>
|
||||
However, when we bag a large number of trees, it is no longer
|
||||
possible to represent the resulting statistical learning procedure
|
||||
using a single tree, and it is no longer clear which variables are
|
||||
most important to the procedure. Thus, bagging improves prediction
|
||||
accuracy at the expense of interpretability. Although the collection
|
||||
of bagged trees is much more difficult to interpret than a single
|
||||
tree, one can obtain an overall summary of the importance of each
|
||||
predictor using the MSE (for bagging regression trees) or the Gini
|
||||
index (for bagging classification trees). In the case of bagging
|
||||
regression trees, we can record the total amount that the MSE is
|
||||
decreased due to splits over a given predictor, averaged over all \( B \) possible
|
||||
trees. A large value indicates an important predictor. Similarly, in
|
||||
the context of bagging classification trees, we can add up the total
|
||||
amount that the Gini index is decreased by splits over a given
|
||||
predictor, averaged over all \( B \) trees.
|
||||
|
||||
<!-- 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"><</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">"k--"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"51%"</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">"k-"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"50%"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Number of coin tosses"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Heads ratio"</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"lower right"</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 -->
|
||||
@@ -231,7 +216,6 @@ predictor, averaged over all \( B \) trees.
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs032.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,22 +177,53 @@ MathJax.Hub.Config({
|
||||
<a name="part0032"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec31" class="anchor">Simple example, head or tail </h2>
|
||||
<h2 id="___sec31" class="anchor">Bagging Example </h2>
|
||||
<p>
|
||||
|
||||
<!-- 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"><</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">"k--"</span>, linewidth<span style="color: #666666">=2</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"51%"</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">"k-"</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"50%"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Number of coin tosses"</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"Heads ratio"</span>)
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">"lower right"</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()
|
||||
<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(solver<span style="color: #666666">=</span><span style="color: #BA2121">"liblinear"</span>, random_state<span style="color: #666666">=42</span>)
|
||||
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=10</span>, random_state<span style="color: #666666">=42</span>)
|
||||
svm_clf <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'hard'</span>)
|
||||
|
||||
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
<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))
|
||||
|
||||
log_clf <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">"liblinear"</span>, random_state<span style="color: #666666">=42</span>)
|
||||
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=10</span>, random_state<span style="color: #666666">=42</span>)
|
||||
svm_clf <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</span>, 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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'soft'</span>)
|
||||
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
<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>
|
||||
@@ -221,7 +246,6 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs033.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,54 +177,47 @@ MathJax.Hub.Config({
|
||||
<a name="part0033"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec32" class="anchor">Bagging Example </h2>
|
||||
<h2 id="___sec32" class="anchor">Random forests </h2>
|
||||
|
||||
<p>
|
||||
Random forests provide an improvement over bagged trees by way of a
|
||||
small tweak that decorrelates the trees.
|
||||
|
||||
<!-- 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
|
||||
<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.
|
||||
|
||||
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>)
|
||||
<p>
|
||||
A fresh sample of \( m \) predictors is
|
||||
taken at each split, and typically we choose
|
||||
|
||||
<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
|
||||
$$
|
||||
m\approx \sqrt{p}.
|
||||
$$
|
||||
|
||||
log_clf <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">"liblinear"</span>, random_state<span style="color: #666666">=42</span>)
|
||||
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=10</span>, random_state<span style="color: #666666">=42</span>)
|
||||
svm_clf <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</span>, random_state<span style="color: #666666">=42</span>)
|
||||
<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.
|
||||
|
||||
voting_clf <span style="color: #666666">=</span> VotingClassifier(
|
||||
estimators<span style="color: #666666">=</span>[(<span style="color: #BA2121">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'hard'</span>)
|
||||
<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.
|
||||
|
||||
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
<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))
|
||||
|
||||
log_clf <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">"liblinear"</span>, random_state<span style="color: #666666">=42</span>)
|
||||
rnd_clf <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=10</span>, random_state<span style="color: #666666">=42</span>)
|
||||
svm_clf <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">"auto"</span>, 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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'soft'</span>)
|
||||
voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
<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>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -251,7 +238,6 @@ voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs034.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,47 +177,21 @@ MathJax.Hub.Config({
|
||||
<a name="part0034"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec33" class="anchor">Random forests </h2>
|
||||
|
||||
<h2 id="___sec33" 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}.
|
||||
$$
|
||||
|
||||
<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">"entropy"</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">'test_score'</span>]
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -243,7 +211,6 @@ this setting.
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs035.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,20 +177,60 @@ MathJax.Hub.Config({
|
||||
<a name="part0035"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec34" class="anchor">A simple scikit-learn example </h2>
|
||||
<h2 id="___sec34" 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">"entropy"</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">'test_score'</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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'hard'</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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'soft'</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>
|
||||
@@ -216,7 +250,6 @@ accuracy <span style="color: #666666">=</span> cross_validate(Random_Forest_mode
|
||||
<li class="active"><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,60 +177,63 @@ MathJax.Hub.Config({
|
||||
<a name="part0036"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec35" class="anchor">Please, not the moons again! </h2>
|
||||
<h2 id="___sec35" 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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'hard'</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">'lr'</span>, log_clf), (<span style="color: #BA2121">'rf'</span>, rnd_clf), (<span style="color: #BA2121">'svc'</span>, svm_clf)],
|
||||
voting<span style="color: #666666">=</span><span style="color: #BA2121">'soft'</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">'#fafab0'</span>,<span style="color: #BA2121">'#9898ff'</span>,<span style="color: #BA2121">'#a0faa0'</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">'#7d7d58'</span>,<span style="color: #BA2121">'#4c4c7f'</span>,<span style="color: #BA2121">'#507d50'</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">"yo"</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">"bs"</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"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r"$x_2$"</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">"Decision Tree"</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">"Decision Trees with Bagging"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -255,7 +252,6 @@ voting_clf<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,65 +177,27 @@ MathJax.Hub.Config({
|
||||
<a name="part0037"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec36" class="anchor">Bagging examples </h2>
|
||||
|
||||
<h2 id="___sec36" 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">"random"</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)
|
||||
<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>
|
||||
|
||||
<!-- 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">'#fafab0'</span>,<span style="color: #BA2121">'#9898ff'</span>,<span style="color: #BA2121">'#a0faa0'</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">'#7d7d58'</span>,<span style="color: #BA2121">'#4c4c7f'</span>,<span style="color: #BA2121">'#507d50'</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">"yo"</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">"bs"</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"$x_1$"</span>, fontsize<span style="color: #666666">=18</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r"$x_2$"</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">"Decision Tree"</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">"Decision Trees with Bagging"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -257,8 +213,6 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._DecisionTrees-bs035.html">36</a></li>
|
||||
<li><a href="._DecisionTrees-bs036.html">37</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs038.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
|
||||
@@ -47,52 +47,47 @@ Automatically generated HTML file from DocOnce source
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -132,42 +127,41 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Regeression Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Then random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs017.html#___sec16" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">Simple Python Code to read in Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Simple example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Bagging Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">A simple scikit-learn example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">Please, not the moons again!</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Bagging examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Then random forests</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -226,7 +220,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-bs038.html">39</a></li>
|
||||
<li><a href="._DecisionTrees-bs037.html">38</a></li>
|
||||
<li><a href="._DecisionTrees-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -194,22 +194,11 @@ given some assumptions, make predictions about the target feature value
|
||||
|
||||
<section>
|
||||
<h2 id="___sec1">A typical Decision Tree with its pertinent Jargon, Classification Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure here we present a decision tree obtained from a classification problem
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec2">A typical Decision Tree with its pertinent Jargon, Regeression Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure we present a decision tree obtained from a simple regression problem
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec3">General Features </h2>
|
||||
<h2 id="___sec2">General Features </h2>
|
||||
|
||||
<p>
|
||||
The overarching approach to decision trees is a top-down approach.
|
||||
@@ -228,7 +217,7 @@ node.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec4">How do we set it up? </h2>
|
||||
<h2 id="___sec3">How do we set it up? </h2>
|
||||
|
||||
<p>
|
||||
In simplified terms, the process of training a decision tree and
|
||||
@@ -247,7 +236,7 @@ Then we are essentially done!
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec5">Decision trees and Regression </h2>
|
||||
<h2 id="___sec4">Decision trees and Regression </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -344,7 +333,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec6">Building a tree, regression </h2>
|
||||
<h2 id="___sec5">Building a tree, regression </h2>
|
||||
|
||||
<p>
|
||||
There are mainly two steps
|
||||
@@ -376,7 +365,7 @@ within box \( j \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec7">A top-down approach, recursive binary splitting </h2>
|
||||
<h2 id="___sec6">A top-down approach, recursive binary splitting </h2>
|
||||
|
||||
<p>
|
||||
Unfortunately, it is computationally infeasible to consider every
|
||||
@@ -395,7 +384,7 @@ better tree in some future step.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec8">Making a tree </h2>
|
||||
<h2 id="___sec7">Making a tree </h2>
|
||||
|
||||
<p>
|
||||
In order to implement the recursive binary splitting we start by selecting
|
||||
@@ -452,7 +441,7 @@ region contains more than five observations.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec9">Pruning the tree </h2>
|
||||
<h2 id="___sec8">Pruning the tree </h2>
|
||||
|
||||
<p>
|
||||
The above procedure is rather straightforward, but leads often to
|
||||
@@ -471,7 +460,7 @@ parameter \( \alpha \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec10">Cost complexity pruning </h2>
|
||||
<h2 id="___sec9">Cost complexity pruning </h2>
|
||||
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
||||
<p> <br>
|
||||
$$
|
||||
@@ -504,7 +493,7 @@ subtree corresponding to \( \alpha \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec11">Schematic Regression Procedure </h2>
|
||||
<h2 id="___sec10">Schematic Regression Procedure </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -529,7 +518,7 @@ subtree corresponding to \( \alpha \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec12">A Classification Tree </h2>
|
||||
<h2 id="___sec11">A Classification Tree </h2>
|
||||
|
||||
<p>
|
||||
A classification tree is very similar to a regression tree, except
|
||||
@@ -548,7 +537,7 @@ fall into that region.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec13">Growing a classification tree </h2>
|
||||
<h2 id="___sec12">Growing a classification tree </h2>
|
||||
|
||||
<p>
|
||||
The task of growing a
|
||||
@@ -572,7 +561,7 @@ than is the classification error rate.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec14">Classification tree, how to split nodes </h2>
|
||||
<h2 id="___sec13">Classification tree, how to split nodes </h2>
|
||||
|
||||
<p>
|
||||
If our targets are the outcome of a classification process that takes
|
||||
@@ -627,7 +616,7 @@ $$
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec15">Visualizing the Tree, Classification </h2>
|
||||
<h2 id="___sec14">Visualizing the Tree, Classification </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -669,7 +658,7 @@ os.system(cmd)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec16">Visualizing the Tree, The Moons </h2>
|
||||
<h2 id="___sec15">Visualizing the Tree, The Moons </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -702,7 +691,7 @@ os.system(cmd)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec17">Computing the Gini index </h2>
|
||||
<h2 id="___sec16">Computing the Gini index </h2>
|
||||
|
||||
<p>
|
||||
The example we will look at is a classical one in many Machine
|
||||
@@ -743,7 +732,7 @@ The table here summarizes the various attributes and
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec18">Simple Python Code to read in Data </h2>
|
||||
<h2 id="___sec17">Simple Python Code to read in Data </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -806,7 +795,7 @@ display(y)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec19">Computing the Gini Factor </h2>
|
||||
<h2 id="___sec18">Computing the Gini Factor </h2>
|
||||
|
||||
<p>
|
||||
The above functions (gini, entropy and misclassification error) are
|
||||
@@ -885,7 +874,7 @@ split = get_split(dataset)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec20">Entropy and the ID3 algorithm </h2>
|
||||
<h2 id="___sec19">Entropy and the ID3 algorithm </h2>
|
||||
|
||||
<p>
|
||||
ID3, learns decision trees by constructing
|
||||
@@ -922,216 +911,204 @@ attributes at each step while growing the tree.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec21">Implementing the ID3 Algorithm </h2>
|
||||
<h2 id="___sec20">Implementing the ID3 Algorithm </h2>
|
||||
|
||||
<p>
|
||||
import re
|
||||
import math
|
||||
from collections import deque
|
||||
|
||||
<p>
|
||||
<!-- x is examples in training set -->
|
||||
<!-- y is set of attributes -->
|
||||
<!-- label is target attributes -->
|
||||
<!-- Node is a class which has properties values, childs, and next -->
|
||||
<!-- root is top node in the decision tree -->
|
||||
<!-- 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">re</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">math</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">collections</span> <span style="color: #8B008B; font-weight: bold">import</span> deque
|
||||
|
||||
<p>
|
||||
class Node(object):
|
||||
def __init__(self):
|
||||
self.value = None
|
||||
self.next = None
|
||||
self.childs = None
|
||||
<span style="color: #228B22"># x is examples in training set</span>
|
||||
<span style="color: #228B22"># y is set of targets</span>
|
||||
<span style="color: #228B22"># label is target attributes</span>
|
||||
<span style="color: #228B22"># Node is a class which has properties values, childs, and next</span>
|
||||
<span style="color: #228B22"># root is top node in the decision tree</span>
|
||||
|
||||
<p>
|
||||
<!-- Simple class of Decision Tree -->
|
||||
<!-- Aimed for who want to learn Decision Tree, so it is not optimized -->
|
||||
class DecisionTree(object):
|
||||
def __init__(self, sample, attributes, labels):
|
||||
self.sample = sample
|
||||
self.attributes = attributes
|
||||
self.labels = labels
|
||||
self.labelCodes = None
|
||||
self.labelCodesCount = None
|
||||
self.initLabelCodes()
|
||||
# print(self.labelCodes)
|
||||
self.root = None
|
||||
self.entropy = self.getEntropy([x for x in range(len(self.labels))])
|
||||
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">Node</span>(<span style="color: #658b00">object</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #658b00">self</span>.value = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.next = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.childs = <span style="color: #658b00">None</span>
|
||||
|
||||
<p>
|
||||
def initLabelCodes(self):
|
||||
self.labelCodes = []
|
||||
self.labelCodesCount = []
|
||||
for l in self.labels:
|
||||
if l not in self.labelCodes:
|
||||
self.labelCodes.append(l)
|
||||
self.labelCodesCount.append(0)
|
||||
self.labelCodesCount[self.labelCodes.index(l)] += 1
|
||||
<span style="color: #228B22"># Simple class of Decision Tree</span>
|
||||
<span style="color: #228B22"># Aimed for who want to learn Decision Tree, so it is not optimized</span>
|
||||
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">DecisionTree</span>(<span style="color: #658b00">object</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>, sample, attributes, labels):
|
||||
<span style="color: #658b00">self</span>.sample = sample
|
||||
<span style="color: #658b00">self</span>.attributes = attributes
|
||||
<span style="color: #658b00">self</span>.labels = labels
|
||||
<span style="color: #658b00">self</span>.labelCodes = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.labelCodesCount = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.initLabelCodes()
|
||||
<span style="color: #228B22"># print(self.labelCodes)</span>
|
||||
<span style="color: #658b00">self</span>.root = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.entropy = <span style="color: #658b00">self</span>.getEntropy([x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labels))])
|
||||
|
||||
<p>
|
||||
def getLabelCodeId(self, sampleId):
|
||||
return self.labelCodes.index(self.labels[sampleId])
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">initLabelCodes</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #658b00">self</span>.labelCodes = []
|
||||
<span style="color: #658b00">self</span>.labelCodesCount = []
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> l <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.labels:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> l <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.labelCodes:
|
||||
<span style="color: #658b00">self</span>.labelCodes.append(l)
|
||||
<span style="color: #658b00">self</span>.labelCodesCount.append(<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #658b00">self</span>.labelCodesCount[<span style="color: #658b00">self</span>.labelCodes.index(l)] += <span style="color: #B452CD">1</span>
|
||||
|
||||
<p>
|
||||
def getAttributeValues(self, sampleIds, attributeId):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getLabelCodeId</span>(<span style="color: #658b00">self</span>, sampleId):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labelCodes.index(<span style="color: #658b00">self</span>.labels[sampleId])
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getAttributeValues</span>(<span style="color: #658b00">self</span>, sampleIds, attributeId):
|
||||
vals = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in vals:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
val = <span style="color: #658b00">self</span>.sample[sid][attributeId]
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> val <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> vals:
|
||||
vals.append(val)
|
||||
# print(vals)
|
||||
return vals
|
||||
<span style="color: #228B22"># print(vals)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> vals
|
||||
|
||||
<p>
|
||||
def getEntropy(self, sampleIds):
|
||||
entropy = 0
|
||||
labelCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCount[self.getLabelCodeId(sid)] += 1
|
||||
# print("-ge", labelCount)
|
||||
for lv in labelCount:
|
||||
# print(lv)
|
||||
if lv != 0:
|
||||
entropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)
|
||||
else:
|
||||
entropy += 0
|
||||
return entropy
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getEntropy</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
entropy = <span style="color: #B452CD">0</span>
|
||||
labelCount = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labelCodes)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
labelCount[<span style="color: #658b00">self</span>.getLabelCodeId(sid)] += <span style="color: #B452CD">1</span>
|
||||
<span style="color: #228B22"># print("-ge", labelCount)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> lv <span style="color: #8B008B">in</span> labelCount:
|
||||
<span style="color: #228B22"># print(lv)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> lv != <span style="color: #B452CD">0</span>:
|
||||
entropy += -lv/<span style="color: #658b00">len</span>(sampleIds) * math.log(lv/<span style="color: #658b00">len</span>(sampleIds), <span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">else</span>:
|
||||
entropy += <span style="color: #B452CD">0</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> entropy
|
||||
|
||||
<p>
|
||||
def getDominantLabel(self, sampleIds):
|
||||
labelCodesCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCodesCount[self.labelCodes.index(self.labels[sid])] += 1
|
||||
return self.labelCodes[labelCodesCount.index(max(labelCodesCount))]
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getDominantLabel</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
labelCodesCount = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labelCodes)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
labelCodesCount[<span style="color: #658b00">self</span>.labelCodes.index(<span style="color: #658b00">self</span>.labels[sid])] += <span style="color: #B452CD">1</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labelCodes[labelCodesCount.index(<span style="color: #658b00">max</span>(labelCodesCount))]
|
||||
|
||||
<p>
|
||||
def getInformationGain(self, sampleIds, attributeId):
|
||||
gain = self.getEntropy(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getInformationGain</span>(<span style="color: #658b00">self</span>, sampleIds, attributeId):
|
||||
gain = <span style="color: #658b00">self</span>.getEntropy(sampleIds)
|
||||
attributeVals = []
|
||||
attributeValsCount = []
|
||||
attributeValsIds = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in attributeVals:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
val = <span style="color: #658b00">self</span>.sample[sid][attributeId]
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> val <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> attributeVals:
|
||||
attributeVals.append(val)
|
||||
attributeValsCount.append(0)
|
||||
attributeValsCount.append(<span style="color: #B452CD">0</span>)
|
||||
attributeValsIds.append([])
|
||||
vid = attributeVals.index(val)
|
||||
attributeValsCount[vid] += 1
|
||||
attributeValsCount[vid] += <span style="color: #B452CD">1</span>
|
||||
attributeValsIds[vid].append(sid)
|
||||
# print("-gig", self.attributes[attributeId])
|
||||
for vc, vids in zip(attributeValsCount, attributeValsIds):
|
||||
# print("-gig", vids)
|
||||
gain -= vc/len(sampleIds) * self.getEntropy(vids)
|
||||
return gain
|
||||
<span style="color: #228B22"># print("-gig", self.attributes[attributeId])</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> vc, vids <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(attributeValsCount, attributeValsIds):
|
||||
<span style="color: #228B22"># print("-gig", vids)</span>
|
||||
gain -= vc/<span style="color: #658b00">len</span>(sampleIds) * <span style="color: #658b00">self</span>.getEntropy(vids)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> gain
|
||||
|
||||
<p>
|
||||
def getAttributeMaxInformationGain(self, sampleIds, attributeIds):
|
||||
attributesEntropy = [0] * len(attributeIds)
|
||||
for i, attId in zip(range(len(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = self.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
|
||||
return self.attributes[maxId], maxId
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getAttributeMaxInformationGain</span>(<span style="color: #658b00">self</span>, sampleIds, attributeIds):
|
||||
attributesEntropy = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(attributeIds)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i, attId <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(<span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = <span style="color: #658b00">self</span>.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(<span style="color: #658b00">max</span>(attributesEntropy))]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.attributes[maxId], maxId
|
||||
|
||||
<p>
|
||||
def isSingleLabeled(self, sampleIds):
|
||||
label = self.labels[sampleIds[0]]
|
||||
for sid in sampleIds:
|
||||
if self.labels[sid] != label:
|
||||
return False
|
||||
return True
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">isSingleLabeled</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
label = <span style="color: #658b00">self</span>.labels[sampleIds[<span style="color: #B452CD">0</span>]]
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.labels[sid] != label:
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">False</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">True</span>
|
||||
|
||||
<p>
|
||||
def getLabel(self, sampleId):
|
||||
return self.labels[sampleId]
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getLabel</span>(<span style="color: #658b00">self</span>, sampleId):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labels[sampleId]
|
||||
|
||||
<p>
|
||||
def id3(self):
|
||||
sampleIds = [x for x in range(len(self.sample))]
|
||||
attributeIds = [x for x in range(len(self.attributes))]
|
||||
self.root = self.id3Recv(sampleIds, attributeIds, self.root)
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">id3</span>(<span style="color: #658b00">self</span>):
|
||||
sampleIds = [x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.sample))]
|
||||
attributeIds = [x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.attributes))]
|
||||
<span style="color: #658b00">self</span>.root = <span style="color: #658b00">self</span>.id3Recv(sampleIds, attributeIds, <span style="color: #658b00">self</span>.root)
|
||||
|
||||
<p>
|
||||
def id3Recv(self, sampleIds, attributeIds, root):
|
||||
root = Node() # Initialize current root
|
||||
if self.isSingleLabeled(sampleIds):
|
||||
root.value = self.labels[sampleIds[0]]
|
||||
return root
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) == 0:
|
||||
root.value = self.getDominantLabel(sampleIds)
|
||||
return root
|
||||
bestAttrName, bestAttrId = self.getAttributeMaxInformationGain(
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">id3Recv</span>(<span style="color: #658b00">self</span>, sampleIds, attributeIds, root):
|
||||
root = Node() <span style="color: #228B22"># Initialize current root</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.isSingleLabeled(sampleIds):
|
||||
root.value = <span style="color: #658b00">self</span>.labels[sampleIds[<span style="color: #B452CD">0</span>]]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
<span style="color: #228B22"># print(attributeIds)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(attributeIds) == <span style="color: #B452CD">0</span>:
|
||||
root.value = <span style="color: #658b00">self</span>.getDominantLabel(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
bestAttrName, bestAttrId = <span style="color: #658b00">self</span>.getAttributeMaxInformationGain(
|
||||
sampleIds, attributeIds)
|
||||
# print(bestAttrName)
|
||||
<span style="color: #228B22"># print(bestAttrName)</span>
|
||||
root.value = bestAttrName
|
||||
root.childs = [] # Create list of children
|
||||
for value in self.getAttributeValues(sampleIds, bestAttrId):
|
||||
# print(value)
|
||||
root.childs = [] <span style="color: #228B22"># Create list of children</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> value <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.getAttributeValues(sampleIds, bestAttrId):
|
||||
<span style="color: #228B22"># print(value)</span>
|
||||
child = Node()
|
||||
child.value = value
|
||||
root.childs.append(child) # Append new child node to current
|
||||
# root
|
||||
root.childs.append(child) <span style="color: #228B22"># Append new child node to current</span>
|
||||
<span style="color: #228B22"># root</span>
|
||||
childSampleIds = []
|
||||
for sid in sampleIds:
|
||||
if self.sample[sid][bestAttrId] == value:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.sample[sid][bestAttrId] == value:
|
||||
childSampleIds.append(sid)
|
||||
if len(childSampleIds) == 0:
|
||||
child.next = self.getDominantLabel(sampleIds)
|
||||
else:
|
||||
# print(bestAttrName, bestAttrId)
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) > 0 and bestAttrId in attributeIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(childSampleIds) == <span style="color: #B452CD">0</span>:
|
||||
child.next = <span style="color: #658b00">self</span>.getDominantLabel(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">else</span>:
|
||||
<span style="color: #228B22"># print(bestAttrName, bestAttrId)</span>
|
||||
<span style="color: #228B22"># print(attributeIds)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(attributeIds) > <span style="color: #B452CD">0</span> <span style="color: #8B008B">and</span> bestAttrId <span style="color: #8B008B">in</span> attributeIds:
|
||||
toRemove = attributeIds.index(bestAttrId)
|
||||
attributeIds.pop(toRemove)
|
||||
child.next = self.id3Recv(
|
||||
child.next = <span style="color: #658b00">self</span>.id3Recv(
|
||||
childSampleIds, attributeIds, child.next)
|
||||
return root
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
|
||||
<p>
|
||||
def printTree(self):
|
||||
if self.root:
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">printTree</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.root:
|
||||
roots = deque()
|
||||
roots.append(self.root)
|
||||
while len(roots) > 0:
|
||||
roots.append(<span style="color: #658b00">self</span>.root)
|
||||
<span style="color: #8B008B; font-weight: bold">while</span> <span style="color: #658b00">len</span>(roots) > <span style="color: #B452CD">0</span>:
|
||||
root = roots.popleft()
|
||||
print(root.value)
|
||||
if root.childs:
|
||||
for child in root.childs:
|
||||
print('({})'.format(child.value))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(root.value)
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> root.childs:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> child <span style="color: #8B008B">in</span> root.childs:
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'({})'</span>.format(child.value))
|
||||
roots.append(child.next)
|
||||
elif root.next:
|
||||
print(root.next)
|
||||
<span style="color: #8B008B; font-weight: bold">elif</span> root.next:
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(root.next)
|
||||
|
||||
<p>
|
||||
def test():
|
||||
f = open('DataFiles/rideclass.csv')
|
||||
attributes = f.readline().split(',')
|
||||
attributes = attributes[1:len(attributes)-1]
|
||||
print(attributes)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">test</span>():
|
||||
f = <span style="color: #658b00">open</span>(<span style="color: #CD5555">'DataFiles/rideclass.csv'</span>)
|
||||
attributes = f.readline().split(<span style="color: #CD5555">','</span>)
|
||||
attributes = attributes[<span style="color: #B452CD">1</span>:<span style="color: #658b00">len</span>(attributes)-<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(attributes)
|
||||
sample = f.readlines()
|
||||
f.close()
|
||||
for i in range(len(sample)):
|
||||
sample[i] = re.sub('\d+,', '', sample[i])
|
||||
sample[i] = sample[i].strip().split(',')
|
||||
<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>(sample)):
|
||||
sample[i] = re.sub(<span style="color: #CD5555">'\d+,'</span>, <span style="color: #CD5555">''</span>, sample[i])
|
||||
sample[i] = sample[i].strip().split(<span style="color: #CD5555">','</span>)
|
||||
labels = []
|
||||
for s in sample:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> s <span style="color: #8B008B">in</span> sample:
|
||||
labels.append(s.pop())
|
||||
# print(sample)
|
||||
# print(labels)
|
||||
<span style="color: #228B22"># print(sample)</span>
|
||||
<span style="color: #228B22"># print(labels)</span>
|
||||
decisionTree = DecisionTree(sample, attributes, labels)
|
||||
print("System entropy {}".format(decisionTree.entropy))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"System entropy {}"</span>.format(decisionTree.entropy))
|
||||
decisionTree.id3()
|
||||
decisionTree.printTree()
|
||||
|
||||
<p>
|
||||
if __name__ == '__main__':
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #00688B">__name__</span> == <span style="color: #CD5555">'__main__'</span>:
|
||||
test()
|
||||
</pre></div>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec22">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<h2 id="___sec21">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1181,7 +1158,7 @@ deep_tree_clf.fit(X_train_scaled, y_train)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec23">Another example, the moons again </h2>
|
||||
<h2 id="___sec22">Another example, the moons again </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1254,7 +1231,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec24">Playing around with regions </h2>
|
||||
<h2 id="___sec23">Playing around with regions </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1283,7 +1260,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec25">Regression trees </h2>
|
||||
<h2 id="___sec24">Regression trees </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1306,7 +1283,7 @@ tree_reg.fit(X, y)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec26">Final regressor code </h2>
|
||||
<h2 id="___sec25">Final regressor code </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1385,7 +1362,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec27">Pros and cons of trees, pros </h2>
|
||||
<h2 id="___sec26">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>
|
||||
@@ -1400,7 +1377,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec28">Disadvantages </h2>
|
||||
<h2 id="___sec27">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>
|
||||
@@ -1418,7 +1395,7 @@ However, by aggregating many decision trees, using methods like bagging, random
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec29">Bagging </h2>
|
||||
<h2 id="___sec28">Bagging </h2>
|
||||
|
||||
<p>
|
||||
The <b>plain</b> decision trees suffer from high
|
||||
@@ -1437,7 +1414,7 @@ learning method.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec30">More bagging </h2>
|
||||
<h2 id="___sec29">More bagging </h2>
|
||||
|
||||
<p>
|
||||
Bagging typically results in improved accuracy
|
||||
@@ -1466,7 +1443,7 @@ predictor, averaged over all \( B \) trees.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec31">Simple example, head or tail </h2>
|
||||
<h2 id="___sec30">Simple example, head or tail </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1487,7 +1464,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec32">Bagging Example </h2>
|
||||
<h2 id="___sec31">Bagging Example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1539,7 +1516,7 @@ voting_clf.fit(X_train, y_train)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec33">Random forests </h2>
|
||||
<h2 id="___sec32">Random forests </h2>
|
||||
|
||||
<p>
|
||||
Random forests provide an improvement over bagged trees by way of a
|
||||
@@ -1585,7 +1562,7 @@ this setting.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec34">A simple scikit-learn example </h2>
|
||||
<h2 id="___sec33">A simple scikit-learn example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1604,7 +1581,7 @@ accuracy = cross_validate(Random_Forest_model,X,Y,cv=<span style="color: #B452CD
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec35">Please, not the moons again! </h2>
|
||||
<h2 id="___sec34">Please, not the moons again! </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1663,7 +1640,7 @@ voting_clf.fit(X_train, y_train)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec36">Bagging examples </h2>
|
||||
<h2 id="___sec35">Bagging examples </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -1725,7 +1702,7 @@ plt.show()
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec37">Then random forests </h2>
|
||||
<h2 id="___sec36">Then random forests </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
|
||||
@@ -67,52 +67,47 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -195,21 +190,10 @@ given some assumptions, make predictions about the target feature value
|
||||
|
||||
<h2 id="___sec1">A typical Decision Tree with its pertinent Jargon, Classification Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure here we present a decision tree obtained from a classification problem
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">A typical Decision Tree with its pertinent Jargon, Regeression Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure we present a decision tree obtained from a simple regression problem
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">General Features </h2>
|
||||
<h2 id="___sec2">General Features </h2>
|
||||
|
||||
<p>
|
||||
The overarching approach to decision trees is a top-down approach.
|
||||
@@ -227,7 +211,7 @@ node.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">How do we set it up? </h2>
|
||||
<h2 id="___sec3">How do we set it up? </h2>
|
||||
|
||||
<p>
|
||||
In simplified terms, the process of training a decision tree and
|
||||
@@ -245,7 +229,7 @@ Then we are essentially done!
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Decision trees and Regression </h2>
|
||||
<h2 id="___sec4">Decision trees and Regression </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -341,7 +325,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Building a tree, regression </h2>
|
||||
<h2 id="___sec5">Building a tree, regression </h2>
|
||||
|
||||
<p>
|
||||
There are mainly two steps
|
||||
@@ -369,7 +353,7 @@ within box \( j \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">A top-down approach, recursive binary splitting </h2>
|
||||
<h2 id="___sec6">A top-down approach, recursive binary splitting </h2>
|
||||
|
||||
<p>
|
||||
Unfortunately, it is computationally infeasible to consider every
|
||||
@@ -388,7 +372,7 @@ better tree in some future step.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Making a tree </h2>
|
||||
<h2 id="___sec7">Making a tree </h2>
|
||||
|
||||
<p>
|
||||
In order to implement the recursive binary splitting we start by selecting
|
||||
@@ -439,7 +423,7 @@ region contains more than five observations.
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec9">Pruning the tree </h2>
|
||||
<h2 id="___sec8">Pruning the tree </h2>
|
||||
|
||||
<p>
|
||||
The above procedure is rather straightforward, but leads often to
|
||||
@@ -458,7 +442,7 @@ parameter \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Cost complexity pruning </h2>
|
||||
<h2 id="___sec9">Cost complexity pruning </h2>
|
||||
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
||||
$$
|
||||
\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T},
|
||||
@@ -489,7 +473,7 @@ subtree corresponding to \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Schematic Regression Procedure </h2>
|
||||
<h2 id="___sec10">Schematic Regression Procedure </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -515,7 +499,7 @@ subtree corresponding to \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec12">A Classification Tree </h2>
|
||||
<h2 id="___sec11">A Classification Tree </h2>
|
||||
|
||||
<p>
|
||||
A classification tree is very similar to a regression tree, except
|
||||
@@ -534,7 +518,7 @@ fall into that region.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec13">Growing a classification tree </h2>
|
||||
<h2 id="___sec12">Growing a classification tree </h2>
|
||||
|
||||
<p>
|
||||
The task of growing a
|
||||
@@ -558,7 +542,7 @@ than is the classification error rate.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec14">Classification tree, how to split nodes </h2>
|
||||
<h2 id="___sec13">Classification tree, how to split nodes </h2>
|
||||
|
||||
<p>
|
||||
If our targets are the outcome of a classification process that takes
|
||||
@@ -608,7 +592,7 @@ $$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Visualizing the Tree, Classification </h2>
|
||||
<h2 id="___sec14">Visualizing the Tree, Classification </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -649,7 +633,7 @@ os.system(cmd)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Visualizing the Tree, The Moons </h2>
|
||||
<h2 id="___sec15">Visualizing the Tree, The Moons </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -681,7 +665,7 @@ os.system(cmd)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">Computing the Gini index </h2>
|
||||
<h2 id="___sec16">Computing the Gini index </h2>
|
||||
|
||||
<p>
|
||||
The example we will look at is a classical one in many Machine
|
||||
@@ -721,7 +705,7 @@ The table here summarizes the various attributes and
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">Simple Python Code to read in Data </h2>
|
||||
<h2 id="___sec17">Simple Python Code to read in Data </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -783,7 +767,7 @@ display(y)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">Computing the Gini Factor </h2>
|
||||
<h2 id="___sec18">Computing the Gini Factor </h2>
|
||||
|
||||
<p>
|
||||
The above functions (gini, entropy and misclassification error) are
|
||||
@@ -861,7 +845,7 @@ split = get_split(dataset)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Entropy and the ID3 algorithm </h2>
|
||||
<h2 id="___sec19">Entropy and the ID3 algorithm </h2>
|
||||
|
||||
<p>
|
||||
ID3, learns decision trees by constructing
|
||||
@@ -897,216 +881,203 @@ attributes at each step while growing the tree.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec21">Implementing the ID3 Algorithm </h2>
|
||||
<h2 id="___sec20">Implementing the ID3 Algorithm </h2>
|
||||
|
||||
<p>
|
||||
import re
|
||||
import math
|
||||
from collections import deque
|
||||
|
||||
<p>
|
||||
<!-- x is examples in training set -->
|
||||
<!-- y is set of attributes -->
|
||||
<!-- label is target attributes -->
|
||||
<!-- Node is a class which has properties values, childs, and next -->
|
||||
<!-- root is top node in the decision tree -->
|
||||
<!-- 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">re</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">math</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">collections</span> <span style="color: #8B008B; font-weight: bold">import</span> deque
|
||||
|
||||
<p>
|
||||
class Node(object):
|
||||
def __init__(self):
|
||||
self.value = None
|
||||
self.next = None
|
||||
self.childs = None
|
||||
<span style="color: #228B22"># x is examples in training set</span>
|
||||
<span style="color: #228B22"># y is set of targets</span>
|
||||
<span style="color: #228B22"># label is target attributes</span>
|
||||
<span style="color: #228B22"># Node is a class which has properties values, childs, and next</span>
|
||||
<span style="color: #228B22"># root is top node in the decision tree</span>
|
||||
|
||||
<p>
|
||||
<!-- Simple class of Decision Tree -->
|
||||
<!-- Aimed for who want to learn Decision Tree, so it is not optimized -->
|
||||
class DecisionTree(object):
|
||||
def __init__(self, sample, attributes, labels):
|
||||
self.sample = sample
|
||||
self.attributes = attributes
|
||||
self.labels = labels
|
||||
self.labelCodes = None
|
||||
self.labelCodesCount = None
|
||||
self.initLabelCodes()
|
||||
# print(self.labelCodes)
|
||||
self.root = None
|
||||
self.entropy = self.getEntropy([x for x in range(len(self.labels))])
|
||||
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">Node</span>(<span style="color: #658b00">object</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #658b00">self</span>.value = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.next = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.childs = <span style="color: #658b00">None</span>
|
||||
|
||||
<p>
|
||||
def initLabelCodes(self):
|
||||
self.labelCodes = []
|
||||
self.labelCodesCount = []
|
||||
for l in self.labels:
|
||||
if l not in self.labelCodes:
|
||||
self.labelCodes.append(l)
|
||||
self.labelCodesCount.append(0)
|
||||
self.labelCodesCount[self.labelCodes.index(l)] += 1
|
||||
<span style="color: #228B22"># Simple class of Decision Tree</span>
|
||||
<span style="color: #228B22"># Aimed for who want to learn Decision Tree, so it is not optimized</span>
|
||||
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">DecisionTree</span>(<span style="color: #658b00">object</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>, sample, attributes, labels):
|
||||
<span style="color: #658b00">self</span>.sample = sample
|
||||
<span style="color: #658b00">self</span>.attributes = attributes
|
||||
<span style="color: #658b00">self</span>.labels = labels
|
||||
<span style="color: #658b00">self</span>.labelCodes = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.labelCodesCount = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.initLabelCodes()
|
||||
<span style="color: #228B22"># print(self.labelCodes)</span>
|
||||
<span style="color: #658b00">self</span>.root = <span style="color: #658b00">None</span>
|
||||
<span style="color: #658b00">self</span>.entropy = <span style="color: #658b00">self</span>.getEntropy([x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labels))])
|
||||
|
||||
<p>
|
||||
def getLabelCodeId(self, sampleId):
|
||||
return self.labelCodes.index(self.labels[sampleId])
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">initLabelCodes</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #658b00">self</span>.labelCodes = []
|
||||
<span style="color: #658b00">self</span>.labelCodesCount = []
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> l <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.labels:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> l <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.labelCodes:
|
||||
<span style="color: #658b00">self</span>.labelCodes.append(l)
|
||||
<span style="color: #658b00">self</span>.labelCodesCount.append(<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #658b00">self</span>.labelCodesCount[<span style="color: #658b00">self</span>.labelCodes.index(l)] += <span style="color: #B452CD">1</span>
|
||||
|
||||
<p>
|
||||
def getAttributeValues(self, sampleIds, attributeId):
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getLabelCodeId</span>(<span style="color: #658b00">self</span>, sampleId):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labelCodes.index(<span style="color: #658b00">self</span>.labels[sampleId])
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getAttributeValues</span>(<span style="color: #658b00">self</span>, sampleIds, attributeId):
|
||||
vals = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in vals:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
val = <span style="color: #658b00">self</span>.sample[sid][attributeId]
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> val <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> vals:
|
||||
vals.append(val)
|
||||
# print(vals)
|
||||
return vals
|
||||
<span style="color: #228B22"># print(vals)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> vals
|
||||
|
||||
<p>
|
||||
def getEntropy(self, sampleIds):
|
||||
entropy = 0
|
||||
labelCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCount[self.getLabelCodeId(sid)] += 1
|
||||
# print("-ge", labelCount)
|
||||
for lv in labelCount:
|
||||
# print(lv)
|
||||
if lv != 0:
|
||||
entropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)
|
||||
else:
|
||||
entropy += 0
|
||||
return entropy
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getEntropy</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
entropy = <span style="color: #B452CD">0</span>
|
||||
labelCount = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labelCodes)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
labelCount[<span style="color: #658b00">self</span>.getLabelCodeId(sid)] += <span style="color: #B452CD">1</span>
|
||||
<span style="color: #228B22"># print("-ge", labelCount)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> lv <span style="color: #8B008B">in</span> labelCount:
|
||||
<span style="color: #228B22"># print(lv)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> lv != <span style="color: #B452CD">0</span>:
|
||||
entropy += -lv/<span style="color: #658b00">len</span>(sampleIds) * math.log(lv/<span style="color: #658b00">len</span>(sampleIds), <span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">else</span>:
|
||||
entropy += <span style="color: #B452CD">0</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> entropy
|
||||
|
||||
<p>
|
||||
def getDominantLabel(self, sampleIds):
|
||||
labelCodesCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCodesCount[self.labelCodes.index(self.labels[sid])] += 1
|
||||
return self.labelCodes[labelCodesCount.index(max(labelCodesCount))]
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getDominantLabel</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
labelCodesCount = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.labelCodes)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
labelCodesCount[<span style="color: #658b00">self</span>.labelCodes.index(<span style="color: #658b00">self</span>.labels[sid])] += <span style="color: #B452CD">1</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labelCodes[labelCodesCount.index(<span style="color: #658b00">max</span>(labelCodesCount))]
|
||||
|
||||
<p>
|
||||
def getInformationGain(self, sampleIds, attributeId):
|
||||
gain = self.getEntropy(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getInformationGain</span>(<span style="color: #658b00">self</span>, sampleIds, attributeId):
|
||||
gain = <span style="color: #658b00">self</span>.getEntropy(sampleIds)
|
||||
attributeVals = []
|
||||
attributeValsCount = []
|
||||
attributeValsIds = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in attributeVals:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
val = <span style="color: #658b00">self</span>.sample[sid][attributeId]
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> val <span style="color: #8B008B">not</span> <span style="color: #8B008B">in</span> attributeVals:
|
||||
attributeVals.append(val)
|
||||
attributeValsCount.append(0)
|
||||
attributeValsCount.append(<span style="color: #B452CD">0</span>)
|
||||
attributeValsIds.append([])
|
||||
vid = attributeVals.index(val)
|
||||
attributeValsCount[vid] += 1
|
||||
attributeValsCount[vid] += <span style="color: #B452CD">1</span>
|
||||
attributeValsIds[vid].append(sid)
|
||||
# print("-gig", self.attributes[attributeId])
|
||||
for vc, vids in zip(attributeValsCount, attributeValsIds):
|
||||
# print("-gig", vids)
|
||||
gain -= vc/len(sampleIds) * self.getEntropy(vids)
|
||||
return gain
|
||||
<span style="color: #228B22"># print("-gig", self.attributes[attributeId])</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> vc, vids <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(attributeValsCount, attributeValsIds):
|
||||
<span style="color: #228B22"># print("-gig", vids)</span>
|
||||
gain -= vc/<span style="color: #658b00">len</span>(sampleIds) * <span style="color: #658b00">self</span>.getEntropy(vids)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> gain
|
||||
|
||||
<p>
|
||||
def getAttributeMaxInformationGain(self, sampleIds, attributeIds):
|
||||
attributesEntropy = [0] * len(attributeIds)
|
||||
for i, attId in zip(range(len(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = self.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
|
||||
return self.attributes[maxId], maxId
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getAttributeMaxInformationGain</span>(<span style="color: #658b00">self</span>, sampleIds, attributeIds):
|
||||
attributesEntropy = [<span style="color: #B452CD">0</span>] * <span style="color: #658b00">len</span>(attributeIds)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i, attId <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(<span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = <span style="color: #658b00">self</span>.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(<span style="color: #658b00">max</span>(attributesEntropy))]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.attributes[maxId], maxId
|
||||
|
||||
<p>
|
||||
def isSingleLabeled(self, sampleIds):
|
||||
label = self.labels[sampleIds[0]]
|
||||
for sid in sampleIds:
|
||||
if self.labels[sid] != label:
|
||||
return False
|
||||
return True
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">isSingleLabeled</span>(<span style="color: #658b00">self</span>, sampleIds):
|
||||
label = <span style="color: #658b00">self</span>.labels[sampleIds[<span style="color: #B452CD">0</span>]]
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.labels[sid] != label:
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">False</span>
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">True</span>
|
||||
|
||||
<p>
|
||||
def getLabel(self, sampleId):
|
||||
return self.labels[sampleId]
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">getLabel</span>(<span style="color: #658b00">self</span>, sampleId):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.labels[sampleId]
|
||||
|
||||
<p>
|
||||
def id3(self):
|
||||
sampleIds = [x for x in range(len(self.sample))]
|
||||
attributeIds = [x for x in range(len(self.attributes))]
|
||||
self.root = self.id3Recv(sampleIds, attributeIds, self.root)
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">id3</span>(<span style="color: #658b00">self</span>):
|
||||
sampleIds = [x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.sample))]
|
||||
attributeIds = [x <span style="color: #8B008B; font-weight: bold">for</span> x <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.attributes))]
|
||||
<span style="color: #658b00">self</span>.root = <span style="color: #658b00">self</span>.id3Recv(sampleIds, attributeIds, <span style="color: #658b00">self</span>.root)
|
||||
|
||||
<p>
|
||||
def id3Recv(self, sampleIds, attributeIds, root):
|
||||
root = Node() # Initialize current root
|
||||
if self.isSingleLabeled(sampleIds):
|
||||
root.value = self.labels[sampleIds[0]]
|
||||
return root
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) == 0:
|
||||
root.value = self.getDominantLabel(sampleIds)
|
||||
return root
|
||||
bestAttrName, bestAttrId = self.getAttributeMaxInformationGain(
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">id3Recv</span>(<span style="color: #658b00">self</span>, sampleIds, attributeIds, root):
|
||||
root = Node() <span style="color: #228B22"># Initialize current root</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.isSingleLabeled(sampleIds):
|
||||
root.value = <span style="color: #658b00">self</span>.labels[sampleIds[<span style="color: #B452CD">0</span>]]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
<span style="color: #228B22"># print(attributeIds)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(attributeIds) == <span style="color: #B452CD">0</span>:
|
||||
root.value = <span style="color: #658b00">self</span>.getDominantLabel(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
bestAttrName, bestAttrId = <span style="color: #658b00">self</span>.getAttributeMaxInformationGain(
|
||||
sampleIds, attributeIds)
|
||||
# print(bestAttrName)
|
||||
<span style="color: #228B22"># print(bestAttrName)</span>
|
||||
root.value = bestAttrName
|
||||
root.childs = [] # Create list of children
|
||||
for value in self.getAttributeValues(sampleIds, bestAttrId):
|
||||
# print(value)
|
||||
root.childs = [] <span style="color: #228B22"># Create list of children</span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> value <span style="color: #8B008B">in</span> <span style="color: #658b00">self</span>.getAttributeValues(sampleIds, bestAttrId):
|
||||
<span style="color: #228B22"># print(value)</span>
|
||||
child = Node()
|
||||
child.value = value
|
||||
root.childs.append(child) # Append new child node to current
|
||||
# root
|
||||
root.childs.append(child) <span style="color: #228B22"># Append new child node to current</span>
|
||||
<span style="color: #228B22"># root</span>
|
||||
childSampleIds = []
|
||||
for sid in sampleIds:
|
||||
if self.sample[sid][bestAttrId] == value:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> sid <span style="color: #8B008B">in</span> sampleIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.sample[sid][bestAttrId] == value:
|
||||
childSampleIds.append(sid)
|
||||
if len(childSampleIds) == 0:
|
||||
child.next = self.getDominantLabel(sampleIds)
|
||||
else:
|
||||
# print(bestAttrName, bestAttrId)
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) > 0 and bestAttrId in attributeIds:
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(childSampleIds) == <span style="color: #B452CD">0</span>:
|
||||
child.next = <span style="color: #658b00">self</span>.getDominantLabel(sampleIds)
|
||||
<span style="color: #8B008B; font-weight: bold">else</span>:
|
||||
<span style="color: #228B22"># print(bestAttrName, bestAttrId)</span>
|
||||
<span style="color: #228B22"># print(attributeIds)</span>
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(attributeIds) > <span style="color: #B452CD">0</span> <span style="color: #8B008B">and</span> bestAttrId <span style="color: #8B008B">in</span> attributeIds:
|
||||
toRemove = attributeIds.index(bestAttrId)
|
||||
attributeIds.pop(toRemove)
|
||||
child.next = self.id3Recv(
|
||||
child.next = <span style="color: #658b00">self</span>.id3Recv(
|
||||
childSampleIds, attributeIds, child.next)
|
||||
return root
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> root
|
||||
|
||||
<p>
|
||||
def printTree(self):
|
||||
if self.root:
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">printTree</span>(<span style="color: #658b00">self</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">self</span>.root:
|
||||
roots = deque()
|
||||
roots.append(self.root)
|
||||
while len(roots) > 0:
|
||||
roots.append(<span style="color: #658b00">self</span>.root)
|
||||
<span style="color: #8B008B; font-weight: bold">while</span> <span style="color: #658b00">len</span>(roots) > <span style="color: #B452CD">0</span>:
|
||||
root = roots.popleft()
|
||||
print(root.value)
|
||||
if root.childs:
|
||||
for child in root.childs:
|
||||
print('({})'.format(child.value))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(root.value)
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> root.childs:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> child <span style="color: #8B008B">in</span> root.childs:
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'({})'</span>.format(child.value))
|
||||
roots.append(child.next)
|
||||
elif root.next:
|
||||
print(root.next)
|
||||
<span style="color: #8B008B; font-weight: bold">elif</span> root.next:
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(root.next)
|
||||
|
||||
<p>
|
||||
def test():
|
||||
f = open('DataFiles/rideclass.csv')
|
||||
attributes = f.readline().split(',')
|
||||
attributes = attributes[1:len(attributes)-1]
|
||||
print(attributes)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">test</span>():
|
||||
f = <span style="color: #658b00">open</span>(<span style="color: #CD5555">'DataFiles/rideclass.csv'</span>)
|
||||
attributes = f.readline().split(<span style="color: #CD5555">','</span>)
|
||||
attributes = attributes[<span style="color: #B452CD">1</span>:<span style="color: #658b00">len</span>(attributes)-<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(attributes)
|
||||
sample = f.readlines()
|
||||
f.close()
|
||||
for i in range(len(sample)):
|
||||
sample[i] = re.sub('\d+,', '', sample[i])
|
||||
sample[i] = sample[i].strip().split(',')
|
||||
<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>(sample)):
|
||||
sample[i] = re.sub(<span style="color: #CD5555">'\d+,'</span>, <span style="color: #CD5555">''</span>, sample[i])
|
||||
sample[i] = sample[i].strip().split(<span style="color: #CD5555">','</span>)
|
||||
labels = []
|
||||
for s in sample:
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> s <span style="color: #8B008B">in</span> sample:
|
||||
labels.append(s.pop())
|
||||
# print(sample)
|
||||
# print(labels)
|
||||
<span style="color: #228B22"># print(sample)</span>
|
||||
<span style="color: #228B22"># print(labels)</span>
|
||||
decisionTree = DecisionTree(sample, attributes, labels)
|
||||
print("System entropy {}".format(decisionTree.entropy))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"System entropy {}"</span>.format(decisionTree.entropy))
|
||||
decisionTree.id3()
|
||||
decisionTree.printTree()
|
||||
|
||||
<p>
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #00688B">__name__</span> == <span style="color: #CD5555">'__main__'</span>:
|
||||
test()
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<h2 id="___sec21">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1155,7 +1126,7 @@ deep_tree_clf.fit(X_train_scaled, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">Another example, the moons again </h2>
|
||||
<h2 id="___sec22">Another example, the moons again </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1227,7 +1198,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">Playing around with regions </h2>
|
||||
<h2 id="___sec23">Playing around with regions </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1255,7 +1226,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">Regression trees </h2>
|
||||
<h2 id="___sec24">Regression trees </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1277,7 +1248,7 @@ tree_reg.fit(X, y)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec26">Final regressor code </h2>
|
||||
<h2 id="___sec25">Final regressor code </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1355,7 +1326,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec27">Pros and cons of trees, pros </h2>
|
||||
<h2 id="___sec26">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>
|
||||
@@ -1369,7 +1340,7 @@ plt.show()
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec28">Disadvantages </h2>
|
||||
<h2 id="___sec27">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>
|
||||
@@ -1386,7 +1357,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="___sec29">Bagging </h2>
|
||||
<h2 id="___sec28">Bagging </h2>
|
||||
|
||||
<p>
|
||||
The <b>plain</b> decision trees suffer from high
|
||||
@@ -1405,7 +1376,7 @@ learning method.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec30">More bagging </h2>
|
||||
<h2 id="___sec29">More bagging </h2>
|
||||
|
||||
<p>
|
||||
Bagging typically results in improved accuracy
|
||||
@@ -1434,7 +1405,7 @@ predictor, averaged over all \( B \) trees.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec31">Simple example, head or tail </h2>
|
||||
<h2 id="___sec30">Simple example, head or tail </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1454,7 +1425,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec32">Bagging Example </h2>
|
||||
<h2 id="___sec31">Bagging Example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1505,7 +1476,7 @@ voting_clf.fit(X_train, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec33">Random forests </h2>
|
||||
<h2 id="___sec32">Random forests </h2>
|
||||
|
||||
<p>
|
||||
Random forests provide an improvement over bagged trees by way of a
|
||||
@@ -1549,7 +1520,7 @@ this setting.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec34">A simple scikit-learn example </h2>
|
||||
<h2 id="___sec33">A simple scikit-learn example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1567,7 +1538,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="___sec35">Please, not the moons again! </h2>
|
||||
<h2 id="___sec34">Please, not the moons again! </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -1625,7 +1596,7 @@ voting_clf.fit(X_train, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec36">Bagging examples </h2>
|
||||
<h2 id="___sec35">Bagging examples </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -1686,7 +1657,7 @@ plt.show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec37">Then random forests </h2>
|
||||
<h2 id="___sec36">Then random forests </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
|
||||
@@ -72,52 +72,47 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('A typical Decision Tree with its pertinent Jargon, Regeression '
|
||||
'Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('General Features', 2, None, '___sec3'),
|
||||
('How do we set it up?', 2, None, '___sec4'),
|
||||
('Decision trees and Regression', 2, None, '___sec5'),
|
||||
('Building a tree, regression', 2, None, '___sec6'),
|
||||
('General Features', 2, None, '___sec2'),
|
||||
('How do we set it up?', 2, None, '___sec3'),
|
||||
('Decision trees and Regression', 2, None, '___sec4'),
|
||||
('Building a tree, regression', 2, None, '___sec5'),
|
||||
('A top-down approach, recursive binary splitting',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
('Making a tree', 2, None, '___sec8'),
|
||||
('Pruning the tree', 2, None, '___sec9'),
|
||||
('Cost complexity pruning', 2, None, '___sec10'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec11'),
|
||||
('A Classification Tree', 2, None, '___sec12'),
|
||||
('Growing a classification tree', 2, None, '___sec13'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec15'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec16'),
|
||||
('Computing the Gini index', 2, None, '___sec17'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec18'),
|
||||
('Computing the Gini Factor', 2, None, '___sec19'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec20'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec21'),
|
||||
'___sec6'),
|
||||
('Making a tree', 2, None, '___sec7'),
|
||||
('Pruning the tree', 2, None, '___sec8'),
|
||||
('Cost complexity pruning', 2, None, '___sec9'),
|
||||
('Schematic Regression Procedure', 2, None, '___sec10'),
|
||||
('A Classification Tree', 2, None, '___sec11'),
|
||||
('Growing a classification tree', 2, None, '___sec12'),
|
||||
('Classification tree, how to split nodes', 2, None, '___sec13'),
|
||||
('Visualizing the Tree, Classification', 2, None, '___sec14'),
|
||||
('Visualizing the Tree, The Moons', 2, None, '___sec15'),
|
||||
('Computing the Gini index', 2, None, '___sec16'),
|
||||
('Simple Python Code to read in Data', 2, None, '___sec17'),
|
||||
('Computing the Gini Factor', 2, None, '___sec18'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec19'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec20'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec22'),
|
||||
('Another example, the moons again', 2, None, '___sec23'),
|
||||
('Playing around with regions', 2, None, '___sec24'),
|
||||
('Regression trees', 2, None, '___sec25'),
|
||||
('Final regressor code', 2, None, '___sec26'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec27'),
|
||||
('Disadvantages', 2, None, '___sec28'),
|
||||
('Bagging', 2, None, '___sec29'),
|
||||
('More bagging', 2, None, '___sec30'),
|
||||
('Simple example, head or tail', 2, None, '___sec31'),
|
||||
('Bagging Example', 2, None, '___sec32'),
|
||||
('Random forests', 2, None, '___sec33'),
|
||||
('A simple scikit-learn example', 2, None, '___sec34'),
|
||||
('Please, not the moons again!', 2, None, '___sec35'),
|
||||
('Bagging examples', 2, None, '___sec36'),
|
||||
('Then random forests', 2, None, '___sec37')]}
|
||||
'___sec21'),
|
||||
('Another example, the moons again', 2, None, '___sec22'),
|
||||
('Playing around with regions', 2, None, '___sec23'),
|
||||
('Regression trees', 2, None, '___sec24'),
|
||||
('Final regressor code', 2, None, '___sec25'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec26'),
|
||||
('Disadvantages', 2, None, '___sec27'),
|
||||
('Bagging', 2, None, '___sec28'),
|
||||
('More bagging', 2, None, '___sec29'),
|
||||
('Simple example, head or tail', 2, None, '___sec30'),
|
||||
('Bagging Example', 2, None, '___sec31'),
|
||||
('Random forests', 2, None, '___sec32'),
|
||||
('A simple scikit-learn example', 2, None, '___sec33'),
|
||||
('Please, not the moons again!', 2, None, '___sec34'),
|
||||
('Bagging examples', 2, None, '___sec35'),
|
||||
('Then random forests', 2, None, '___sec36')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -200,21 +195,10 @@ given some assumptions, make predictions about the target feature value
|
||||
|
||||
<h2 id="___sec1">A typical Decision Tree with its pertinent Jargon, Classification Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure here we present a decision tree obtained from a classification problem
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">A typical Decision Tree with its pertinent Jargon, Regeression Problem </h2>
|
||||
|
||||
<p>
|
||||
In the figure we present a decision tree obtained from a simple regression problem
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">General Features </h2>
|
||||
<h2 id="___sec2">General Features </h2>
|
||||
|
||||
<p>
|
||||
The overarching approach to decision trees is a top-down approach.
|
||||
@@ -232,7 +216,7 @@ node.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">How do we set it up? </h2>
|
||||
<h2 id="___sec3">How do we set it up? </h2>
|
||||
|
||||
<p>
|
||||
In simplified terms, the process of training a decision tree and
|
||||
@@ -250,7 +234,7 @@ Then we are essentially done!
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Decision trees and Regression </h2>
|
||||
<h2 id="___sec4">Decision trees and Regression </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -346,7 +330,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Building a tree, regression </h2>
|
||||
<h2 id="___sec5">Building a tree, regression </h2>
|
||||
|
||||
<p>
|
||||
There are mainly two steps
|
||||
@@ -374,7 +358,7 @@ within box \( j \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">A top-down approach, recursive binary splitting </h2>
|
||||
<h2 id="___sec6">A top-down approach, recursive binary splitting </h2>
|
||||
|
||||
<p>
|
||||
Unfortunately, it is computationally infeasible to consider every
|
||||
@@ -393,7 +377,7 @@ better tree in some future step.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Making a tree </h2>
|
||||
<h2 id="___sec7">Making a tree </h2>
|
||||
|
||||
<p>
|
||||
In order to implement the recursive binary splitting we start by selecting
|
||||
@@ -444,7 +428,7 @@ region contains more than five observations.
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec9">Pruning the tree </h2>
|
||||
<h2 id="___sec8">Pruning the tree </h2>
|
||||
|
||||
<p>
|
||||
The above procedure is rather straightforward, but leads often to
|
||||
@@ -463,7 +447,7 @@ parameter \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Cost complexity pruning </h2>
|
||||
<h2 id="___sec9">Cost complexity pruning </h2>
|
||||
For each value of \( \alpha \) there corresponds a subtree \( T \in T_0 \) such that
|
||||
$$
|
||||
\sum_{m=1}^{\overline{T}}\sum_{i:x_i\in R_m}(y_i-\overline{y}_{R_m})^2+\alpha\overline{T},
|
||||
@@ -494,7 +478,7 @@ subtree corresponding to \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Schematic Regression Procedure </h2>
|
||||
<h2 id="___sec10">Schematic Regression Procedure </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -520,7 +504,7 @@ subtree corresponding to \( \alpha \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec12">A Classification Tree </h2>
|
||||
<h2 id="___sec11">A Classification Tree </h2>
|
||||
|
||||
<p>
|
||||
A classification tree is very similar to a regression tree, except
|
||||
@@ -539,7 +523,7 @@ fall into that region.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec13">Growing a classification tree </h2>
|
||||
<h2 id="___sec12">Growing a classification tree </h2>
|
||||
|
||||
<p>
|
||||
The task of growing a
|
||||
@@ -563,7 +547,7 @@ than is the classification error rate.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec14">Classification tree, how to split nodes </h2>
|
||||
<h2 id="___sec13">Classification tree, how to split nodes </h2>
|
||||
|
||||
<p>
|
||||
If our targets are the outcome of a classification process that takes
|
||||
@@ -613,7 +597,7 @@ $$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Visualizing the Tree, Classification </h2>
|
||||
<h2 id="___sec14">Visualizing the Tree, Classification </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -654,7 +638,7 @@ os<span style="color: #666666">.</span>system(cmd)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Visualizing the Tree, The Moons </h2>
|
||||
<h2 id="___sec15">Visualizing the Tree, The Moons </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -686,7 +670,7 @@ os<span style="color: #666666">.</span>system(cmd)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">Computing the Gini index </h2>
|
||||
<h2 id="___sec16">Computing the Gini index </h2>
|
||||
|
||||
<p>
|
||||
The example we will look at is a classical one in many Machine
|
||||
@@ -726,7 +710,7 @@ The table here summarizes the various attributes and
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">Simple Python Code to read in Data </h2>
|
||||
<h2 id="___sec17">Simple Python Code to read in Data </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -788,7 +772,7 @@ display(y)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">Computing the Gini Factor </h2>
|
||||
<h2 id="___sec18">Computing the Gini Factor </h2>
|
||||
|
||||
<p>
|
||||
The above functions (gini, entropy and misclassification error) are
|
||||
@@ -866,7 +850,7 @@ split <span style="color: #666666">=</span> get_split(dataset)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Entropy and the ID3 algorithm </h2>
|
||||
<h2 id="___sec19">Entropy and the ID3 algorithm </h2>
|
||||
|
||||
<p>
|
||||
ID3, learns decision trees by constructing
|
||||
@@ -902,216 +886,203 @@ attributes at each step while growing the tree.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec21">Implementing the ID3 Algorithm </h2>
|
||||
<h2 id="___sec20">Implementing the ID3 Algorithm </h2>
|
||||
|
||||
<p>
|
||||
import re
|
||||
import math
|
||||
from collections import deque
|
||||
|
||||
<p>
|
||||
<!-- x is examples in training set -->
|
||||
<!-- y is set of attributes -->
|
||||
<!-- label is target attributes -->
|
||||
<!-- Node is a class which has properties values, childs, and next -->
|
||||
<!-- root is top node in the decision tree -->
|
||||
<!-- 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">re</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">math</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">collections</span> <span style="color: #008000; font-weight: bold">import</span> deque
|
||||
|
||||
<p>
|
||||
class Node(object):
|
||||
def __init__(self):
|
||||
self.value = None
|
||||
self.next = None
|
||||
self.childs = None
|
||||
<span style="color: #408080; font-style: italic"># x is examples in training set</span>
|
||||
<span style="color: #408080; font-style: italic"># y is set of targets</span>
|
||||
<span style="color: #408080; font-style: italic"># label is target attributes</span>
|
||||
<span style="color: #408080; font-style: italic"># Node is a class which has properties values, childs, and next</span>
|
||||
<span style="color: #408080; font-style: italic"># root is top node in the decision tree</span>
|
||||
|
||||
<p>
|
||||
<!-- Simple class of Decision Tree -->
|
||||
<!-- Aimed for who want to learn Decision Tree, so it is not optimized -->
|
||||
class DecisionTree(object):
|
||||
def __init__(self, sample, attributes, labels):
|
||||
self.sample = sample
|
||||
self.attributes = attributes
|
||||
self.labels = labels
|
||||
self.labelCodes = None
|
||||
self.labelCodesCount = None
|
||||
self.initLabelCodes()
|
||||
# print(self.labelCodes)
|
||||
self.root = None
|
||||
self.entropy = self.getEntropy([x for x in range(len(self.labels))])
|
||||
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Node</span>(<span style="color: #008000">object</span>):
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>childs <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
|
||||
<p>
|
||||
def initLabelCodes(self):
|
||||
self.labelCodes = []
|
||||
self.labelCodesCount = []
|
||||
for l in self.labels:
|
||||
if l not in self.labelCodes:
|
||||
self.labelCodes.append(l)
|
||||
self.labelCodesCount.append(0)
|
||||
self.labelCodesCount[self.labelCodes.index(l)] += 1
|
||||
<span style="color: #408080; font-style: italic"># Simple class of Decision Tree</span>
|
||||
<span style="color: #408080; font-style: italic"># Aimed for who want to learn Decision Tree, so it is not optimized</span>
|
||||
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">DecisionTree</span>(<span style="color: #008000">object</span>):
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, sample, attributes, labels):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>sample <span style="color: #666666">=</span> sample
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes <span style="color: #666666">=</span> attributes
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labels <span style="color: #666666">=</span> labels
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>initLabelCodes()
|
||||
<span style="color: #408080; font-style: italic"># print(self.labelCodes)</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">None</span>
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>entropy <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy([x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels))])
|
||||
|
||||
<p>
|
||||
def getLabelCodeId(self, sampleId):
|
||||
return self.labelCodes.index(self.labels[sampleId])
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">initLabelCodes</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels:
|
||||
<span style="color: #008000; font-weight: bold">if</span> l <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes:
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>append(l)
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(l)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
|
||||
<p>
|
||||
def getAttributeValues(self, sampleIds, attributeId):
|
||||
vals = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in vals:
|
||||
vals.append(val)
|
||||
# print(vals)
|
||||
return vals
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabelCodeId</span>(<span style="color: #008000">self</span>, sampleId):
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId])
|
||||
|
||||
<p>
|
||||
def getEntropy(self, sampleIds):
|
||||
entropy = 0
|
||||
labelCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCount[self.getLabelCodeId(sid)] += 1
|
||||
# print("-ge", labelCount)
|
||||
for lv in labelCount:
|
||||
# print(lv)
|
||||
if lv != 0:
|
||||
entropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)
|
||||
else:
|
||||
entropy += 0
|
||||
return entropy
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeValues</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
|
||||
vals <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
|
||||
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> vals:
|
||||
vals<span style="color: #666666">.</span>append(val)
|
||||
<span style="color: #408080; font-style: italic"># print(vals)</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> vals
|
||||
|
||||
<p>
|
||||
def getDominantLabel(self, sampleIds):
|
||||
labelCodesCount = [0] * len(self.labelCodes)
|
||||
for sid in sampleIds:
|
||||
labelCodesCount[self.labelCodes.index(self.labels[sid])] += 1
|
||||
return self.labelCodes[labelCodesCount.index(max(labelCodesCount))]
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getEntropy</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
entropy <span style="color: #666666">=</span> <span style="color: #666666">0</span>
|
||||
labelCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
labelCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>getLabelCodeId(sid)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #408080; font-style: italic"># print("-ge", labelCount)</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> lv <span style="color: #AA22FF; font-weight: bold">in</span> labelCount:
|
||||
<span style="color: #408080; font-style: italic"># print(lv)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> lv <span style="color: #666666">!=</span> <span style="color: #666666">0</span>:
|
||||
entropy <span style="color: #666666">+=</span> <span style="color: #666666">-</span>lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> math<span style="color: #666666">.</span>log(lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds), <span style="color: #666666">2</span>)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
entropy <span style="color: #666666">+=</span> <span style="color: #666666">0</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> entropy
|
||||
|
||||
<p>
|
||||
def getInformationGain(self, sampleIds, attributeId):
|
||||
gain = self.getEntropy(sampleIds)
|
||||
attributeVals = []
|
||||
attributeValsCount = []
|
||||
attributeValsIds = []
|
||||
for sid in sampleIds:
|
||||
val = self.sample[sid][attributeId]
|
||||
if val not in attributeVals:
|
||||
attributeVals.append(val)
|
||||
attributeValsCount.append(0)
|
||||
attributeValsIds.append([])
|
||||
vid = attributeVals.index(val)
|
||||
attributeValsCount[vid] += 1
|
||||
attributeValsIds[vid].append(sid)
|
||||
# print("-gig", self.attributes[attributeId])
|
||||
for vc, vids in zip(attributeValsCount, attributeValsIds):
|
||||
# print("-gig", vids)
|
||||
gain -= vc/len(sampleIds) * self.getEntropy(vids)
|
||||
return gain
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getDominantLabel</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
labelCodesCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid])] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes[labelCodesCount<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(labelCodesCount))]
|
||||
|
||||
<p>
|
||||
def getAttributeMaxInformationGain(self, sampleIds, attributeIds):
|
||||
attributesEntropy = [0] * len(attributeIds)
|
||||
for i, attId in zip(range(len(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] = self.getInformationGain(sampleIds, attId)
|
||||
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
|
||||
return self.attributes[maxId], maxId
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
|
||||
gain <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(sampleIds)
|
||||
attributeVals <span style="color: #666666">=</span> []
|
||||
attributeValsCount <span style="color: #666666">=</span> []
|
||||
attributeValsIds <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
|
||||
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> attributeVals:
|
||||
attributeVals<span style="color: #666666">.</span>append(val)
|
||||
attributeValsCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
|
||||
attributeValsIds<span style="color: #666666">.</span>append([])
|
||||
vid <span style="color: #666666">=</span> attributeVals<span style="color: #666666">.</span>index(val)
|
||||
attributeValsCount[vid] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
attributeValsIds[vid]<span style="color: #666666">.</span>append(sid)
|
||||
<span style="color: #408080; font-style: italic"># print("-gig", self.attributes[attributeId])</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> vc, vids <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(attributeValsCount, attributeValsIds):
|
||||
<span style="color: #408080; font-style: italic"># print("-gig", vids)</span>
|
||||
gain <span style="color: #666666">-=</span> vc<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(vids)
|
||||
<span style="color: #008000; font-weight: bold">return</span> gain
|
||||
|
||||
<p>
|
||||
def isSingleLabeled(self, sampleIds):
|
||||
label = self.labels[sampleIds[0]]
|
||||
for sid in sampleIds:
|
||||
if self.labels[sid] != label:
|
||||
return False
|
||||
return True
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeMaxInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds):
|
||||
attributesEntropy <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(attributeIds)
|
||||
<span style="color: #008000; font-weight: bold">for</span> i, attId <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(<span style="color: #008000">range</span>(<span style="color: #008000">len</span>(attributeIds)), attributeIds):
|
||||
attributesEntropy[i] <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getInformationGain(sampleIds, attId)
|
||||
maxId <span style="color: #666666">=</span> attributeIds[attributesEntropy<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(attributesEntropy))]
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>attributes[maxId], maxId
|
||||
|
||||
<p>
|
||||
def getLabel(self, sampleId):
|
||||
return self.labels[sampleId]
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">isSingleLabeled</span>(<span style="color: #008000">self</span>, sampleIds):
|
||||
label <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid] <span style="color: #666666">!=</span> label:
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">False</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">True</span>
|
||||
|
||||
<p>
|
||||
def id3(self):
|
||||
sampleIds = [x for x in range(len(self.sample))]
|
||||
attributeIds = [x for x in range(len(self.attributes))]
|
||||
self.root = self.id3Recv(sampleIds, attributeIds, self.root)
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabel</span>(<span style="color: #008000">self</span>, sampleId):
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId]
|
||||
|
||||
<p>
|
||||
def id3Recv(self, sampleIds, attributeIds, root):
|
||||
root = Node() # Initialize current root
|
||||
if self.isSingleLabeled(sampleIds):
|
||||
root.value = self.labels[sampleIds[0]]
|
||||
return root
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) == 0:
|
||||
root.value = self.getDominantLabel(sampleIds)
|
||||
return root
|
||||
bestAttrName, bestAttrId = self.getAttributeMaxInformationGain(
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3</span>(<span style="color: #008000">self</span>):
|
||||
sampleIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>sample))]
|
||||
attributeIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes))]
|
||||
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(sampleIds, attributeIds, <span style="color: #008000">self</span><span style="color: #666666">.</span>root)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3Recv</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds, root):
|
||||
root <span style="color: #666666">=</span> Node() <span style="color: #408080; font-style: italic"># Initialize current root</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>isSingleLabeled(sampleIds):
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
bestAttrName, bestAttrId <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeMaxInformationGain(
|
||||
sampleIds, attributeIds)
|
||||
# print(bestAttrName)
|
||||
root.value = bestAttrName
|
||||
root.childs = [] # Create list of children
|
||||
for value in self.getAttributeValues(sampleIds, bestAttrId):
|
||||
# print(value)
|
||||
child = Node()
|
||||
child.value = value
|
||||
root.childs.append(child) # Append new child node to current
|
||||
# root
|
||||
childSampleIds = []
|
||||
for sid in sampleIds:
|
||||
if self.sample[sid][bestAttrId] == value:
|
||||
childSampleIds.append(sid)
|
||||
if len(childSampleIds) == 0:
|
||||
child.next = self.getDominantLabel(sampleIds)
|
||||
else:
|
||||
# print(bestAttrName, bestAttrId)
|
||||
# print(attributeIds)
|
||||
if len(attributeIds) > 0 and bestAttrId in attributeIds:
|
||||
toRemove = attributeIds.index(bestAttrId)
|
||||
attributeIds.pop(toRemove)
|
||||
child.next = self.id3Recv(
|
||||
childSampleIds, attributeIds, child.next)
|
||||
return root
|
||||
<span style="color: #408080; font-style: italic"># print(bestAttrName)</span>
|
||||
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> bestAttrName
|
||||
root<span style="color: #666666">.</span>childs <span style="color: #666666">=</span> [] <span style="color: #408080; font-style: italic"># Create list of children</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> value <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeValues(sampleIds, bestAttrId):
|
||||
<span style="color: #408080; font-style: italic"># print(value)</span>
|
||||
child <span style="color: #666666">=</span> Node()
|
||||
child<span style="color: #666666">.</span>value <span style="color: #666666">=</span> value
|
||||
root<span style="color: #666666">.</span>childs<span style="color: #666666">.</span>append(child) <span style="color: #408080; font-style: italic"># Append new child node to current</span>
|
||||
<span style="color: #408080; font-style: italic"># root</span>
|
||||
childSampleIds <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][bestAttrId] <span style="color: #666666">==</span> value:
|
||||
childSampleIds<span style="color: #666666">.</span>append(sid)
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(childSampleIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
|
||||
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
<span style="color: #408080; font-style: italic"># print(bestAttrName, bestAttrId)</span>
|
||||
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #AA22FF; font-weight: bold">and</span> bestAttrId <span style="color: #AA22FF; font-weight: bold">in</span> attributeIds:
|
||||
toRemove <span style="color: #666666">=</span> attributeIds<span style="color: #666666">.</span>index(bestAttrId)
|
||||
attributeIds<span style="color: #666666">.</span>pop(toRemove)
|
||||
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(
|
||||
childSampleIds, attributeIds, child<span style="color: #666666">.</span>next)
|
||||
<span style="color: #008000; font-weight: bold">return</span> root
|
||||
|
||||
<p>
|
||||
def printTree(self):
|
||||
if self.root:
|
||||
roots = deque()
|
||||
roots.append(self.root)
|
||||
while len(roots) > 0:
|
||||
root = roots.popleft()
|
||||
print(root.value)
|
||||
if root.childs:
|
||||
for child in root.childs:
|
||||
print('({})'.format(child.value))
|
||||
roots.append(child.next)
|
||||
elif root.next:
|
||||
print(root.next)
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">printTree</span>(<span style="color: #008000">self</span>):
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>root:
|
||||
roots <span style="color: #666666">=</span> deque()
|
||||
roots<span style="color: #666666">.</span>append(<span style="color: #008000">self</span><span style="color: #666666">.</span>root)
|
||||
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #008000">len</span>(roots) <span style="color: #666666">></span> <span style="color: #666666">0</span>:
|
||||
root <span style="color: #666666">=</span> roots<span style="color: #666666">.</span>popleft()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>value)
|
||||
<span style="color: #008000; font-weight: bold">if</span> root<span style="color: #666666">.</span>childs:
|
||||
<span style="color: #008000; font-weight: bold">for</span> child <span style="color: #AA22FF; font-weight: bold">in</span> root<span style="color: #666666">.</span>childs:
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'({})'</span><span style="color: #666666">.</span>format(child<span style="color: #666666">.</span>value))
|
||||
roots<span style="color: #666666">.</span>append(child<span style="color: #666666">.</span>next)
|
||||
<span style="color: #008000; font-weight: bold">elif</span> root<span style="color: #666666">.</span>next:
|
||||
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>next)
|
||||
|
||||
<p>
|
||||
def test():
|
||||
f = open('DataFiles/rideclass.csv')
|
||||
attributes = f.readline().split(',')
|
||||
attributes = attributes[1:len(attributes)-1]
|
||||
print(attributes)
|
||||
sample = f.readlines()
|
||||
f.close()
|
||||
for i in range(len(sample)):
|
||||
sample[i] = re.sub('\d+,', '', sample[i])
|
||||
sample[i] = sample[i].strip().split(',')
|
||||
labels = []
|
||||
for s in sample:
|
||||
labels.append(s.pop())
|
||||
# print(sample)
|
||||
# print(labels)
|
||||
decisionTree = DecisionTree(sample, attributes, labels)
|
||||
print("System entropy {}".format(decisionTree.entropy))
|
||||
decisionTree.id3()
|
||||
decisionTree.printTree()
|
||||
|
||||
<p>
|
||||
if __name__ == '__main__':
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test</span>():
|
||||
f <span style="color: #666666">=</span> <span style="color: #008000">open</span>(<span style="color: #BA2121">'DataFiles/rideclass.csv'</span>)
|
||||
attributes <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readline()<span style="color: #666666">.</span>split(<span style="color: #BA2121">','</span>)
|
||||
attributes <span style="color: #666666">=</span> attributes[<span style="color: #666666">1</span>:<span style="color: #008000">len</span>(attributes)<span style="color: #666666">-1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(attributes)
|
||||
sample <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readlines()
|
||||
f<span style="color: #666666">.</span>close()
|
||||
<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>(sample)):
|
||||
sample[i] <span style="color: #666666">=</span> re<span style="color: #666666">.</span>sub(<span style="color: #BA2121">'\d+,'</span>, <span style="color: #BA2121">''</span>, sample[i])
|
||||
sample[i] <span style="color: #666666">=</span> sample[i]<span style="color: #666666">.</span>strip()<span style="color: #666666">.</span>split(<span style="color: #BA2121">','</span>)
|
||||
labels <span style="color: #666666">=</span> []
|
||||
<span style="color: #008000; font-weight: bold">for</span> s <span style="color: #AA22FF; font-weight: bold">in</span> sample:
|
||||
labels<span style="color: #666666">.</span>append(s<span style="color: #666666">.</span>pop())
|
||||
<span style="color: #408080; font-style: italic"># print(sample)</span>
|
||||
<span style="color: #408080; font-style: italic"># print(labels)</span>
|
||||
decisionTree <span style="color: #666666">=</span> DecisionTree(sample, attributes, labels)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"System entropy {}"</span><span style="color: #666666">.</span>format(decisionTree<span style="color: #666666">.</span>entropy))
|
||||
decisionTree<span style="color: #666666">.</span>id3()
|
||||
decisionTree<span style="color: #666666">.</span>printTree()
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #19177C">__name__</span> <span style="color: #666666">==</span> <span style="color: #BA2121">'__main__'</span>:
|
||||
test()
|
||||
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<h2 id="___sec21">Cancer Data again now with Decision Trees and other Methods </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1160,7 +1131,7 @@ deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">Another example, the moons again </h2>
|
||||
<h2 id="___sec22">Another example, the moons again </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1232,7 +1203,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">Playing around with regions </h2>
|
||||
<h2 id="___sec23">Playing around with regions </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1260,7 +1231,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">Regression trees </h2>
|
||||
<h2 id="___sec24">Regression trees </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1282,7 +1253,7 @@ tree_reg<span style="color: #666666">.</span>fit(X, y)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec26">Final regressor code </h2>
|
||||
<h2 id="___sec25">Final regressor code </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1360,7 +1331,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec27">Pros and cons of trees, pros </h2>
|
||||
<h2 id="___sec26">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>
|
||||
@@ -1374,7 +1345,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec28">Disadvantages </h2>
|
||||
<h2 id="___sec27">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>
|
||||
@@ -1391,7 +1362,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="___sec29">Bagging </h2>
|
||||
<h2 id="___sec28">Bagging </h2>
|
||||
|
||||
<p>
|
||||
The <b>plain</b> decision trees suffer from high
|
||||
@@ -1410,7 +1381,7 @@ learning method.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec30">More bagging </h2>
|
||||
<h2 id="___sec29">More bagging </h2>
|
||||
|
||||
<p>
|
||||
Bagging typically results in improved accuracy
|
||||
@@ -1439,7 +1410,7 @@ predictor, averaged over all \( B \) trees.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec31">Simple example, head or tail </h2>
|
||||
<h2 id="___sec30">Simple example, head or tail </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1459,7 +1430,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec32">Bagging Example </h2>
|
||||
<h2 id="___sec31">Bagging Example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1510,7 +1481,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="___sec33">Random forests </h2>
|
||||
<h2 id="___sec32">Random forests </h2>
|
||||
|
||||
<p>
|
||||
Random forests provide an improvement over bagged trees by way of a
|
||||
@@ -1554,7 +1525,7 @@ this setting.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec34">A simple scikit-learn example </h2>
|
||||
<h2 id="___sec33">A simple scikit-learn example </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1572,7 +1543,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="___sec35">Please, not the moons again! </h2>
|
||||
<h2 id="___sec34">Please, not the moons again! </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -1630,7 +1601,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="___sec36">Bagging examples </h2>
|
||||
<h2 id="___sec35">Bagging examples </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -1691,7 +1662,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec37">Then random forests </h2>
|
||||
<h2 id="___sec36">Then random forests </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
|
||||
@@ -47,12 +47,8 @@
|
||||
"\n",
|
||||
"## A typical Decision Tree with its pertinent Jargon, Classification Problem\n",
|
||||
"\n",
|
||||
"In the figure here we present a decision tree obtained from a classification problem\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## A typical Decision Tree with its pertinent Jargon, Regeression Problem\n",
|
||||
"\n",
|
||||
"In the figure we present a decision tree obtained from a simple regression problem\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## General Features\n",
|
||||
@@ -818,17 +814,26 @@
|
||||
"The ID3 algorithm uses this information gain measure to select among the candidate\n",
|
||||
"attributes at each step while growing the tree.\n",
|
||||
"\n",
|
||||
"## Implementing the ID3 Algorithm\n",
|
||||
"\n",
|
||||
"## Implementing the ID3 Algorithm"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"import math\n",
|
||||
"from collections import deque\n",
|
||||
"\n",
|
||||
"<!-- x is examples in training set -->\n",
|
||||
"<!-- y is set of attributes -->\n",
|
||||
"<!-- label is target attributes -->\n",
|
||||
"<!-- Node is a class which has properties values, childs, and next -->\n",
|
||||
"<!-- root is top node in the decision tree -->\n",
|
||||
"# x is examples in training set\n",
|
||||
"# y is set of targets\n",
|
||||
"# label is target attributes\n",
|
||||
"# Node is a class which has properties values, childs, and next\n",
|
||||
"# root is top node in the decision tree\n",
|
||||
"\n",
|
||||
"class Node(object):\n",
|
||||
"\tdef __init__(self):\n",
|
||||
@@ -836,8 +841,8 @@
|
||||
"\t\tself.next = None\n",
|
||||
"\t\tself.childs = None\n",
|
||||
"\n",
|
||||
"<!-- Simple class of Decision Tree -->\n",
|
||||
"<!-- Aimed for who want to learn Decision Tree, so it is not optimized -->\n",
|
||||
"# Simple class of Decision Tree\n",
|
||||
"# Aimed for who want to learn Decision Tree, so it is not optimized\n",
|
||||
"class DecisionTree(object):\n",
|
||||
"\tdef __init__(self, sample, attributes, labels):\n",
|
||||
"\t\tself.sample = sample\n",
|
||||
@@ -1006,15 +1011,19 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
"\ttest()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\ttest()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Cancer Data again now with Decision Trees and other Methods"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1072,7 +1081,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1153,7 +1162,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1190,7 +1199,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1206,7 +1215,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1227,7 +1236,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1275,7 +1284,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1393,7 +1402,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 14,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1422,7 +1431,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 15,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1528,7 +1537,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 16,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1555,7 +1564,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 17,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1583,7 +1592,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1599,7 +1608,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 19,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1617,7 +1626,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 20,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1640,7 +1649,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 21,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1658,7 +1667,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 22,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1670,7 +1679,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 23,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1684,7 +1693,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 24,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1727,7 +1736,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 25,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1740,7 +1749,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 26,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -35,13 +35,8 @@ given some assumptions, make predictions about the target feature value
|
||||
!split
|
||||
===== A typical Decision Tree with its pertinent Jargon, Classification Problem =====
|
||||
|
||||
In the figure here we present a decision tree obtained from a classification problem
|
||||
|
||||
|
||||
!split
|
||||
===== A typical Decision Tree with its pertinent Jargon, Regeression Problem =====
|
||||
|
||||
In the figure we present a decision tree obtained from a simple regression problem
|
||||
|
||||
|
||||
!split
|
||||
@@ -665,12 +660,13 @@ attributes at each step while growing the tree.
|
||||
!split
|
||||
===== Implementing the ID3 Algorithm =====
|
||||
|
||||
!bc pycod
|
||||
import re
|
||||
import math
|
||||
from collections import deque
|
||||
|
||||
# x is examples in training set
|
||||
# y is set of attributes
|
||||
# y is set of targets
|
||||
# label is target attributes
|
||||
# Node is a class which has properties values, childs, and next
|
||||
# root is top node in the decision tree
|
||||
@@ -852,7 +848,7 @@ def test():
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
|
||||
!ec
|
||||
|
||||
!split
|
||||
===== Cancer Data again now with Decision Trees and other Methods =====
|
||||
|
||||
Reference in New Issue
Block a user