-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtree.py
More file actions
134 lines (100 loc) · 3.81 KB
/
dtree.py
File metadata and controls
134 lines (100 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import math
def entropy(dataset):
"Calculate the entropy of a dataset"
n = len(dataset)
nPos = len([x for x in dataset if x.positive])
nNeg = n - nPos
if nPos == 0 or nNeg == 0:
return 0.0
return -float(nPos)/n * log2(float(nPos)/n) + \
-float(nNeg)/n * log2(float(nNeg)/n)
def averageGain(dataset, attribute):
"Calculate the expected information gain when an attribute becomes known"
weighted = 0.0
for v in attribute.values:
subset = select(dataset, attribute, v)
weighted += entropy(subset) * len(subset)
return entropy(dataset) - weighted/len(dataset)
def log2(x):
"Logarithm, base 2"
return math.log(x, 2)
def select(dataset, attribute, value):
"Return subset of data samples where the attribute has the given value"
return [x for x in dataset if x.attribute[attribute] == value]
def bestAttribute(dataset, attributes):
"Attribute with highest expected information gain"
gains = [(averageGain(dataset, a), a) for a in attributes]
return max(gains, key=lambda x: x[0])[1]
def allPositive(dataset):
"Check if all samples are positive"
return all([x.positive for x in dataset])
def allNegative(dataset):
"Check if all samples are negative"
return not any([x.positive for x in dataset])
def mostCommon(dataset):
"Majority class of the dataset"
pCount = len([x for x in dataset if x.positive])
nCount = len([x for x in dataset if not x.positive])
return pCount > nCount
class TreeNode:
"Decision tree representation"
def __init__(self, attribute, branches, default):
self.attribute = attribute
self.branches = branches
self.default = default
def __repr__(self):
"Produce readable (string) representation of the tree"
accum = str(self.attribute) + '('
for x in sorted(self.branches):
accum += str(self.branches[x])
return accum + ')'
class TreeLeaf:
"Decision tree representation for leaf nodes"
def __init__(self, cvalue):
self.cvalue = cvalue
def __repr__(self):
"Produce readable (string) representation of this leaf"
if self.cvalue:
return '+'
return '-'
def buildTree(dataset, attributes, maxdepth=1000000):
"Recursively build a decision tree"
def buildBranch(dataset, default, attributes):
if not dataset:
return TreeLeaf(default)
if allPositive(dataset):
return TreeLeaf(True)
if allNegative(dataset):
return TreeLeaf(False)
return buildTree(dataset, attributes, maxdepth-1)
default = mostCommon(dataset)
if maxdepth < 1:
return TreeLeaf(default)
a = bestAttribute(dataset, attributes)
attributesLeft = [x for x in attributes if x != a]
branches = [(v, buildBranch(select(dataset, a, v), default, attributesLeft))
for v in a.values]
return TreeNode(a, dict(branches), default)
def classify(tree, sample):
"Classify a sample using the given decition tree"
if isinstance(tree, TreeLeaf):
return tree.cvalue
return classify(tree.branches[sample.attribute[tree.attribute]], sample)
def check(tree, testdata):
"Measure fraction of correctly classified samples"
correct = 0
for x in testdata:
if classify(tree, x) == x.positive:
correct += 1
return float(correct)/len(testdata)
def allPruned(tree):
"Return a list if trees, each with one node replaced by the corresponding default class"
if isinstance(tree, TreeLeaf):
return ()
alternatives = (TreeLeaf(tree.default),)
for v in tree.branches:
for r in allPruned(tree.branches[v]):
b = tree.branches.copy()
b[v] = r
alternatives += (TreeNode(tree.attribute, b, tree.default),)
return alternatives