added html files
This commit is contained in:
@@ -0,0 +1,347 @@
|
||||
<!--
|
||||
Automatically generated HTML file from DocOnce source
|
||||
(https://github.com/hplgit/doconce/)
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Data Analysis and Machine Learning: From Decision Trees to Forests and all that">
|
||||
|
||||
<title>Data Analysis and Machine Learning: From Decision Trees to Forests and all that</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
|
||||
<style type="text/css">
|
||||
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Decision trees, overarching aims', 2, None, '___sec0'),
|
||||
('A typical Decision Tree with its pertinent Jargon, '
|
||||
'Classification Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('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,
|
||||
'___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'),
|
||||
('Algorithms for Setting up Decision Trees', 2, None, '___sec16'),
|
||||
('The CART algorithm for Classification', 2, None, '___sec17'),
|
||||
('The CART algorithm for Regression', 2, None, '___sec18'),
|
||||
('Computing the Gini index', 2, None, '___sec19'),
|
||||
('Simple Python Code to read in Data and perform Classification',
|
||||
2,
|
||||
None,
|
||||
'___sec20'),
|
||||
('Computing the Gini Factor', 2, None, '___sec21'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec22'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec23'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec24'),
|
||||
('Another example, the moons again', 2, None, '___sec25'),
|
||||
('Playing around with regions', 2, None, '___sec26'),
|
||||
('Regression trees', 2, None, '___sec27'),
|
||||
('Final regressor code', 2, None, '___sec28'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec29'),
|
||||
('Disadvantages', 2, None, '___sec30'),
|
||||
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
|
||||
'Boosting, Meet the Jungle of Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec31'),
|
||||
('An Overview of Ensemble Methods', 2, None, '___sec32'),
|
||||
('Bagging', 2, None, '___sec33'),
|
||||
('More bagging', 2, None, '___sec34'),
|
||||
('Simple Voting Example, head or tail', 2, None, '___sec35'),
|
||||
('Using the Voting Classifier', 2, None, '___sec36'),
|
||||
('Please, not the moons again! Voting and Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec37'),
|
||||
('Now Bagging', 2, None, '___sec38'),
|
||||
('Making our own Bagging with Bootstrap', 2, None, '___sec39'),
|
||||
('Changing the Level of the Decision Tree', 2, None, '___sec40'),
|
||||
('Random forests', 2, None, '___sec41'),
|
||||
('Random Forest Algorithm', 2, None, '___sec42'),
|
||||
('Random Forests Compared with other Methods on the Cancer Data',
|
||||
2,
|
||||
None,
|
||||
'___sec43'),
|
||||
('Compare Bagging on Trees with Random Forests',
|
||||
2,
|
||||
None,
|
||||
'___sec44'),
|
||||
('Bootstrap with Random Forests Instead of a Single Tree, own '
|
||||
'Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec45'),
|
||||
("Boosting, a Bird'e Eye", 2, None, '___sec46'),
|
||||
('Adaptive boosting: AdaBoost, Basic Algorithm',
|
||||
2,
|
||||
None,
|
||||
'___sec47'),
|
||||
('Basic Steps of AdaBoost', 2, None, '___sec48'),
|
||||
('AdaBoost Examples', 2, None, '___sec49'),
|
||||
('Gradient boosting: Basics', 2, None, '___sec50'),
|
||||
('Gradient Boosting, algorithm', 2, None, '___sec51'),
|
||||
('Gradient Boosting, Examples', 2, None, '___sec52'),
|
||||
('Gradient Boots with Early Stopping', 2, None, '___sec53'),
|
||||
('XGBoost: Extreme Gradient Boosting', 2, None, '___sec54'),
|
||||
('Regression Case', 2, None, '___sec55'),
|
||||
('Xgboost on the Cancer Data', 2, None, '___sec56')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="DecisionTrees-bs.html">Data Analysis and Machine Learning: From Decision Trees to Forests and all that</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._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%;">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%;">Algorithms for Setting up Decision Trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">The CART algorithm for Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">The CART algorithm for Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Using the Voting Classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Please, not the moons again! Voting and Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs039.html#___sec38" style="font-size: 80%;">Now Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs040.html#___sec39" style="font-size: 80%;">Making our own Bagging with Bootstrap</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs041.html#___sec40" style="font-size: 80%;">Changing the Level of the Decision Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs042.html#___sec41" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs043.html#___sec42" style="font-size: 80%;">Random Forest Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs044.html#___sec43" style="font-size: 80%;">Random Forests Compared with other Methods on the Cancer Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs045.html#___sec44" style="font-size: 80%;">Compare Bagging on Trees with Random Forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs046.html#___sec45" style="font-size: 80%;">Bootstrap with Random Forests Instead of a Single Tree, own Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs047.html#___sec46" style="font-size: 80%;">Boosting, a Bird'e Eye</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs048.html#___sec47" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs049.html#___sec48" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs050.html#___sec49" style="font-size: 80%;">AdaBoost Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs051.html#___sec50" style="font-size: 80%;">Gradient boosting: Basics</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs052.html#___sec51" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs053.html#___sec52" style="font-size: 80%;">Gradient Boosting, Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec53" style="font-size: 80%;">Gradient Boots with Early Stopping</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs055.html#___sec54" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs056.html#___sec55" style="font-size: 80%;">Regression Case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs057.html#___sec56" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
|
||||
<div class="container">
|
||||
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0054"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec53" class="anchor">Gradient Boots with Early Stopping </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.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
|
||||
|
||||
X_train, X_val, y_train, y_val <span style="color: #666666">=</span> train_test_split(X, y, random_state<span style="color: #666666">=49</span>)
|
||||
|
||||
gbrt <span style="color: #666666">=</span> GradientBoostingRegressor(max_depth<span style="color: #666666">=2</span>, n_estimators<span style="color: #666666">=120</span>, random_state<span style="color: #666666">=42</span>)
|
||||
gbrt<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
errors <span style="color: #666666">=</span> [mean_squared_error(y_val, y_pred)
|
||||
<span style="color: #008000; font-weight: bold">for</span> y_pred <span style="color: #AA22FF; font-weight: bold">in</span> gbrt<span style="color: #666666">.</span>staged_predict(X_val)]
|
||||
bst_n_estimators <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argmin(errors) <span style="color: #666666">+</span> <span style="color: #666666">1</span>
|
||||
|
||||
gbrt_best <span style="color: #666666">=</span> GradientBoostingRegressor(max_depth<span style="color: #666666">=2</span>,n_estimators<span style="color: #666666">=</span>bst_n_estimators, random_state<span style="color: #666666">=42</span>)
|
||||
gbrt_best<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
min_error <span style="color: #666666">=</span> np<span style="color: #666666">.</span>min(errors)
|
||||
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(errors, <span style="color: #BA2121">"b.-"</span>)
|
||||
plt<span style="color: #666666">.</span>plot([bst_n_estimators, bst_n_estimators], [<span style="color: #666666">0</span>, min_error], <span style="color: #BA2121">"k--"</span>)
|
||||
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">0</span>, <span style="color: #666666">120</span>], [min_error, min_error], <span style="color: #BA2121">"k--"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(bst_n_estimators, min_error, <span style="color: #BA2121">"ko"</span>)
|
||||
plt<span style="color: #666666">.</span>text(bst_n_estimators, min_error<span style="color: #666666">*1.2</span>, <span style="color: #BA2121">"Minimum"</span>, ha<span style="color: #666666">=</span><span style="color: #BA2121">"center"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">120</span>, <span style="color: #666666">0</span>, <span style="color: #666666">0.01</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"Number of trees"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Validation error"</span>, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
|
||||
plot_predictions([gbrt_best], X, y, axes<span style="color: #666666">=</span>[<span style="color: #666666">-0.5</span>, <span style="color: #666666">0.5</span>, <span style="color: #666666">-0.1</span>, <span style="color: #666666">0.8</span>])
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"Best model (</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121"> trees)"</span> <span style="color: #666666">%</span> bst_n_estimators, fontsize<span style="color: #666666">=14</span>)
|
||||
|
||||
save_fig(<span style="color: #BA2121">"early_stopping_gbrt_plot"</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
|
||||
gbrt <span style="color: #666666">=</span> GradientBoostingRegressor(max_depth<span style="color: #666666">=2</span>, warm_start<span style="color: #666666">=</span><span style="color: #008000">True</span>, random_state<span style="color: #666666">=42</span>)
|
||||
|
||||
min_val_error <span style="color: #666666">=</span> <span style="color: #008000">float</span>(<span style="color: #BA2121">"inf"</span>)
|
||||
error_going_up <span style="color: #666666">=</span> <span style="color: #666666">0</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> n_estimators <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>, <span style="color: #666666">120</span>):
|
||||
gbrt<span style="color: #666666">.</span>n_estimators <span style="color: #666666">=</span> n_estimators
|
||||
gbrt<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
y_pred <span style="color: #666666">=</span> gbrt<span style="color: #666666">.</span>predict(X_val)
|
||||
val_error <span style="color: #666666">=</span> mean_squared_error(y_val, y_pred)
|
||||
<span style="color: #008000; font-weight: bold">if</span> val_error <span style="color: #666666"><</span> min_val_error:
|
||||
min_val_error <span style="color: #666666">=</span> val_error
|
||||
error_going_up <span style="color: #666666">=</span> <span style="color: #666666">0</span>
|
||||
<span style="color: #008000; font-weight: bold">else</span>:
|
||||
error_going_up <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #008000; font-weight: bold">if</span> error_going_up <span style="color: #666666">==</span> <span style="color: #666666">5</span>:
|
||||
<span style="color: #008000; font-weight: bold">break</span> <span style="color: #408080; font-style: italic"># early stopping</span>
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(gbrt<span style="color: #666666">.</span>n_estimators)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Minimum validation MSE:"</span>, min_val_error)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li><a href="._DecisionTrees-bs053.html">«</a></li>
|
||||
<li><a href="._DecisionTrees-bs000.html">1</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs046.html">47</a></li>
|
||||
<li><a href="._DecisionTrees-bs047.html">48</a></li>
|
||||
<li><a href="._DecisionTrees-bs048.html">49</a></li>
|
||||
<li><a href="._DecisionTrees-bs049.html">50</a></li>
|
||||
<li><a href="._DecisionTrees-bs050.html">51</a></li>
|
||||
<li><a href="._DecisionTrees-bs051.html">52</a></li>
|
||||
<li><a href="._DecisionTrees-bs052.html">53</a></li>
|
||||
<li><a href="._DecisionTrees-bs053.html">54</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs054.html">55</a></li>
|
||||
<li><a href="._DecisionTrees-bs055.html">56</a></li>
|
||||
<li><a href="._DecisionTrees-bs056.html">57</a></li>
|
||||
<li><a href="._DecisionTrees-bs057.html">58</a></li>
|
||||
<li><a href="._DecisionTrees-bs055.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="http://..."><img width="250" align=right src="http://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright only on the titlepage -->
|
||||
</center>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
<!--
|
||||
Automatically generated HTML file from DocOnce source
|
||||
(https://github.com/hplgit/doconce/)
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Data Analysis and Machine Learning: From Decision Trees to Forests and all that">
|
||||
|
||||
<title>Data Analysis and Machine Learning: From Decision Trees to Forests and all that</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
|
||||
<style type="text/css">
|
||||
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Decision trees, overarching aims', 2, None, '___sec0'),
|
||||
('A typical Decision Tree with its pertinent Jargon, '
|
||||
'Classification Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('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,
|
||||
'___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'),
|
||||
('Algorithms for Setting up Decision Trees', 2, None, '___sec16'),
|
||||
('The CART algorithm for Classification', 2, None, '___sec17'),
|
||||
('The CART algorithm for Regression', 2, None, '___sec18'),
|
||||
('Computing the Gini index', 2, None, '___sec19'),
|
||||
('Simple Python Code to read in Data and perform Classification',
|
||||
2,
|
||||
None,
|
||||
'___sec20'),
|
||||
('Computing the Gini Factor', 2, None, '___sec21'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec22'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec23'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec24'),
|
||||
('Another example, the moons again', 2, None, '___sec25'),
|
||||
('Playing around with regions', 2, None, '___sec26'),
|
||||
('Regression trees', 2, None, '___sec27'),
|
||||
('Final regressor code', 2, None, '___sec28'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec29'),
|
||||
('Disadvantages', 2, None, '___sec30'),
|
||||
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
|
||||
'Boosting, Meet the Jungle of Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec31'),
|
||||
('An Overview of Ensemble Methods', 2, None, '___sec32'),
|
||||
('Bagging', 2, None, '___sec33'),
|
||||
('More bagging', 2, None, '___sec34'),
|
||||
('Simple Voting Example, head or tail', 2, None, '___sec35'),
|
||||
('Using the Voting Classifier', 2, None, '___sec36'),
|
||||
('Please, not the moons again! Voting and Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec37'),
|
||||
('Now Bagging', 2, None, '___sec38'),
|
||||
('Making our own Bagging with Bootstrap', 2, None, '___sec39'),
|
||||
('Changing the Level of the Decision Tree', 2, None, '___sec40'),
|
||||
('Random forests', 2, None, '___sec41'),
|
||||
('Random Forest Algorithm', 2, None, '___sec42'),
|
||||
('Random Forests Compared with other Methods on the Cancer Data',
|
||||
2,
|
||||
None,
|
||||
'___sec43'),
|
||||
('Compare Bagging on Trees with Random Forests',
|
||||
2,
|
||||
None,
|
||||
'___sec44'),
|
||||
('Bootstrap with Random Forests Instead of a Single Tree, own '
|
||||
'Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec45'),
|
||||
("Boosting, a Bird'e Eye", 2, None, '___sec46'),
|
||||
('Adaptive boosting: AdaBoost, Basic Algorithm',
|
||||
2,
|
||||
None,
|
||||
'___sec47'),
|
||||
('Basic Steps of AdaBoost', 2, None, '___sec48'),
|
||||
('AdaBoost Examples', 2, None, '___sec49'),
|
||||
('Gradient boosting: Basics', 2, None, '___sec50'),
|
||||
('Gradient Boosting, algorithm', 2, None, '___sec51'),
|
||||
('Gradient Boosting, Examples', 2, None, '___sec52'),
|
||||
('Gradient Boots with Early Stopping', 2, None, '___sec53'),
|
||||
('XGBoost: Extreme Gradient Boosting', 2, None, '___sec54'),
|
||||
('Regression Case', 2, None, '___sec55'),
|
||||
('Xgboost on the Cancer Data', 2, None, '___sec56')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="DecisionTrees-bs.html">Data Analysis and Machine Learning: From Decision Trees to Forests and all that</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._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%;">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%;">Algorithms for Setting up Decision Trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">The CART algorithm for Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">The CART algorithm for Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Using the Voting Classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Please, not the moons again! Voting and Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs039.html#___sec38" style="font-size: 80%;">Now Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs040.html#___sec39" style="font-size: 80%;">Making our own Bagging with Bootstrap</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs041.html#___sec40" style="font-size: 80%;">Changing the Level of the Decision Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs042.html#___sec41" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs043.html#___sec42" style="font-size: 80%;">Random Forest Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs044.html#___sec43" style="font-size: 80%;">Random Forests Compared with other Methods on the Cancer Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs045.html#___sec44" style="font-size: 80%;">Compare Bagging on Trees with Random Forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs046.html#___sec45" style="font-size: 80%;">Bootstrap with Random Forests Instead of a Single Tree, own Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs047.html#___sec46" style="font-size: 80%;">Boosting, a Bird'e Eye</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs048.html#___sec47" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs049.html#___sec48" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs050.html#___sec49" style="font-size: 80%;">AdaBoost Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs051.html#___sec50" style="font-size: 80%;">Gradient boosting: Basics</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs052.html#___sec51" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs053.html#___sec52" style="font-size: 80%;">Gradient Boosting, Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs054.html#___sec53" style="font-size: 80%;">Gradient Boots with Early Stopping</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec54" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs056.html#___sec55" style="font-size: 80%;">Regression Case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs057.html#___sec56" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
|
||||
<div class="container">
|
||||
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0055"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec54" class="anchor">XGBoost: Extreme Gradient Boosting </h2>
|
||||
|
||||
<p>
|
||||
<a href="https://github.com/dmlc/xgboost" target="_self">XGBoost</a> or Extreme Gradient
|
||||
Boosting, is an optimized distributed gradient boosting library
|
||||
designed to be highly efficient, flexible and portable. It implements
|
||||
machine learning algorithms under the Gradient Boosting
|
||||
framework. XGBoost provides a parallel tree boosting that solve many
|
||||
data science problems in a fast and accurate way. See the <a href="https://arxiv.org/abs/1603.02754" target="_self">article by Chen and Guestrin</a>.
|
||||
|
||||
<p>
|
||||
The authors design and build a highly scalable end-to-end tree
|
||||
boosting system. It has a theoretically justified weighted quantile
|
||||
sketch for efficient proposal calculation. It introduces a novel sparsity-aware algorithm for parallel tree learning and an effective cache-aware block structure for out-of-core tree learning.
|
||||
|
||||
<p>
|
||||
It is now the algorithm which wins essentially all ML competitions!!!
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li><a href="._DecisionTrees-bs054.html">«</a></li>
|
||||
<li><a href="._DecisionTrees-bs000.html">1</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs047.html">48</a></li>
|
||||
<li><a href="._DecisionTrees-bs048.html">49</a></li>
|
||||
<li><a href="._DecisionTrees-bs049.html">50</a></li>
|
||||
<li><a href="._DecisionTrees-bs050.html">51</a></li>
|
||||
<li><a href="._DecisionTrees-bs051.html">52</a></li>
|
||||
<li><a href="._DecisionTrees-bs052.html">53</a></li>
|
||||
<li><a href="._DecisionTrees-bs053.html">54</a></li>
|
||||
<li><a href="._DecisionTrees-bs054.html">55</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs055.html">56</a></li>
|
||||
<li><a href="._DecisionTrees-bs056.html">57</a></li>
|
||||
<li><a href="._DecisionTrees-bs057.html">58</a></li>
|
||||
<li><a href="._DecisionTrees-bs056.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="http://..."><img width="250" align=right src="http://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright only on the titlepage -->
|
||||
</center>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
<!--
|
||||
Automatically generated HTML file from DocOnce source
|
||||
(https://github.com/hplgit/doconce/)
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Data Analysis and Machine Learning: From Decision Trees to Forests and all that">
|
||||
|
||||
<title>Data Analysis and Machine Learning: From Decision Trees to Forests and all that</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
|
||||
<style type="text/css">
|
||||
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Decision trees, overarching aims', 2, None, '___sec0'),
|
||||
('A typical Decision Tree with its pertinent Jargon, '
|
||||
'Classification Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('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,
|
||||
'___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'),
|
||||
('Algorithms for Setting up Decision Trees', 2, None, '___sec16'),
|
||||
('The CART algorithm for Classification', 2, None, '___sec17'),
|
||||
('The CART algorithm for Regression', 2, None, '___sec18'),
|
||||
('Computing the Gini index', 2, None, '___sec19'),
|
||||
('Simple Python Code to read in Data and perform Classification',
|
||||
2,
|
||||
None,
|
||||
'___sec20'),
|
||||
('Computing the Gini Factor', 2, None, '___sec21'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec22'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec23'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec24'),
|
||||
('Another example, the moons again', 2, None, '___sec25'),
|
||||
('Playing around with regions', 2, None, '___sec26'),
|
||||
('Regression trees', 2, None, '___sec27'),
|
||||
('Final regressor code', 2, None, '___sec28'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec29'),
|
||||
('Disadvantages', 2, None, '___sec30'),
|
||||
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
|
||||
'Boosting, Meet the Jungle of Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec31'),
|
||||
('An Overview of Ensemble Methods', 2, None, '___sec32'),
|
||||
('Bagging', 2, None, '___sec33'),
|
||||
('More bagging', 2, None, '___sec34'),
|
||||
('Simple Voting Example, head or tail', 2, None, '___sec35'),
|
||||
('Using the Voting Classifier', 2, None, '___sec36'),
|
||||
('Please, not the moons again! Voting and Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec37'),
|
||||
('Now Bagging', 2, None, '___sec38'),
|
||||
('Making our own Bagging with Bootstrap', 2, None, '___sec39'),
|
||||
('Changing the Level of the Decision Tree', 2, None, '___sec40'),
|
||||
('Random forests', 2, None, '___sec41'),
|
||||
('Random Forest Algorithm', 2, None, '___sec42'),
|
||||
('Random Forests Compared with other Methods on the Cancer Data',
|
||||
2,
|
||||
None,
|
||||
'___sec43'),
|
||||
('Compare Bagging on Trees with Random Forests',
|
||||
2,
|
||||
None,
|
||||
'___sec44'),
|
||||
('Bootstrap with Random Forests Instead of a Single Tree, own '
|
||||
'Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec45'),
|
||||
("Boosting, a Bird'e Eye", 2, None, '___sec46'),
|
||||
('Adaptive boosting: AdaBoost, Basic Algorithm',
|
||||
2,
|
||||
None,
|
||||
'___sec47'),
|
||||
('Basic Steps of AdaBoost', 2, None, '___sec48'),
|
||||
('AdaBoost Examples', 2, None, '___sec49'),
|
||||
('Gradient boosting: Basics', 2, None, '___sec50'),
|
||||
('Gradient Boosting, algorithm', 2, None, '___sec51'),
|
||||
('Gradient Boosting, Examples', 2, None, '___sec52'),
|
||||
('Gradient Boots with Early Stopping', 2, None, '___sec53'),
|
||||
('XGBoost: Extreme Gradient Boosting', 2, None, '___sec54'),
|
||||
('Regression Case', 2, None, '___sec55'),
|
||||
('Xgboost on the Cancer Data', 2, None, '___sec56')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="DecisionTrees-bs.html">Data Analysis and Machine Learning: From Decision Trees to Forests and all that</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._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%;">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%;">Algorithms for Setting up Decision Trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">The CART algorithm for Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">The CART algorithm for Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Using the Voting Classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Please, not the moons again! Voting and Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs039.html#___sec38" style="font-size: 80%;">Now Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs040.html#___sec39" style="font-size: 80%;">Making our own Bagging with Bootstrap</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs041.html#___sec40" style="font-size: 80%;">Changing the Level of the Decision Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs042.html#___sec41" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs043.html#___sec42" style="font-size: 80%;">Random Forest Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs044.html#___sec43" style="font-size: 80%;">Random Forests Compared with other Methods on the Cancer Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs045.html#___sec44" style="font-size: 80%;">Compare Bagging on Trees with Random Forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs046.html#___sec45" style="font-size: 80%;">Bootstrap with Random Forests Instead of a Single Tree, own Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs047.html#___sec46" style="font-size: 80%;">Boosting, a Bird'e Eye</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs048.html#___sec47" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs049.html#___sec48" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs050.html#___sec49" style="font-size: 80%;">AdaBoost Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs051.html#___sec50" style="font-size: 80%;">Gradient boosting: Basics</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs052.html#___sec51" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs053.html#___sec52" style="font-size: 80%;">Gradient Boosting, Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs054.html#___sec53" style="font-size: 80%;">Gradient Boots with Early Stopping</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs055.html#___sec54" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec55" style="font-size: 80%;">Regression Case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs057.html#___sec56" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
|
||||
<div class="container">
|
||||
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0056"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec55" class="anchor">Regression Case </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>
|
||||
<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">import</span> <span style="color: #0000FF; font-weight: bold">xgboost</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">xgb</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
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</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> mean_squared_error
|
||||
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">6</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Make data set.</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(<span style="color: #666666">0</span>, <span style="color: #666666">0.1</span>, x<span style="color: #666666">.</span>shape)
|
||||
|
||||
error <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
|
||||
bias <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
|
||||
variance <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
|
||||
polydegree <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(x, y, test_size<span style="color: #666666">=0.2</span>)
|
||||
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: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(maxdegree):
|
||||
model <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBRegressor(objective <span style="color: #666666">=</span><span style="color: #BA2121">'reg:squarederror'</span>, colsample_bytree <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>, learning_rate <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>,
|
||||
max_depth <span style="color: #666666">=</span> degree, alpha <span style="color: #666666">=</span> <span style="color: #666666">10</span>, n_estimators <span style="color: #666666">=</span> <span style="color: #666666">10</span>)
|
||||
model<span style="color: #666666">.</span>fit(X_train_scaled,y_train)
|
||||
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test_scaled)
|
||||
polydegree[degree] <span style="color: #666666">=</span> degree
|
||||
error[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> y_pred)<span style="color: #666666">**2</span>) )
|
||||
bias[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( (y_test <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_pred))<span style="color: #666666">**2</span> )
|
||||
variance[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>var(y_pred) )
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Max depth:'</span>, degree)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Error:'</span>, error[degree])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Bias^2:'</span>, bias[degree])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Var:'</span>, variance[degree])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'{} >= {} + {} = {}'</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
|
||||
|
||||
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdegree<span style="color: #666666">-1</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">'Error'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">'bias'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">'Variance'</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 -->
|
||||
<ul class="pagination">
|
||||
<li><a href="._DecisionTrees-bs055.html">«</a></li>
|
||||
<li><a href="._DecisionTrees-bs000.html">1</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs048.html">49</a></li>
|
||||
<li><a href="._DecisionTrees-bs049.html">50</a></li>
|
||||
<li><a href="._DecisionTrees-bs050.html">51</a></li>
|
||||
<li><a href="._DecisionTrees-bs051.html">52</a></li>
|
||||
<li><a href="._DecisionTrees-bs052.html">53</a></li>
|
||||
<li><a href="._DecisionTrees-bs053.html">54</a></li>
|
||||
<li><a href="._DecisionTrees-bs054.html">55</a></li>
|
||||
<li><a href="._DecisionTrees-bs055.html">56</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs056.html">57</a></li>
|
||||
<li><a href="._DecisionTrees-bs057.html">58</a></li>
|
||||
<li><a href="._DecisionTrees-bs057.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="http://..."><img width="250" align=right src="http://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright only on the titlepage -->
|
||||
</center>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
<!--
|
||||
Automatically generated HTML file from DocOnce source
|
||||
(https://github.com/hplgit/doconce/)
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Data Analysis and Machine Learning: From Decision Trees to Forests and all that">
|
||||
|
||||
<title>Data Analysis and Machine Learning: From Decision Trees to Forests and all that</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
|
||||
<style type="text/css">
|
||||
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Decision trees, overarching aims', 2, None, '___sec0'),
|
||||
('A typical Decision Tree with its pertinent Jargon, '
|
||||
'Classification Problem',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('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,
|
||||
'___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'),
|
||||
('Algorithms for Setting up Decision Trees', 2, None, '___sec16'),
|
||||
('The CART algorithm for Classification', 2, None, '___sec17'),
|
||||
('The CART algorithm for Regression', 2, None, '___sec18'),
|
||||
('Computing the Gini index', 2, None, '___sec19'),
|
||||
('Simple Python Code to read in Data and perform Classification',
|
||||
2,
|
||||
None,
|
||||
'___sec20'),
|
||||
('Computing the Gini Factor', 2, None, '___sec21'),
|
||||
('Entropy and the ID3 algorithm', 2, None, '___sec22'),
|
||||
('Implementing the ID3 Algorithm', 2, None, '___sec23'),
|
||||
('Cancer Data again now with Decision Trees and other Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec24'),
|
||||
('Another example, the moons again', 2, None, '___sec25'),
|
||||
('Playing around with regions', 2, None, '___sec26'),
|
||||
('Regression trees', 2, None, '___sec27'),
|
||||
('Final regressor code', 2, None, '___sec28'),
|
||||
('Pros and cons of trees, pros', 2, None, '___sec29'),
|
||||
('Disadvantages', 2, None, '___sec30'),
|
||||
('Ensemble Methods: From a Single Tree to Many Trees and Extreme '
|
||||
'Boosting, Meet the Jungle of Methods',
|
||||
2,
|
||||
None,
|
||||
'___sec31'),
|
||||
('An Overview of Ensemble Methods', 2, None, '___sec32'),
|
||||
('Bagging', 2, None, '___sec33'),
|
||||
('More bagging', 2, None, '___sec34'),
|
||||
('Simple Voting Example, head or tail', 2, None, '___sec35'),
|
||||
('Using the Voting Classifier', 2, None, '___sec36'),
|
||||
('Please, not the moons again! Voting and Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec37'),
|
||||
('Now Bagging', 2, None, '___sec38'),
|
||||
('Making our own Bagging with Bootstrap', 2, None, '___sec39'),
|
||||
('Changing the Level of the Decision Tree', 2, None, '___sec40'),
|
||||
('Random forests', 2, None, '___sec41'),
|
||||
('Random Forest Algorithm', 2, None, '___sec42'),
|
||||
('Random Forests Compared with other Methods on the Cancer Data',
|
||||
2,
|
||||
None,
|
||||
'___sec43'),
|
||||
('Compare Bagging on Trees with Random Forests',
|
||||
2,
|
||||
None,
|
||||
'___sec44'),
|
||||
('Bootstrap with Random Forests Instead of a Single Tree, own '
|
||||
'Bagging',
|
||||
2,
|
||||
None,
|
||||
'___sec45'),
|
||||
("Boosting, a Bird'e Eye", 2, None, '___sec46'),
|
||||
('Adaptive boosting: AdaBoost, Basic Algorithm',
|
||||
2,
|
||||
None,
|
||||
'___sec47'),
|
||||
('Basic Steps of AdaBoost', 2, None, '___sec48'),
|
||||
('AdaBoost Examples', 2, None, '___sec49'),
|
||||
('Gradient boosting: Basics', 2, None, '___sec50'),
|
||||
('Gradient Boosting, algorithm', 2, None, '___sec51'),
|
||||
('Gradient Boosting, Examples', 2, None, '___sec52'),
|
||||
('Gradient Boots with Early Stopping', 2, None, '___sec53'),
|
||||
('XGBoost: Extreme Gradient Boosting', 2, None, '___sec54'),
|
||||
('Regression Case', 2, None, '___sec55'),
|
||||
('Xgboost on the Cancer Data', 2, None, '___sec56')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="DecisionTrees-bs.html">Data Analysis and Machine Learning: From Decision Trees to Forests and all that</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._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%;">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%;">Algorithms for Setting up Decision Trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs018.html#___sec17" style="font-size: 80%;">The CART algorithm for Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs019.html#___sec18" style="font-size: 80%;">The CART algorithm for Regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini index</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs021.html#___sec20" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs022.html#___sec21" style="font-size: 80%;">Computing the Gini Factor</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs023.html#___sec22" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs024.html#___sec23" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs025.html#___sec24" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs026.html#___sec25" style="font-size: 80%;">Another example, the moons again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs027.html#___sec26" style="font-size: 80%;">Playing around with regions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs028.html#___sec27" style="font-size: 80%;">Regression trees</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs029.html#___sec28" style="font-size: 80%;">Final regressor code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs030.html#___sec29" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs031.html#___sec30" style="font-size: 80%;">Disadvantages</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs032.html#___sec31" style="font-size: 80%;">Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs033.html#___sec32" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs034.html#___sec33" style="font-size: 80%;">Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs035.html#___sec34" style="font-size: 80%;">More bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs036.html#___sec35" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs037.html#___sec36" style="font-size: 80%;">Using the Voting Classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs038.html#___sec37" style="font-size: 80%;">Please, not the moons again! Voting and Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs039.html#___sec38" style="font-size: 80%;">Now Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs040.html#___sec39" style="font-size: 80%;">Making our own Bagging with Bootstrap</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs041.html#___sec40" style="font-size: 80%;">Changing the Level of the Decision Tree</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs042.html#___sec41" style="font-size: 80%;">Random forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs043.html#___sec42" style="font-size: 80%;">Random Forest Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs044.html#___sec43" style="font-size: 80%;">Random Forests Compared with other Methods on the Cancer Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs045.html#___sec44" style="font-size: 80%;">Compare Bagging on Trees with Random Forests</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs046.html#___sec45" style="font-size: 80%;">Bootstrap with Random Forests Instead of a Single Tree, own Bagging</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs047.html#___sec46" style="font-size: 80%;">Boosting, a Bird'e Eye</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs048.html#___sec47" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs049.html#___sec48" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs050.html#___sec49" style="font-size: 80%;">AdaBoost Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs051.html#___sec50" style="font-size: 80%;">Gradient boosting: Basics</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs052.html#___sec51" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs053.html#___sec52" style="font-size: 80%;">Gradient Boosting, Examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs054.html#___sec53" style="font-size: 80%;">Gradient Boots with Early Stopping</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs055.html#___sec54" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DecisionTrees-bs056.html#___sec55" style="font-size: 80%;">Regression Case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec56" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
|
||||
<div class="container">
|
||||
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0057"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec56" class="anchor">Xgboost on the Cancer Data </h2>
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">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.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: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">xgboost</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">xgb</span>
|
||||
<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">#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)
|
||||
|
||||
xg_clf <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBClassifier()
|
||||
xg_clf<span style="color: #666666">.</span>fit(X_train_scaled,y_train)
|
||||
xgb<span style="color: #666666">.</span>plot_tree(xg_clf,num_trees<span style="color: #666666">=0</span>)
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'figure.figsize'</span>] <span style="color: #666666">=</span> [<span style="color: #666666">50</span>, <span style="color: #666666">10</span>]
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
xgb<span style="color: #666666">.</span>plot_importance(xg_clf)
|
||||
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">'figure.figsize'</span>] <span style="color: #666666">=</span> [<span style="color: #666666">5</span>, <span style="color: #666666">5</span>]
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li><a href="._DecisionTrees-bs056.html">«</a></li>
|
||||
<li><a href="._DecisionTrees-bs000.html">1</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DecisionTrees-bs049.html">50</a></li>
|
||||
<li><a href="._DecisionTrees-bs050.html">51</a></li>
|
||||
<li><a href="._DecisionTrees-bs051.html">52</a></li>
|
||||
<li><a href="._DecisionTrees-bs052.html">53</a></li>
|
||||
<li><a href="._DecisionTrees-bs053.html">54</a></li>
|
||||
<li><a href="._DecisionTrees-bs054.html">55</a></li>
|
||||
<li><a href="._DecisionTrees-bs055.html">56</a></li>
|
||||
<li><a href="._DecisionTrees-bs056.html">57</a></li>
|
||||
<li class="active"><a href="._DecisionTrees-bs057.html">58</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="http://..."><img width="250" align=right src="http://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright only on the titlepage -->
|
||||
</center>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
@@ -2431,9 +2431,8 @@ It is now the algorithm which wins essentially all ML competitions!!!
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
|
||||
|
||||
n = <span style="color: #B452CD">40</span>
|
||||
n_boostraps = <span style="color: #B452CD">100</span>
|
||||
maxdegree = <span style="color: #B452CD">8</span>
|
||||
n = <span style="color: #B452CD">100</span>
|
||||
maxdegree = <span style="color: #B452CD">6</span>
|
||||
|
||||
<span style="color: #228B22"># Make data set.</span>
|
||||
x = np.linspace(-<span style="color: #B452CD">3</span>, <span style="color: #B452CD">3</span>, n).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
||||
@@ -2450,8 +2449,8 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
|
||||
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">'reg:linear'</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,
|
||||
max_depth = maxdegree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">10</span>)
|
||||
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">'reg:squarederror'</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,
|
||||
max_depth = degree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">10</span>)
|
||||
model.fit(X_train_scaled,y_train)
|
||||
y_pred = model.predict(X_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -2464,6 +2463,7 @@ X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Var:'</span>, variance[degree])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'{} >= {} + {} = {}'</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
||||
|
||||
plt.xlim(<span style="color: #B452CD">1</span>,maxdegree-<span style="color: #B452CD">1</span>)
|
||||
plt.plot(polydegree, error, label=<span style="color: #CD5555">'Error'</span>)
|
||||
plt.plot(polydegree, bias, label=<span style="color: #CD5555">'bias'</span>)
|
||||
plt.plot(polydegree, variance, label=<span style="color: #CD5555">'Variance'</span>)
|
||||
|
||||
@@ -2410,9 +2410,8 @@ It is now the algorithm which wins essentially all ML competitions!!!
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
|
||||
|
||||
n = <span style="color: #B452CD">40</span>
|
||||
n_boostraps = <span style="color: #B452CD">100</span>
|
||||
maxdegree = <span style="color: #B452CD">8</span>
|
||||
n = <span style="color: #B452CD">100</span>
|
||||
maxdegree = <span style="color: #B452CD">6</span>
|
||||
|
||||
<span style="color: #228B22"># Make data set.</span>
|
||||
x = np.linspace(-<span style="color: #B452CD">3</span>, <span style="color: #B452CD">3</span>, n).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
|
||||
@@ -2429,8 +2428,8 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
|
||||
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">'reg:linear'</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,
|
||||
max_depth = maxdegree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">10</span>)
|
||||
model = xgb.XGBRegressor(objective =<span style="color: #CD5555">'reg:squarederror'</span>, colsample_bytree = <span style="color: #B452CD">0.3</span>, learning_rate = <span style="color: #B452CD">0.1</span>,
|
||||
max_depth = degree, alpha = <span style="color: #B452CD">10</span>, n_estimators = <span style="color: #B452CD">10</span>)
|
||||
model.fit(X_train_scaled,y_train)
|
||||
y_pred = model.predict(X_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -2443,6 +2442,7 @@ X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Var:'</span>, variance[degree])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'{} >= {} + {} = {}'</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
||||
|
||||
plt.xlim(<span style="color: #B452CD">1</span>,maxdegree-<span style="color: #B452CD">1</span>)
|
||||
plt.plot(polydegree, error, label=<span style="color: #CD5555">'Error'</span>)
|
||||
plt.plot(polydegree, bias, label=<span style="color: #CD5555">'bias'</span>)
|
||||
plt.plot(polydegree, variance, label=<span style="color: #CD5555">'Variance'</span>)
|
||||
|
||||
@@ -2415,9 +2415,8 @@ It is now the algorithm which wins essentially all ML competitions!!!
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</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> mean_squared_error
|
||||
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">40</span>
|
||||
n_boostraps <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">8</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">6</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Make data set.</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
|
||||
@@ -2434,8 +2433,8 @@ X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(maxdegree):
|
||||
model <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBRegressor(objective <span style="color: #666666">=</span><span style="color: #BA2121">'reg:linear'</span>, colsample_bytree <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>, learning_rate <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>,
|
||||
max_depth <span style="color: #666666">=</span> maxdegree, alpha <span style="color: #666666">=</span> <span style="color: #666666">10</span>, n_estimators <span style="color: #666666">=</span> <span style="color: #666666">10</span>)
|
||||
model <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBRegressor(objective <span style="color: #666666">=</span><span style="color: #BA2121">'reg:squarederror'</span>, colsample_bytree <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>, learning_rate <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>,
|
||||
max_depth <span style="color: #666666">=</span> degree, alpha <span style="color: #666666">=</span> <span style="color: #666666">10</span>, n_estimators <span style="color: #666666">=</span> <span style="color: #666666">10</span>)
|
||||
model<span style="color: #666666">.</span>fit(X_train_scaled,y_train)
|
||||
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test_scaled)
|
||||
polydegree[degree] <span style="color: #666666">=</span> degree
|
||||
@@ -2448,6 +2447,7 @@ X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #6
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Var:'</span>, variance[degree])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'{} >= {} + {} = {}'</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
|
||||
|
||||
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdegree<span style="color: #666666">-1</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">'Error'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">'bias'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">'Variance'</span>)
|
||||
|
||||
@@ -2509,9 +2509,8 @@
|
||||
"import scikitplot as skplt\n",
|
||||
"from sklearn.metrics import mean_squared_error\n",
|
||||
"\n",
|
||||
"n = 40\n",
|
||||
"n_boostraps = 100\n",
|
||||
"maxdegree = 8\n",
|
||||
"n = 100\n",
|
||||
"maxdegree = 6\n",
|
||||
"\n",
|
||||
"# Make data set.\n",
|
||||
"x = np.linspace(-3, 3, n).reshape(-1, 1)\n",
|
||||
@@ -2528,8 +2527,8 @@
|
||||
"X_test_scaled = scaler.transform(X_test)\n",
|
||||
"\n",
|
||||
"for degree in range(maxdegree):\n",
|
||||
" model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,\n",
|
||||
" max_depth = maxdegree, alpha = 10, n_estimators = 10)\n",
|
||||
" model = xgb.XGBRegressor(objective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,\n",
|
||||
" max_depth = degree, alpha = 10, n_estimators = 10)\n",
|
||||
" model.fit(X_train_scaled,y_train)\n",
|
||||
" y_pred = model.predict(X_test_scaled)\n",
|
||||
" polydegree[degree] = degree\n",
|
||||
@@ -2542,6 +2541,7 @@
|
||||
" print('Var:', variance[degree])\n",
|
||||
" print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))\n",
|
||||
"\n",
|
||||
"plt.xlim(1,maxdegree-1)\n",
|
||||
"plt.plot(polydegree, error, label='Error')\n",
|
||||
"plt.plot(polydegree, bias, label='bias')\n",
|
||||
"plt.plot(polydegree, variance, label='Variance')\n",
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -2038,9 +2038,8 @@ from sklearn.preprocessing import StandardScaler
|
||||
import scikitplot as skplt
|
||||
from sklearn.metrics import mean_squared_error
|
||||
|
||||
n = 40
|
||||
n_boostraps = 100
|
||||
maxdegree = 8
|
||||
n = 100
|
||||
maxdegree = 6
|
||||
|
||||
# Make data set.
|
||||
x = np.linspace(-3, 3, n).reshape(-1, 1)
|
||||
@@ -2057,8 +2056,8 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
for degree in range(maxdegree):
|
||||
model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
|
||||
max_depth = maxdegree, alpha = 10, n_estimators = 10)
|
||||
model = xgb.XGBRegressor(objective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,
|
||||
max_depth = degree, alpha = 10, n_estimators = 10)
|
||||
model.fit(X_train_scaled,y_train)
|
||||
y_pred = model.predict(X_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -2071,6 +2070,7 @@ for degree in range(maxdegree):
|
||||
print('Var:', variance[degree])
|
||||
print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
||||
|
||||
plt.xlim(1,maxdegree-1)
|
||||
plt.plot(polydegree, error, label='Error')
|
||||
plt.plot(polydegree, bias, label='bias')
|
||||
plt.plot(polydegree, variance, label='Variance')
|
||||
|
||||
@@ -6,8 +6,7 @@ from sklearn.preprocessing import StandardScaler
|
||||
import scikitplot as skplt
|
||||
from sklearn.metrics import mean_squared_error
|
||||
|
||||
n = 40
|
||||
n_boostraps = 100
|
||||
n = 500
|
||||
maxdegree = 8
|
||||
|
||||
# Make data set.
|
||||
@@ -25,8 +24,8 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
for degree in range(maxdegree):
|
||||
model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
|
||||
max_depth = maxdegree, alpha = 10, n_estimators = 10)
|
||||
model = xgb.XGBRegressor(objective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,
|
||||
max_depth = degree, alpha = 10, n_estimators = 10)
|
||||
model.fit(X_train_scaled,y_train)
|
||||
y_pred = model.predict(X_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -39,6 +38,7 @@ for degree in range(maxdegree):
|
||||
print('Var:', variance[degree])
|
||||
print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
||||
|
||||
plt.xlim(1,maxdegree-1)
|
||||
plt.plot(polydegree, error, label='Error')
|
||||
plt.plot(polydegree, bias, label='bias')
|
||||
plt.plot(polydegree, variance, label='Variance')
|
||||
|
||||
Reference in New Issue
Block a user