-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionTree.py
179 lines (149 loc) · 6.3 KB
/
DecisionTree.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
##
# @author Rafal Litka
# @file DecisionTree.py
from Classifier import Classifier
import math
import pandas as pd
import random
import ete3
## Wezel drzewa
class TreeNode(Classifier):
def __init__(self):
super().__init__()
self.leafes = {}
self.branches = {}
def fit(self, trainSet, trainLab):
entropies = {feature:0 for feature in trainSet.columns}
for feature in trainSet.columns:
column = trainSet[feature]
for value in column.unique():
reducedLab = trainLab[column==value]
# wyznaczanie entropii
entropy = 0.
for count in reducedLab.value_counts():
x = count/len(reducedLab)
if not (x == 0. or x == 1.):
entropy -= x*math.log2(x)
entropies[feature] += len(reducedLab)/len(trainLab)*entropy
# wybor cechy o minimalnej entropii
self.splitFeature = min(entropies,key=entropies.get)
column = trainSet[self.splitFeature]
for value in trainSet[self.splitFeature].unique():
reducedSet = trainSet.loc[column == value, :]
reducedLab = trainLab[column == value]
# obliczanie entropii
entropy = 0.
for count in reducedLab.value_counts():
x = count / len(reducedLab)
if not (x == 0. or x == 1.):
entropy -= x * math.log2(x)
if entropy == 0.0 or len(trainSet.columns) == 1:
# tworzenie liscia
self.leafes[value] = TreeLeaf()
self.leafes[value].fit(reducedLab)
else:
# tworzenie drzewa
self.branches[value] = TreeNode()
self.branches[value].fit(reducedSet.drop(labels=[self.splitFeature],axis='columns'), reducedLab)
def predict(self, testSet):
values = testSet.loc[:,self.splitFeature].unique()
tLab = pd.Series(None,index=testSet.index, name='class')
for value in values:
if value in list(self.leafes.keys()) + list(self.branches.keys()):
if value in list(self.leafes.keys()):
tLab[testSet[self.splitFeature] == value] = self.leafes[value].predict()
if value in list(self.branches.keys()):
reducedSet = testSet.loc[testSet.loc[:,self.splitFeature] == value,:]
tLab[testSet[self.splitFeature] == value] = self.branches[value].predict(reducedSet)
else:
# losowy wybor jezeli atrybutu nie bylo w zbiorze trenujacym
randChoice = random.choice(list(self.leafes.keys()) + list(self.branches.keys()))
if randChoice in list(self.leafes.keys()):
tLab[testSet[self.splitFeature] == value] = self.leafes[randChoice].predict()
else:
reducedSet = testSet.loc[testSet.loc[:,self.splitFeature] == value,:]
tLab[testSet[self.splitFeature] == value] = self.branches[randChoice].predict(reducedSet)
return tLab
## Przycinanie drzewa
# @param pruneSet zestaw danych do przyciecia drzewa
# @param pruneLab zestaw etykiet do przyciecia drzewa
def prune(self, pruneSet, pruneLab):
toDel = []
for value in self.branches.keys():
reducedSet = pruneSet.loc[pruneSet.loc[:, self.splitFeature] == value, :]
if not reducedSet.shape[0]:
continue
reducedLab = pruneLab[pruneSet[self.splitFeature] == value]
nodeRes = self.branches[value].predict(reducedSet)
nodeErr = sum(nodeRes != reducedLab)
leaf = TreeLeaf()
leaf.fit(reducedLab)
leafRes = pd.Series(None, index=reducedLab.index, name=reducedLab.name)
leafRes[:] = leaf.predict()
leafErr = sum(leafRes != reducedLab)
if leafErr < nodeErr:
self.leafes[value] = leaf
toDel.append(value)
else:
self.branches[value].prune(reducedSet, reducedLab)
for d in toDel:
del self.branches[d]
## wizualizacja drzewa
def visualize(self):
me = ete3.TreeNode(name=self.splitFeature)
for key in self.leafes.keys():
me.add_child(self.leafes[key].visualize())
for key in self.branches.keys():
me.add_child(self.branches[key].visualize())
return me
## lisc drzewa
class TreeLeaf:
def __init__(self):
super().__init__()
self.decision = None
def fit(self, trainLab):
counts = trainLab.value_counts()
self.decision = counts.index[counts.argmax()]
def predict(self):
return self.decision
## wizualizacja liscia
def visualize(self):
return ete3.TreeNode(name=self.decision)
## Drzewo decyzyjne
class DecisionTree(Classifier):
def __init__(self, pruneSplit = 0.0):
super().__init__()
self.rootNode = TreeNode()
self.pruneSplit = pruneSplit
def fit(self, trainSet, trainLab):
n = trainLab.shape[0]
split = int(self.pruneSplit*n)
pruneSet = trainSet.iloc[:split]
pruneLab = trainLab.iloc[:split]
trainSet = trainSet.iloc[split:]
trainLab = trainLab.iloc[split:]
self.rootNode.fit(trainSet, trainLab)
self.rootNode.prune(pruneSet, pruneLab)
def predict(self, testSet):
return self.rootNode.predict(testSet)
## Przycinanie drzewa
def prune(self, pruneSet, pruneLab):
self.rootNode.prune(pruneSet, pruneLab)
## Wizualizacja drzewa
def visualize(self, fileName):
t = self.rootNode.visualize()
ts = ete3.TreeStyle()
def my_layout(node):
if node.is_leaf():
# If terminal node, draws its name
name_face = ete3.AttrFace("name")
else:
# If internal node, draws label with smaller font size
name_face = ete3.AttrFace("name", fsize=10)
# Adds the name face to the image at the preferred position
ete3.faces.add_face_to_node(name_face, node, column=0, position="branch-right")
ts.layout_fn = my_layout
ts.show_leaf_name = False
t.render(file_name=fileName, tree_style=ts)
pass