This repository was archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
191 lines (141 loc) · 5.64 KB
/
Copy pathtree.py
File metadata and controls
191 lines (141 loc) · 5.64 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
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
import multiprocessing as mp
from question import Question
def unique_vals(dataset, column):
return set([entry.data[column] for entry in dataset])
def count_labels(dataset):
counts = {}
for entry in dataset:
for label in entry.label:
if label not in counts:
counts[label] = 1
else:
counts[label] += 1
return counts
def partition(dataset, question):
matching, non_matching = [], []
for entry in dataset:
if question.match(entry):
matching.append(entry)
else:
non_matching.append(entry)
return matching, non_matching
def gini(dataset):
counts = count_labels(dataset)
impurity = 1
for label in counts:
prob = counts[label] / float(len(dataset))
impurity -= prob**2
return impurity
def info_gain(left_set, right_set, uncertainty):
p = float(len(left_set)) / float(len(left_set) + len(right_set))
return uncertainty - p * gini(left_set) - (1-p) * gini(right_set)
def splitter(info):
question, dataset, uncertainty = info
matching, non_matching = partition(dataset, question)
if not matching or not non_matching:
return None
gain = info_gain(matching, non_matching, uncertainty)
return (gain, question, (matching, non_matching))
def find_best_split(fields, dataset, uncertainty=None):
print("Splitting {} entries.".format(len(dataset)))
best_gain, best_question, best_split = 0, None, None
uncertainty = uncertainty or gini(dataset)
columns = len(dataset[0].data)
for i in range(columns):
values = unique_vals(dataset, i)
if len(dataset) > 400:
# Parallelize best split search
cpus = mp.cpu_count()
if i == 0:
print("-- Using {} CPUs to parallelize the split search.".format(cpus))
splits = []
for value in values:
question = Question(fields, i, value)
splits.append((question, dataset, uncertainty))
chunk = max(int(len(splits)/(cpus*4)), 1)
with mp.Pool(cpus) as p:
for split in p.imap_unordered(splitter, splits, chunksize=chunk):
if split is not None:
gain, question, branches = split
if gain > best_gain:
best_gain, best_question, best_split = gain, question, branches
else:
for value in values:
question = Question(fields, i, value)
matching, non_matching = partition(dataset, question)
if not matching or not non_matching:
continue
gain = info_gain(matching, non_matching, uncertainty)
if gain > best_gain:
best_gain, best_question = gain, question
best_split = (matching, non_matching)
return best_gain, best_question, best_split
class Node(object):
def __init__(self, fields, dataset, level=0):
self.fields = fields
self.gini = gini(dataset)
self.build(dataset, level)
def build(self, dataset, level):
best_split = find_best_split(self.fields, dataset, self.gini)
gain, question, branches = best_split
if not branches:
# Means we got 0 gain
print("Found a leaf at level {}".format(level))
self.predictions = count_labels(dataset)
self.is_leaf = True
return
left, right = branches
print("Found a level {} split:".format(level))
print(question)
print("Matching: {} entries\tNon-matching: {} entries".format(len(left), len(right)))
self.left_branch = Node(self.fields, left, level + 1)
self.right_branch = Node(self.fields, right, level + 1)
self.question = question
self.is_leaf = False
return
def classify(self, entry):
if self.is_leaf:
return self
if self.question.match(entry):
return self.left_branch.classify(entry)
else:
return self.right_branch.classify(entry)
def predict(self, entry):
successes = []
predict = self.classify(entry).predictions.copy()
for label in entry.label:
total = float(sum(predict.values()))
for key, value in predict.items():
predict[key] = float(predict[key]) / total
if label in predict:
success = float(predict[label]) / total
successes.append(success)
return sum(successes), predict
def print(self, spacing=''):
if self.is_leaf:
s = spacing + "Predict: "
total = float(sum(self.predictions.values()))
probs = {}
for label in self.predictions:
prob = self.predictions[label] * 100 / total
probs[label] = "{:.2f}%".format(prob)
return s + str(probs)
s = spacing + "(Gini: {:.2f}) ".format(self.gini) + str(self.question) + '\n'
s += spacing + "├─ True:\n"
s += self.left_branch.print(spacing + "│ ") + '\n'
s += spacing + "└─ False:\n"
s += self.right_branch.print(spacing + " ")
return s
def __str__(self):
return self.print()
class Tree(object):
def __init__(self, fields, dataset):
self.fields = fields
self.dataset = dataset
self.root = Node(self.fields, self.dataset)
def classify(self, entry):
return self.root.classify(entry)
def predict(self, entry):
return self.root.predict(entry)
def __str__(self):
return str(self.root)