-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.rb
More file actions
587 lines (294 loc) · 8.22 KB
/
DecisionTree.rb
File metadata and controls
587 lines (294 loc) · 8.22 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
__author__ = 'CFWLoader'
require 'set'
require './graphviz_dt_api.rb'
class DTNode
def initialize data_partition = nil, class_tag = nil, attr_tag_name = nil
@class_tag = class_tag
@data_partition = data_partition
@attr_tag_name = attr_tag_name
@child_nodes = {}
end
def data_partition= val
@data_partition = val
end
def data_partition
@data_partition
end
def class_tag= val
@class_tag = val
end
def class_tag
@class_tag
end
def attr_tag_name= val
@attr_tag_name = val
end
def attr_tag_name
@attr_tag_name
end
def child_nodes
@child_nodes
end
def add_child_node trait_value, child_node
@child_nodes[trait_value] = child_node
end
def get_child_node_by_attr_val attr_val
if @child_nodes.has_key? attr_val
@child_nodes[attr_val]
else
nil
end
end
end
class DecisionTree
def initialize dataset, class_tag_name, attr_list = nil
@dataset = dataset
@class_tag_name = class_tag_name
if attr_list.nil?
@attr_list = gen_attr_list dataset
else
@attr_list = attr_list
end
@root = DTNode.new dataset
end
def gen_attr_list dataset
attr_list = Set.new
dataset.each{|record|
record.each_key { |key|
unless key == @class_tag_name or attr_list.include? key
attr_list << key
end
}
}
attr_list
end
def same_class? data_partition
class_val = data_partition[0][@class_tag_name]
data_partition.each { |record| if record[@class_tag_name] != class_val then return false end }
true
end
def majority_class_value data_partition
class_statistic = {}
data_partition.each { |record|
if class_statistic.include? record[@class_tag_name]
class_statistic[record[@class_tag_name]] += 1
else
class_statistic[record[@class_tag_name]] = 1
end
}
max_count = 0
mark_class_value = nil
class_statistic.each_pair{|k, v|
if v > max_count
max_count = v
mark_class_value = k
end
}
mark_class_value
end
def entropy data_partition, attr = nil
if attr.nil?
class_statistic = {}
data_partition.each { |record|
if class_statistic.include? record[@class_tag_name]
class_statistic[record[@class_tag_name]] += 1
else
class_statistic[record[@class_tag_name]] = 1
end
}
data_entropy = 0
total_count = data_partition.size
class_statistic.each_value {|val|
propability = val / total_count.to_f
data_entropy += propability * Math.log2(propability)
}
return -data_entropy
end
attr_hash = {}
data_partition.each { |record|
attr_val = record[attr]
if attr_hash.include? attr_val
attr_hash[attr_val][0] += 1
attr_hash[attr_val] << record
else
attr_hash[attr_val] = [1, record]
end
}
total_count = data_partition.size
attr_data_entropy = 0
attr_hash.each_value {|val|
attr_data_entropy += val[0] / total_count.to_f * entropy(val[1..(val.size - 1)])
val.delete_at 0
}
return attr_data_entropy, attr_hash
end
def max_entropy_gain_attr data_partition, attr_list
ori_entropy = entropy data_partition
max_entropy_gain = 0
attr_val = nil
attr_val_partition = nil
attr_list.each {|attr|
tmp_entropy_gain, tmp_attr_val_partition = entropy(data_partition, attr)
tmp_entropy_gain = ori_entropy - tmp_entropy_gain
if max_entropy_gain < tmp_entropy_gain
max_entropy_gain = tmp_entropy_gain
attr_val = attr
attr_val_partition = tmp_attr_val_partition
end
}
return attr_val, attr_val_partition
end
def gen_decision_tree data_partition, attr_list
if same_class? data_partition
return DTNode.new data_partition, data_partition[0][@class_tag_name]
end
if @attr_list.empty?
return DTNode.new data_partition, majority_class_value(data_partition)
end
ret_node = DTNode.new data_partition
pri_attr, partitions_under_attr = max_entropy_gain_attr data_partition, attr_list
ret_node.attr_tag_name= pri_attr
partitions_under_attr.each_pair {|key, val|
if val.size < 1
new_child = DTNode.new val, majority_class_value(data_partition)
else
new_attr_list= attr_list.clone
new_attr_list.delete_if {|ele| ele == key}
new_child = gen_decision_tree val, new_attr_list
end
ret_node.add_child_node key, new_child
}
ret_node
end
def fit
@root = gen_decision_tree @dataset, @attr_list
end
def classify record
iter = @root
while iter.class_tag.nil?
iter = iter.get_child_node_by_attr_val record[iter.attr_tag_name]
if iter.nil?
return 'Failed to classify the record.'
end
end
iter.class_tag
end
def root
@root
end
end
def load_employee_data path
src_data = open path, 'r'
data_collection = []
src_data.each { |line|
record = line.chomp.split(',')
trans_rec = {}
trans_rec['department'] = record[0]
trans_rec['status'] = record[1]
trans_rec['age'] = record[2]
trans_rec['salary'] = record[3]
trans_rec['count'] = record[4]
data_collection << trans_rec
}
src_data.close
data_collection
end
def load_employee_regened_data path
src_data = open path, 'r'
train_collection = []
src_data.each { |line|
record = line.chomp.split(',')
trans_rec = {}
trans_rec['department'] = record[0]
trans_rec['age'] = record[1]
trans_rec['salary'] = record[2]
trans_rec['status'] = record[3]
train_collection << trans_rec
}
src_data.close
train_collection
end
def load_test_data path
src_data = open path, 'r'
data_collection = []
src_data.each { |line|
record = line.chomp.split(',')
trans_rec = {}
trans_rec['attr1'] = record[0]
trans_rec['attr2'] = record[1]
trans_rec['status'] = record[2]
data_collection << trans_rec
}
src_data.close
data_collection
end
def test_cast1
test_dc = load_test_data './test.data'
dtree = DecisionTree.new test_dc, 'status'
all_entropy = dtree.entropy test_dc
age_entropy, data_partitions = dtree.entropy test_dc, 'attr2'
puts all_entropy - age_entropy
data_partitions.each {|element|
puts element.inspect
}
end
def test_case2
data_collection = load_employee_data('./employees.data')
data_collection.each { |record|
#puts record.inspect
record.each_pair { |key, val|
print "(#{key}, #{val})"
}
puts
}
end
def test_case3
data_collection = load_employee_data('./employees.data')
dtree = DecisionTree.new data_collection, 'status'
dtree.fit
correct_count = 0
data_collection.each {|record|
if record['status'] == dtree.classify(record)
correct_count += 1
end
}
puts "Correctness Ratio: #{100 * correct_count/data_collection.size.to_f}%."
end
def test_case4
data_collection = load_employee_data('./employees.data')
dtree = DecisionTree.new data_collection, 'status'
dtree.fit
test_record = {'department' => 'systems', 'age' => '26-30', 'salary' => '46000-50000'}
result = dtree.classify test_record
puts result
end
def test_case5
data_collection = load_employee_data('./employees.data')
correct_count = 0
attr_list = ['deparment', 'age', 'salary']
0.upto (data_collection.size - 1) { |idx|
k1_fold = data_collection.clone
k1_fold.delete_at idx
cdt = DecisionTree.new k1_fold, 'status', attr_list
cdt.fit
if data_collection[idx]['status'] == cdt.classify(data_collection[idx])
correct_count += 1
end
}
printf "%.2f%", 100 * correct_count/data_collection.size.to_f
end
def test_case6
data_collection = load_employee_data('./employees.data')
dt = DecisionTree.new data_collection, 'status'
dt.fit
Graphviz_DT_API.gen_graph dt.root, 'dt.dot'
end
def test_case7
data_collection = load_employee_regened_data('./employees_regened.data')
dt = DecisionTree.new data_collection, 'status'
dt.fit
Graphviz_DT_API.gen_graph dt.root, 'dt.dot'
end
if __FILE__ == $0
test_case6
end