-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtree.py
334 lines (291 loc) · 12.8 KB
/
dtree.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""This is the main module for making manual decision trees."""
import pandas as pd
import pydot
import numpy as np
from collections import defaultdict
import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
import itertools
class decisionNode(object):
newid = itertools.count().next
def __init__(self, parent=None, qstr=None, df=None):
"""A decision tree node.
parent := The parent node. None for root node.
qstr := Query string to define this level. None for root node.
df := pd.DataFrame containing the data. ONLY for root node.
"""
self.id = decisionNode.newid()
self.parent = parent
if self.parent is None:
self.qstr = ""
self.var = 'All data'
self.val = 'All data'
else:
self.qstr = qstr
self.var, self.val = self.qstr.split(' == ')
self.val = self.val.strip("'")
self.children = list()
self.n_terminal = 0
if df is not None:
self.df = df
elif self.parent is None:
raise Exception()
else:
self.df = self.parent.df.query(self.qstr)
self.size = len(self.df)
if self.parent is None:
self.marginal_prob = float(1.)
self.overall_prob = float(1.)
else:
self.marginal_prob = float(self.size) / float(self.parent.size)
self.overall_prob = self.marginal_prob * self.parent.marginal_prob
self.depth = self._depth()
def __repr__(self):
return "%s%s (%d, %0.1f%%, %0.1f%%)" % ('\t' * self.depth,
self.qstr,
self.size,
self.marginal_prob * 100.,
self.overall_prob * 100.
)
def _print_children(self):
"""Prints out the children of this node."""
print self
for child in self.children:
child._print_children()
def _tofile(self, file):
"""Utility function for writing a node to a text file."""
file.write(str(self) + '\n')
for child in self.children:
child._tofile(file)
def _toexcel(self, worksheet, merge_format, pct_format, datadict):
"""Prepare the data in the node for Excel writing."""
global depth_used # This is for location tracking
if self.n_terminal == 0:
height = 1
else:
height = self.n_terminal
start_col = self.depth * 4
# Write the header
if depth_used[self.depth] == 1:
worksheet.write(0, start_col, self.var)
worksheet.write(0, start_col + 1, 'N')
worksheet.write(0, start_col + 2, 'Marginal Share')
worksheet.write(0, start_col + 3, 'Overall Share')
start_row = depth_used[self.depth]
self.start_row = start_row
# Make pretty versions of the value names
if datadict is not None:
if self.var in datadict.keys():
thisdict = datadict[self.var]
if self.val in thisdict.keys():
pretty_val = thisdict[self.val]
else:
pretty_val = self.val
else:
pretty_val = self.val
else:
pretty_val = self.val
marg_prob_function = '=%s/%s' # String for writing marg calc
overall_prob_function = '=%s*%s'
if self.parent is None:
parent_start_row = self.start_row
parent_start_col = start_col
else:
parent_start_row = self.parent.start_row
parent_start_col = start_col - 4
if height == 1:
worksheet.write(start_row, start_col, pretty_val, merge_format)
worksheet.write(start_row, start_col + 1, self.size, merge_format)
worksheet.write(start_row, start_col + 2,
marg_prob_function %
(xl_rowcol_to_cell(self.start_row,
start_col + 1),
xl_rowcol_to_cell(parent_start_row,
parent_start_col + 1)),
pct_format)
if parent_start_col == start_col:
worksheet.write(start_row, start_col + 3,
'=%s' % xl_rowcol_to_cell(start_row,
start_col + 2),
pct_format)
else:
worksheet.write(start_row, start_col + 3,
overall_prob_function %
(xl_rowcol_to_cell(self.start_row,
start_col + 2),
xl_rowcol_to_cell(parent_start_row,
parent_start_col + 3)),
pct_format)
else:
worksheet.merge_range(start_row, start_col,
start_row + height - 1, start_col,
pretty_val, merge_format)
worksheet.merge_range(start_row, start_col + 1,
start_row + height - 1, start_col + 1,
self.size, merge_format)
worksheet.merge_range(start_row, start_col + 2,
start_row + height - 1, start_col + 2,
marg_prob_function %
(xl_rowcol_to_cell(self.start_row,
start_col + 1),
xl_rowcol_to_cell(parent_start_row,
parent_start_col + 1)),
pct_format)
if parent_start_col == start_col:
worksheet.merge_range(start_row, start_col + 3,
start_row + height - 1, start_col + 3,
'=%s' % xl_rowcol_to_cell(start_row,
start_col + 2))
else:
worksheet.merge_range(start_row, start_col + 3,
start_row + height - 1, start_col + 3,
overall_prob_function %
(xl_rowcol_to_cell(self.start_row,
start_col + 2),
xl_rowcol_to_cell(parent_start_row,
parent_start_col + 3)),
pct_format)
depth_used[self.depth] += height
if self.n_terminal != 0:
for child in self.children:
child._toexcel(worksheet, merge_format, pct_format, datadict)
def _depth(self):
"""Calculate depth of this node."""
if self.parent is not None:
return self.parent._depth() + 1
else:
return 0
def _terminal_children(self):
"""Count number of terminal children under this node."""
self.n_terminal = 0
if self.children != []:
for child in self.children:
child._terminal_children()
if self.parent is not None:
self.parent.n_terminal += self.n_terminal
else:
self.parent.n_terminal += 1
def _visualize(self):
"""Make a set of pydot Edges for all links."""
edges = []
if self.children != []:
for child in self.children:
edges.append(pydot.Edge(str(self), str(child)))
edges.extend(child._visualize())
return edges
def spawn_child(self, qstr):
""" Create a child.
qstr := Marginal query string
"""
self.children.append(decisionNode(self, qstr))
def spawn_children(self, split_var, split_vals=None, dtype='cat'):
if type(split_var) == tuple:
raise TypeError()
self.df[split_var] = self.df[split_var].astype(str)
for v in np.unique(self.df[split_var]):
qstr = "%s == '%s'" % (split_var, str(v))
self.spawn_child(qstr)
def find_bottom(self):
if self.children == []:
return [self]
else:
found_children = [c.find_bottom() for c in self.children]
return [item for sublist in found_children for item in sublist]
def siblings(self):
if self.parent is None:
return []
else:
return [a for a in self.parent.children if a is not self]
class dtree(object):
def __init__(self, data):
"""Initialize a Decision Tree object.
data := The full dataset in a Pandas Dataframe
"""
self.data = data
self.columns = self.data.columns
self.root = decisionNode(df=self.data.copy())
def __repr__(self):
if self.pretty_print() is not None:
return self.pretty_print()
else:
return "A decision tree that has yet to be constructed."
def _reset(self):
self.root = decisionNode(df=self.data.copy())
def split_tree(self, split_vars, reset=False):
"""Construct the tree splits
split_vars := The variables you want the tree split on as a list
reset := Boolean. Do you want a fresh tree?
"""
self._reset()
# TODO implement the split_vars for continuous variables to take cut points
# TODO accept splitting constraints
for s in split_vars:
# This needs to go to the deepest children and do the splits
for c in self.root.find_bottom():
c.spawn_children(s)
def pretty_print(self):
self.root._print_children()
def to_excel(self, workbook, output_sheet,
highlight_leaves=True, closeit=True, datadict=None):
"""Write a tree to an excel sheet.
workbook := An xlsxwriter.workbook
output_sheet := Name for the worksheet
datadict := a nested dictionary of variable names, value names,
and replacement values for pretty output
"""
merge_format = workbook.add_format({'align': 'center',
'valign': 'vcenter'})
pct_format = workbook.add_format({'align': 'center',
'valign': 'vcenter'
})
pct_format.set_num_format('0.0%')
global depth_used
depth_used = defaultdict(lambda: 1)
self.root._terminal_children()
# if output_name[-5:] != '.xlsx':
# output_name += '.xlsx'
worksheet = workbook.add_worksheet(name=output_sheet)
self.root._toexcel(worksheet, merge_format, pct_format, datadict)
worksheet.freeze_panes(1, 0)
if highlight_leaves:
for d in depth_used.keys():
if d == 0:
continue
worksheet.conditional_format(1, d * 4 + 3,
depth_used[d], d * 4 + 3,
{'type': '2_color_scale',
'min_type': 'min',
'max_type': 'max',
'min_color': 'white',
'max_color': 'red'})
if closeit:
workbook.close()
def many_trees_to_excel(self, defdict, workbook, closeit=True,
datadict=None):
""" Generate many trees and write them to a common Excel workbook.
defdict := dictionary with keys as sheet names and values as list of
columns to construct the tree
workbook_path := name of the workbook
"""
for name, cols in defdict.iteritems():
print name
self.split_tree(cols, reset=True)
self.to_excel(workbook, name, closeit=False, datadict=datadict)
if closeit:
workbook.close()
else:
return workbook
def to_text(self, file_name):
""" Dumps pretty printed tree to <file_name>.txt"""
with open(file_name, mode='w') as fi:
fi.write(
"Structured as 'leaf details (Number records, Marginal Share, Overall Share)'")
self.root._tofile(fi)
def to_png(self, output_path):
""" Uses graphviz to make a graph picture
output_path := .png for the output"""
graph = pydot.Dot(graph_type='graph')
edges = self.root._visualize()
for e in edges:
graph.add_edge(e)
graph.write(output_path)