Files
FYS-STK4155/doc/pub/week44/html/._week44-bs024.html
T
2020-09-16 18:51:56 +02:00

445 lines
37 KiB
HTML

<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="week 44: From Decision Trees to Bagging methods">
<title>week 44: From Decision Trees to Bagging methods</title>
<!-- 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'),
('Bagging Examples', 2, None, '___sec38'),
('Making your own Bootstrap: Changing the Level of the Decision '
'Tree',
2,
None,
'___sec39')]}
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="week44-bs.html">week 44: From Decision Trees to Bagging methods</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="._week44-bs001.html#___sec0" style="font-size: 80%;">Decision trees, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs002.html#___sec1" style="font-size: 80%;">A typical Decision Tree with its pertinent Jargon, Classification Problem</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs003.html#___sec2" style="font-size: 80%;">General Features</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs004.html#___sec3" style="font-size: 80%;">How do we set it up?</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs005.html#___sec4" style="font-size: 80%;">Decision trees and Regression</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs006.html#___sec5" style="font-size: 80%;">Building a tree, regression</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs007.html#___sec6" style="font-size: 80%;">A top-down approach, recursive binary splitting</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs008.html#___sec7" style="font-size: 80%;">Making a tree</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs009.html#___sec8" style="font-size: 80%;">Pruning the tree</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs010.html#___sec9" style="font-size: 80%;">Cost complexity pruning</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs011.html#___sec10" style="font-size: 80%;">Schematic Regression Procedure</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs012.html#___sec11" style="font-size: 80%;">A Classification Tree</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs013.html#___sec12" style="font-size: 80%;">Growing a classification tree</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs014.html#___sec13" style="font-size: 80%;">Classification tree, how to split nodes</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs015.html#___sec14" style="font-size: 80%;">Visualizing the Tree, Classification</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs016.html#___sec15" style="font-size: 80%;">Visualizing the Tree, The Moons</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs017.html#___sec16" style="font-size: 80%;">Algorithms for Setting up Decision Trees</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs018.html#___sec17" style="font-size: 80%;">The CART algorithm for Classification</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs019.html#___sec18" style="font-size: 80%;">The CART algorithm for Regression</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs020.html#___sec19" style="font-size: 80%;">Computing the Gini index</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs021.html#___sec20" style="font-size: 80%;">Simple Python Code to read in Data and perform Classification</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs022.html#___sec21" style="font-size: 80%;">Computing the Gini Factor</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs023.html#___sec22" style="font-size: 80%;">Entropy and the ID3 algorithm</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">Implementing the ID3 Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs025.html#___sec24" style="font-size: 80%;">Cancer Data again now with Decision Trees and other Methods</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs026.html#___sec25" style="font-size: 80%;">Another example, the moons again</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs027.html#___sec26" style="font-size: 80%;">Playing around with regions</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs028.html#___sec27" style="font-size: 80%;">Regression trees</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs029.html#___sec28" style="font-size: 80%;">Final regressor code</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs030.html#___sec29" style="font-size: 80%;">Pros and cons of trees, pros</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs031.html#___sec30" style="font-size: 80%;">Disadvantages</a></li>
<!-- navigation toc: --> <li><a href="._week44-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="._week44-bs033.html#___sec32" style="font-size: 80%;">An Overview of Ensemble Methods</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs034.html#___sec33" style="font-size: 80%;">Bagging</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs035.html#___sec34" style="font-size: 80%;">More bagging</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs036.html#___sec35" style="font-size: 80%;">Simple Voting Example, head or tail</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs037.html#___sec36" style="font-size: 80%;">Using the Voting Classifier</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs038.html#___sec37" style="font-size: 80%;">Please, not the moons again! Voting and Bagging</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs039.html#___sec38" style="font-size: 80%;">Bagging Examples</a></li>
<!-- navigation toc: --> <li><a href="._week44-bs040.html#___sec39" style="font-size: 80%;">Making your own Bootstrap: Changing the Level of the Decision Tree</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0024"></a>
<!-- !split -->
<h2 id="___sec23" class="anchor">Implementing the ID3 Algorithm </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">re</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">math</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">collections</span> <span style="color: #008000; font-weight: bold">import</span> deque
<span style="color: #408080; font-style: italic"># x is examples in training set</span>
<span style="color: #408080; font-style: italic"># y is set of targets</span>
<span style="color: #408080; font-style: italic"># label is target attributes</span>
<span style="color: #408080; font-style: italic"># Node is a class which has properties values, childs, and next</span>
<span style="color: #408080; font-style: italic"># root is top node in the decision tree</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Node</span>(<span style="color: #008000">object</span>):
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>childs <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #408080; font-style: italic"># Simple class of Decision Tree</span>
<span style="color: #408080; font-style: italic"># Aimed for who want to learn Decision Tree, so it is not optimized</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">DecisionTree</span>(<span style="color: #008000">object</span>):
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, sample, attributes, labels):
<span style="color: #008000">self</span><span style="color: #666666">.</span>sample <span style="color: #666666">=</span> sample
<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes <span style="color: #666666">=</span> attributes
<span style="color: #008000">self</span><span style="color: #666666">.</span>labels <span style="color: #666666">=</span> labels
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>initLabelCodes()
<span style="color: #408080; font-style: italic"># print(self.labelCodes)</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">None</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>entropy <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy([x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels))])
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">initLabelCodes</span>(<span style="color: #008000">self</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes <span style="color: #666666">=</span> []
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels:
<span style="color: #008000; font-weight: bold">if</span> l <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes:
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>append(l)
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(l)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabelCodeId</span>(<span style="color: #008000">self</span>, sampleId):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId])
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeValues</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
vals <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> vals:
vals<span style="color: #666666">.</span>append(val)
<span style="color: #408080; font-style: italic"># print(vals)</span>
<span style="color: #008000; font-weight: bold">return</span> vals
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getEntropy</span>(<span style="color: #008000">self</span>, sampleIds):
entropy <span style="color: #666666">=</span> <span style="color: #666666">0</span>
labelCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
labelCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>getLabelCodeId(sid)] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
<span style="color: #408080; font-style: italic"># print(&quot;-ge&quot;, labelCount)</span>
<span style="color: #008000; font-weight: bold">for</span> lv <span style="color: #AA22FF; font-weight: bold">in</span> labelCount:
<span style="color: #408080; font-style: italic"># print(lv)</span>
<span style="color: #008000; font-weight: bold">if</span> lv <span style="color: #666666">!=</span> <span style="color: #666666">0</span>:
entropy <span style="color: #666666">+=</span> <span style="color: #666666">-</span>lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> math<span style="color: #666666">.</span>log(lv<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds), <span style="color: #666666">2</span>)
<span style="color: #008000; font-weight: bold">else</span>:
entropy <span style="color: #666666">+=</span> <span style="color: #666666">0</span>
<span style="color: #008000; font-weight: bold">return</span> entropy
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getDominantLabel</span>(<span style="color: #008000">self</span>, sampleIds):
labelCodesCount <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes)
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
labelCodesCount[<span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes<span style="color: #666666">.</span>index(<span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid])] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labelCodes[labelCodesCount<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(labelCodesCount))]
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeId):
gain <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(sampleIds)
attributeVals <span style="color: #666666">=</span> []
attributeValsCount <span style="color: #666666">=</span> []
attributeValsIds <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
val <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][attributeId]
<span style="color: #008000; font-weight: bold">if</span> val <span style="color: #AA22FF; font-weight: bold">not</span> <span style="color: #AA22FF; font-weight: bold">in</span> attributeVals:
attributeVals<span style="color: #666666">.</span>append(val)
attributeValsCount<span style="color: #666666">.</span>append(<span style="color: #666666">0</span>)
attributeValsIds<span style="color: #666666">.</span>append([])
vid <span style="color: #666666">=</span> attributeVals<span style="color: #666666">.</span>index(val)
attributeValsCount[vid] <span style="color: #666666">+=</span> <span style="color: #666666">1</span>
attributeValsIds[vid]<span style="color: #666666">.</span>append(sid)
<span style="color: #408080; font-style: italic"># print(&quot;-gig&quot;, self.attributes[attributeId])</span>
<span style="color: #008000; font-weight: bold">for</span> vc, vids <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(attributeValsCount, attributeValsIds):
<span style="color: #408080; font-style: italic"># print(&quot;-gig&quot;, vids)</span>
gain <span style="color: #666666">-=</span> vc<span style="color: #666666">/</span><span style="color: #008000">len</span>(sampleIds) <span style="color: #666666">*</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getEntropy(vids)
<span style="color: #008000; font-weight: bold">return</span> gain
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getAttributeMaxInformationGain</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds):
attributesEntropy <span style="color: #666666">=</span> [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(attributeIds)
<span style="color: #008000; font-weight: bold">for</span> i, attId <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(<span style="color: #008000">range</span>(<span style="color: #008000">len</span>(attributeIds)), attributeIds):
attributesEntropy[i] <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getInformationGain(sampleIds, attId)
maxId <span style="color: #666666">=</span> attributeIds[attributesEntropy<span style="color: #666666">.</span>index(<span style="color: #008000">max</span>(attributesEntropy))]
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>attributes[maxId], maxId
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">isSingleLabeled</span>(<span style="color: #008000">self</span>, sampleIds):
label <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sid] <span style="color: #666666">!=</span> label:
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">False</span>
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">True</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">getLabel</span>(<span style="color: #008000">self</span>, sampleId):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleId]
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3</span>(<span style="color: #008000">self</span>):
sampleIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>sample))]
attributeIds <span style="color: #666666">=</span> [x <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>attributes))]
<span style="color: #008000">self</span><span style="color: #666666">.</span>root <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(sampleIds, attributeIds, <span style="color: #008000">self</span><span style="color: #666666">.</span>root)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">id3Recv</span>(<span style="color: #008000">self</span>, sampleIds, attributeIds, root):
root <span style="color: #666666">=</span> Node() <span style="color: #408080; font-style: italic"># Initialize current root</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>isSingleLabeled(sampleIds):
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>labels[sampleIds[<span style="color: #666666">0</span>]]
<span style="color: #008000; font-weight: bold">return</span> root
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
<span style="color: #008000; font-weight: bold">return</span> root
bestAttrName, bestAttrId <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeMaxInformationGain(
sampleIds, attributeIds)
<span style="color: #408080; font-style: italic"># print(bestAttrName)</span>
root<span style="color: #666666">.</span>value <span style="color: #666666">=</span> bestAttrName
root<span style="color: #666666">.</span>childs <span style="color: #666666">=</span> [] <span style="color: #408080; font-style: italic"># Create list of children</span>
<span style="color: #008000; font-weight: bold">for</span> value <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getAttributeValues(sampleIds, bestAttrId):
<span style="color: #408080; font-style: italic"># print(value)</span>
child <span style="color: #666666">=</span> Node()
child<span style="color: #666666">.</span>value <span style="color: #666666">=</span> value
root<span style="color: #666666">.</span>childs<span style="color: #666666">.</span>append(child) <span style="color: #408080; font-style: italic"># Append new child node to current</span>
<span style="color: #408080; font-style: italic"># root</span>
childSampleIds <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> sid <span style="color: #AA22FF; font-weight: bold">in</span> sampleIds:
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>sample[sid][bestAttrId] <span style="color: #666666">==</span> value:
childSampleIds<span style="color: #666666">.</span>append(sid)
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(childSampleIds) <span style="color: #666666">==</span> <span style="color: #666666">0</span>:
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>getDominantLabel(sampleIds)
<span style="color: #008000; font-weight: bold">else</span>:
<span style="color: #408080; font-style: italic"># print(bestAttrName, bestAttrId)</span>
<span style="color: #408080; font-style: italic"># print(attributeIds)</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(attributeIds) <span style="color: #666666">&gt;</span> <span style="color: #666666">0</span> <span style="color: #AA22FF; font-weight: bold">and</span> bestAttrId <span style="color: #AA22FF; font-weight: bold">in</span> attributeIds:
toRemove <span style="color: #666666">=</span> attributeIds<span style="color: #666666">.</span>index(bestAttrId)
attributeIds<span style="color: #666666">.</span>pop(toRemove)
child<span style="color: #666666">.</span>next <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>id3Recv(
childSampleIds, attributeIds, child<span style="color: #666666">.</span>next)
<span style="color: #008000; font-weight: bold">return</span> root
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">printTree</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>root:
roots <span style="color: #666666">=</span> deque()
roots<span style="color: #666666">.</span>append(<span style="color: #008000">self</span><span style="color: #666666">.</span>root)
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #008000">len</span>(roots) <span style="color: #666666">&gt;</span> <span style="color: #666666">0</span>:
root <span style="color: #666666">=</span> roots<span style="color: #666666">.</span>popleft()
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>value)
<span style="color: #008000; font-weight: bold">if</span> root<span style="color: #666666">.</span>childs:
<span style="color: #008000; font-weight: bold">for</span> child <span style="color: #AA22FF; font-weight: bold">in</span> root<span style="color: #666666">.</span>childs:
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;({})&#39;</span><span style="color: #666666">.</span>format(child<span style="color: #666666">.</span>value))
roots<span style="color: #666666">.</span>append(child<span style="color: #666666">.</span>next)
<span style="color: #008000; font-weight: bold">elif</span> root<span style="color: #666666">.</span>next:
<span style="color: #008000; font-weight: bold">print</span>(root<span style="color: #666666">.</span>next)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test</span>():
f <span style="color: #666666">=</span> <span style="color: #008000">open</span>(<span style="color: #BA2121">&#39;DataFiles/rideclass.csv&#39;</span>)
attributes <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readline()<span style="color: #666666">.</span>split(<span style="color: #BA2121">&#39;,&#39;</span>)
attributes <span style="color: #666666">=</span> attributes[<span style="color: #666666">1</span>:<span style="color: #008000">len</span>(attributes)<span style="color: #666666">-1</span>]
<span style="color: #008000; font-weight: bold">print</span>(attributes)
sample <span style="color: #666666">=</span> f<span style="color: #666666">.</span>readlines()
f<span style="color: #666666">.</span>close()
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(sample)):
sample[i] <span style="color: #666666">=</span> re<span style="color: #666666">.</span>sub(<span style="color: #BA2121">&#39;\d+,&#39;</span>, <span style="color: #BA2121">&#39;&#39;</span>, sample[i])
sample[i] <span style="color: #666666">=</span> sample[i]<span style="color: #666666">.</span>strip()<span style="color: #666666">.</span>split(<span style="color: #BA2121">&#39;,&#39;</span>)
labels <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> s <span style="color: #AA22FF; font-weight: bold">in</span> sample:
labels<span style="color: #666666">.</span>append(s<span style="color: #666666">.</span>pop())
<span style="color: #408080; font-style: italic"># print(sample)</span>
<span style="color: #408080; font-style: italic"># print(labels)</span>
decisionTree <span style="color: #666666">=</span> DecisionTree(sample, attributes, labels)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;System entropy {}&quot;</span><span style="color: #666666">.</span>format(decisionTree<span style="color: #666666">.</span>entropy))
decisionTree<span style="color: #666666">.</span>id3()
decisionTree<span style="color: #666666">.</span>printTree()
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #19177C">__name__</span> <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;__main__&#39;</span>:
test()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._week44-bs023.html">&laquo;</a></li>
<li><a href="._week44-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._week44-bs016.html">17</a></li>
<li><a href="._week44-bs017.html">18</a></li>
<li><a href="._week44-bs018.html">19</a></li>
<li><a href="._week44-bs019.html">20</a></li>
<li><a href="._week44-bs020.html">21</a></li>
<li><a href="._week44-bs021.html">22</a></li>
<li><a href="._week44-bs022.html">23</a></li>
<li><a href="._week44-bs023.html">24</a></li>
<li class="active"><a href="._week44-bs024.html">25</a></li>
<li><a href="._week44-bs025.html">26</a></li>
<li><a href="._week44-bs026.html">27</a></li>
<li><a href="._week44-bs027.html">28</a></li>
<li><a href="._week44-bs028.html">29</a></li>
<li><a href="._week44-bs029.html">30</a></li>
<li><a href="._week44-bs030.html">31</a></li>
<li><a href="._week44-bs031.html">32</a></li>
<li><a href="._week44-bs032.html">33</a></li>
<li><a href="._week44-bs033.html">34</a></li>
<li><a href="">...</a></li>
<li><a href="._week44-bs040.html">41</a></li>
<li><a href="._week44-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>