-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decision_trees.py
266 lines (173 loc) · 6.13 KB
/
Decision_trees.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
# In[65]:
from sklearn.metrics import f1_score
import matplotlib.pyplot as plt
# In[1]:
class Tree:
'''Create a binary tree; keyword-only arguments `data`, `left`, `right`.
Examples:
l1 = Tree.leaf("leaf1")
l2 = Tree.leaf("leaf2")
tree = Tree(data="root", left=l1, right=Tree(right=l2))
'''
def leaf(data):
'''Create a leaf tree
'''
return Tree(data=data)
# pretty-print trees
def __repr__(self):
if self.is_leaf():
return "Leaf(%r)" % self.data
else:
return "Tree(%r) { left = %r, right = %r }" % (self.data, self.left, self.right)
# all arguments after `*` are *keyword-only*!
def __init__(self, *, data = None, left = None, right = None):
self.data = data
self.left = left
self.right = right
def is_leaf(self):
'''Check if this tree is a leaf tree
'''
return self.left == None and self.right == None
def children(self):
'''List of child subtrees
'''
return [x for x in [self.left, self.right] if x]
def depth(self):
'''Compute the depth of a tree
A leaf is depth-1, and a child is one deeper than the parent.
'''
return max([x.depth() for x in self.children()], default=0) + 1
# In[3]:
l1 = Tree.leaf('like')
l2 = Tree.leaf('like')
l3 = Tree.leaf('like')
l4 = Tree.leaf('nah')
l5 = Tree.leaf('nah')
# In[44]:
q4 = Tree(data="morning", left=l3, right=l5)
q3 = Tree(data='likedOtherSys', left=l4, right=l2)
q2 = Tree(data='TakenOtherSys', left=q4, right=q3)
q1 = Tree(data='isSystems', left=l1, right=q2)
# In[79]:
print(q1)
# In[4]:
# the comma-separated values were previously saved as a .txt file
data = pd.read_csv('data.txt', header=0)
new = data['rating'] >= 0
data['ok'] = new
# In[86]:
def single_feature_score(data: object, goal: str, feature: str, scorer='percent') -> float:
assert type(goal) == str and type(feature) == str, "feature names should be passed as strings"
assert type(data)==pd.DataFrame, "data should be passed as a Pandas DataFrame"
sample_df = data[[feature, goal]]
s_liked = sample_df.loc[sample_df[feature]==True]
s_disliked = sample_df.loc[sample_df[feature]==False]
all_liked = s_liked.shape[0]
all_disliked = s_disliked.shape[0]
like_liked = s_liked.loc[s_liked[goal]==True].shape[0]
like_disliked = s_liked.loc[s_liked[goal]==False].shape[0]
dislike_liked = s_disliked.loc[s_disliked[goal]==True].shape[0]
dislike_disliked = s_disliked.loc[s_disliked[goal]==False].shape[0]
if scorer == 'std':
like_std = np.std(s_liked[goal])
dislike_std = np.std(s_disliked[goal])
return -(like_std + dislike_std)
elif scorer == "percent":
return (max([like_liked, like_disliked]) / all_liked) + (max([dislike_liked, dislike_disliked]) / all_disliked)
# In[10]:
def best_feature(data, goal, features, scorer="percent"):
# optional: avoid the lambda using `functools.partial`
return max(features, key=lambda f: single_feature_score(data, goal, f, scorer))
# In[42]:
params = ["easy", "ai", "systems", "theory", "morning"]
# In[20]:
print(f"The best feature is: \n{best_feature(data, 'ok', params, 'percent')}")
# In[21]:
print(f'The worst feature is: \n{min(params, key=lambda f: single_feature_score(data, "ok", f, "percent"))}')
# In[52]:
def DecisionTreeTrain(data: object, features: list, goal="ok") -> Tree:
assert type(goal)==str, "goal feature name should be passed as a string"
assert type(data)==pd.DataFrame, "data should be passed as a Pandas DataFrame"
features = features.copy()
ok_slice = data[goal]
guess = np.max(ok_slice)
if ok_slice.unique().shape[0] == 1:
return Tree.leaf(guess)
elif len(features) == 0:
return Tree.leaf(guess)
else:
best = best_feature(data, goal, features, "percent")
yes = data.loc[data[best] == True]
no = data.loc[data[best] == False]
features.pop(features.index(best))
left = DecisionTreeTrain(no, features)
right = DecisionTreeTrain(yes, features)
return Tree(data=best, left=left, right=right)
# In[53]:
tr = DecisionTreeTrain(data, params)
# In[56]:
def DecisionTreeTest(tree, testp):
assert type(testp) == dict or type(testp) == pd.DataFrame, "test point should be passed as a dict-like object"
if tree.is_leaf() == True:
return tree.data
else:
if testp[tree.data] == False:
return DecisionTreeTest(tree.left, testp)
else:
return DecisionTreeTest(tree.right, testp)
# In[57]:
test_point = {"easy":False, "ai":False, "systems":True, "theory":False, "morning":False}
# In[58]:
DecisionTreeTest(tr, test_point)
# In[59]:
def DecisionTree_with_depth(data, features, goal="ok", maxdepth=5):
maxdepth -= 1
assert type(goal)==str, "goal feature name should be passed as a string"
features = features.copy()
ok_slice = data[goal]
guess = np.max(ok_slice)
if ok_slice.unique().shape[0] == 1:
return Tree.leaf(guess)
elif len(features) == 0:
return Tree.leaf(guess)
elif maxdepth == 0:
return Tree.leaf(guess)
else:
best = best_feature(data, goal, features, "percent")
yes = data.loc[data[best] == True]
no = data.loc[data[best] == False]
features.pop(features.index(best))
left = DecisionTree_with_depth(no, features, maxdepth=maxdepth)
right = DecisionTree_with_depth(yes, features, maxdepth=maxdepth)
return Tree(data=best, left=left, right=right)
# In[60]:
max_tr = DecisionTree_with_depth(data, params, maxdepth=3)
# In[77]:
def test_func(data) -> None:
all_num = data.shape[0]
random_half = np.random.randint(0, all_num, size=(all_num,))
tests = []
rights = []
params = ["easy", "ai", "systems", "theory", "morning"]
for i in random_half:
test_pt = data.iloc[i,:].to_dict()
tests.append(test_pt)
rights.append(test_pt["ok"])
scores = []
for i in range(1, 6):
results = []
mx_tree = DecisionTree_with_depth(data, params, maxdepth=i)
for item in tests:
results.append(DecisionTreeTest(mx_tree, item))
scores.append(f1_score(rights, results))
fig = plt.scatter(range(1, 6), scores)
plt.xlabel("Tree depth")
plt.ylabel("F-1 score")
plt.show()
# In[78]:
test_func(data=data)