-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc4_5.py
393 lines (362 loc) · 11.4 KB
/
c4_5.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#from scipy import mode
# coding=utf-8
import csv
import math
import operator
import copy
#import numpy as np
from collections import Counter
class node(object):
"""A class for decision tree structure
Attributes:
val: value of the node, may be list to store condition
child: list of subtree
"""
def __init__(self, val, child):
self.val = val
self.child = child
def __str__(self):
return 'val:'+str(self.val) +'\n'+'child:'+ str(self.child)
class C4_5(object):
"""A class for all the implementation of c4.5 dtree algorithm
Attributes:
tree: store the decision tree as tree structure
format: the form of attributes, i.e. ['numeric', 'nominal', 'nominal']
attributes: a list of attributes of the input data
train_set: the input training data
data_len: the length of train_set
rule: the generated rule
"""
def __init__(self, train_set, format, rule):
"""Initialize C4_5 with training data, format, rule(can be empty)"""
self.tree = node(None, [])
"""store the headerline in attributes
store the data in train_set
"""
train_set = list(train_set)
self.format = format
self.attributes = train_set[0][:-1]
self.train_set = train_set[1:]
self.data_len = len(self.train_set)
self.rule = rule
"""it's a recursive method,
when first call it, the input tree should be self.tree
attributes the same
examples should be self.train_set use list comprehension to get col
maybe : operand is more optimized, but they're both O(n) in principle
zip(*train_set
"""
def train(self, examples, tree, attributes, default):
# when no examples return default
if len(examples) == 0:
#print 'A'
return default
# if all examples have the same classification return the classification
elif allequal([item[-1] for item in examples]):
#print 'B'
return examples[0][-1]
# when attributes is empty then return mode(examples)
elif len(attributes) == 0 or len(examples) < 100:
#print 'C'
return mode([item[-1] for item in examples])[0][0]
else:
best = self.choose_attr(attributes, examples)
if best == 0:
return mode([item[-1] for item in examples])[0][0]
print best
tree.val = best[0]
#attributes.remove(best[0])
idx = self.attributes.index(best[0])
if best[1] == 'nom':
attributes.remove(best[0])
for v in unique([item[idx] for item in examples]):
exp = [item for item in examples if item[idx] == v]
subtree = self.train(exp, node(None, []), list(attributes), mode([item[-1] for item in examples])[0][0])
branch = node([best[0], '==', v], [subtree])
tree.child.append(branch)
else:
#print 'A'
#print 'examples: '+ str(len(examples))
exp1 = [item for item in examples if float(item[idx]) > best[2]]
#print 'exp1: '+str(len(exp1))
default = mode([item[-1] for item in examples])[0][0]
if len(exp1) == len(examples):
return default
subtree1 = self.train(exp1, node(None, []), list(attributes), default)
branch1 = node([best[0], '>', str(best[2])], [subtree1])
tree.child.append(branch1)
exp2 = [item for item in examples if float(item[idx]) <= best[2]]
#print 'exp2: '+str(len(exp2))
subtree2 = self.train(exp2, node(None, []), list(attributes), default)
branch2 = node([best[0], '<=', str(best[2])], [subtree2])
tree.child.append(branch2)
return tree
def choose_attr(self, attributes, examples):
max_gain = -10
data_len = float(len(examples))
group = [item[-1] for item in examples]
infor = entropy([group.count('1')/data_len, group.count('0')/data_len])
for attr in attributes:
idx = self.attributes.index(attr)
gain = infor
if self.format[idx] == 'nominal':
mode_exp = mode([item[idx] for item in examples])[0][0]
#mode_0 = mode([item[idx] for item in examples if item[-1]=='0'])[0][0]
for item in examples:
if item[idx] == '?':
item[idx] = mode_exp
for i in unique([item[idx] for item in examples]):
exp = [item[-1] for item in examples if item[idx] == i]
split_len = float(len(exp))
gain -= split_len/data_len * entropy([ exp.count('1')/split_len, exp.count('0')/split_len ])
if gain > max_gain:
max_gain = gain
best = [attr, 'nom']
else:
num_lst = [float(item[idx]) for item in examples if item[idx] != '?']
#num_lst_0 = [float(item[idx]) for item in examples if item[idx] != '?' and item[-1]=='0']
mean = sum(num_lst) / len(num_lst)
#mean_0 = sum(num_lst_0) / len(num_lst_0)
#mode_exp = float(mode([item[idx] for item in examples])[0][0])
exps = list(examples)
for item in exps:
if item[idx] == '?':
item[idx] = mean
else:
item[idx] = float(item[idx])
#exps = list(examples)
#for item in exps:
# item[idx] = float(item[idx])
exps.sort(key=operator.itemgetter(idx))
split_candidate = self.split(exps, idx)
for thresh in split_candidate:
#print '*************************************'
#print thresh
gain = infor
exp1 = [item[-1] for item in exps if float(item[idx]) > thresh]
exp2 = [item[-1] for item in exps if float(item[idx]) <= thresh]
len1 = float(len(exp1))
len2 = float(len(exp2))
if len1==0 or len2==0:
gain = 0
else:
gain -= len1/data_len * entropy([ exp1.count('1')/len1, exp1.count('0')/len1 ])
gain -= len2/data_len * entropy([ exp2.count('1')/len2, exp2.count('0')/len2 ])
if gain > max_gain:
max_gain = gain
best = [attr, 'num', thresh]
print max_gain
if max_gain <= 0:
return 0
return best
def rep_miss(self, examples):
exp = copy.deepcopy(examples)
for attr in self.attributes:
idx = self.attributes.index(attr)
if self.format[idx] == 'nominal':
mode_exp = mode([item[idx] for item in exp])[0][0]
#mode_0 = mode([item[idx] for item in exp if item[-1]=='0'])[0][0]
for item in exp:
if item[idx] == '?':
item[idx] = mode_exp
else:
num_lst = [float(item[idx]) for item in exp if item[idx] != '?']
#num_lst_0 = [float(item[idx]) for item in exp if item[idx] != '?' and item[-1] == '0']
mean = sum(num_lst) / len(num_lst)
#mean_0 = sum(num_lst_0) / len(num_lst_0)
#print mean
#mode_exp = mode([item[idx] for item in exp])[0][0]
for item in exp:
if item[idx] == '?':
item[idx] = mean
return exp
def split(self, lst, idx):
split_candidate = []
for x, y in zip(lst, lst[1:]):
if (x[-1] != y[-1]) and (x[idx] != y[idx]):
split_candidate.append( (x[idx] + y[idx]) / 2 )
return split_candidate
"""
def rep_miss(self, examples):
exp = copy.deepcopy(examples)
for attr in self.attributes:
idx = self.attributes.index(attr)
mode_1 = mode([item[idx] for item in examples if item[-1] == '1'])[0][0]
mode_0 = mode([item[idx] for item in examples if item[-1] == '0'])[0][0]
for item in exp:
if item[idx] == '?':
if item[-1] == '1':
item[idx] = mode_1
else:
item[idx] = mode_0
return exp
"""
def validate(self, valid_file):
valid_set = list(read_data(valid_file))
valid_data = valid_set[1:]
acc = 0.0
exp = self.rep_miss(valid_data)
#print exp
for sample in exp:
rlt = self.test(sample)
#print rlt
#print 'rlt: '+rlt
#print 'smp: '+sample[-1]
if rlt == sample[-1]:
acc += 1.0
return acc/len(valid_data)
def preprocess(self, valid_file):
valid_set = list(read_data(valid_file))
valid_data = valid_set[1:]
exp = self.rep_miss(valid_data)
return exp
def valid_prune(self, exp):
acc = 0
for sample in exp:
if self.test(sample) == sample[-1]:
acc += 1
return float(acc)/len(exp)
def predict(self, test_file):
test_set = read_data(test_file)
test_data = test_set[1:]
exp = self.rep_miss(test_data)
for i in range(0, len(exp)):
rlt = self.test(exp[i])
test_data[i].append(rlt)
header = test_set[0]
header.append('predict '+header[-1])
test_data.insert(0, header)
with open('outfile.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(test_data)
def fast_test(self, tree, sample):
if tree.child:
if tree.child[0] == '1':
return '1'
elif tree.child[0] == '0':
return '0'
else:
for item in tree.child:
if isinstance(item.val, list):
idx = self.attributes.index(item.val[0])
attr = sample[idx]
if self.format[idx] == 'nominal':
if attr == item.val[2]:
return self.fast_test(item, sample)
else:
if item.val[1] == '>':
if float(attr) > float(item.val[2]):
return self.fast_test(item, sample)
else:
if float(attr) <= float(item.val[2]):
return self.fast_test(item, sample)
else:
return self.fast_test(item, sample)
def prune(self, valid_file):
exp = self.preprocess(valid_file)
for single_rule in self.rule:
before = self.valid_prune(exp)
old_rule = copy.deepcopy(self.rule)
self.rule.remove(single_rule)
after = self.valid_prune(exp)
print before
print after
#if after > before:
# self.prune(valid_file)
if after < before:
self.rule = old_rule
def test(self, sample):
#print '.'
for single_rule in self.rule:
for stump in single_rule:
if stump[0] == 'class':
return stump[2]
idx = self.attributes.index(stump[0])
attr = sample[idx]
if self.format[idx] == 'nominal':
if attr == stump[2]:
pass
else:
break
else:
if stump[1] == '>':
if float(attr) > float(stump[2]):
pass
else:
break
else:
if float(attr) <= float(stump[2]):
pass
else:
break
return '0'
def __str__(self):
#print self.rule
rule_str = []
for single_rule in self.rule:
single_rule_str = []
for item in single_rule:
single_rule_str.append('('+' '.join(item)+')')
rule_str.append('('+' ∨ '.join(single_rule_str)+')')
return ' ∧ '.join(rule_str)
def rule_generator(self, tree, single_rule):
#if flag:
# self.rule = []
#print tree
if tree.child:
if isinstance(tree.val, list):
single_rule.append(tree.val)
if tree.child[0] == '1':
single_rule.append(['class', '=', '1'])
self.rule.append(single_rule)
elif tree.child[0] == '0':
single_rule.append(['class', '=', '0'])
self.rule.append(single_rule)
else:
for item in tree.child:
self.rule_generator(item, list(single_rule))
"""
def rule_generator(self, tree, rule_str, rule):
if tree.child:
if isinstance(tree.val, list):
rule_str.append(''.join(tree.val))
if tree.child[0] == str(1):
rule.append('('+' AND '.join(rule_str)+')')
elif tree.child[0] == str(0):
pass
else:
for item in tree.child:
self.rule_generator(item, list(rule_str), rule)
return ' OR '.join(rule)
"""
def read_data(inputfile):
csvfile = open(inputfile, 'rb')
data = list(csv.reader(csvfile))
csvfile.close()
return data
def entropy(lst):
if lst.count(0):
return 0
entrop = 0
for p in lst:
entrop -= p * math.log(p, 2)
return entrop
"""produce the list of unique values of a list
"""
def unique(seq):
keys = {}
for e in seq:
keys[e] = 1
return keys.keys()
def allequal(seq):
flag = seq[0]
for item in seq:
if item != flag:
return 0
return 1
def mode(lst):
frequent = Counter(lst)
mostfrequent = frequent.most_common(2)
if mostfrequent[0][0] == '?':
mostfrequent = mostfrequent[1:]
return mostfrequent