forked from tobegit3hub/ml_implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decision_tree_id3.py
executable file
·187 lines (134 loc) · 5.3 KB
/
decision_tree_id3.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import math
import operator
def calculateShannonEntropy(dataset):
# [[0, 0, 0, 0, 'N'], [0, 0, 1, 1, 'Y']]
instance_number = len(dataset)
# {'Y': 1, 'N': 1}
label_number_map = {}
for instance in dataset:
label = instance[-1]
if label not in label_number_map.keys():
label_number_map[label] = 0
label_number_map[label] += 1
total_shannon_entropy = 0.0
for label in label_number_map:
probability = float(label_number_map[label]) / instance_number
shannon_entropy = probability * math.log(probability, 2) * -1
total_shannon_entropy += shannon_entropy
return total_shannon_entropy
def testCalculateShannonEntropy():
# Should be 1.0
dataset = [[0, 0, 0, 0, 'N'], [0, 0, 1, 1, 'Y']]
print("The shannon entropy is: {}".format(calculateShannonEntropy(dataset)))
# Should be 0.0
dataset = [[0, 0, 0, 0, 'N'], [0, 0, 1, 1, 'N']]
print("The shannon entropy is: {}".format(calculateShannonEntropy(dataset)))
def split_dataset(dataset, feature, value):
""" Get the dataset when "feature" is equal to "value"
"""
reture_data_set = []
# TODO: Example
for instance in dataset:
if instance[feature] == value:
new_instance = instance[:feature]
new_instance.extend(instance[feature + 1:])
reture_data_set.append(new_instance)
return reture_data_set
def choose_best_feature_to_split(dataset):
# Example: 4
feature_number = len(dataset[0]) - 1
base_entropy = calculateShannonEntropy(dataset)
best_info_gain_ratio = 0.0
best_feature = -1
best_after_split_entropy = 0.0
# Example: [0, 0, 0, 0]
for i in range(feature_number):
# Example:
instance_with_one_feature = [instance[i] for instance in dataset]
feature_value_set = set(instance_with_one_feature)
after_split_entropy = 0.0
instrinsic_value = 0.0
# Example: [0, 1]
for value in feature_value_set:
sub_dataset = split_dataset(dataset, i, value)
probability = len(sub_dataset) / float(len(dataset))
after_split_entropy += probability * calculateShannonEntropy(sub_dataset)
instrinsic_value += -probability * math.log(probability, 2)
'''
info_gain = base_entropy - after_split_entropy
# Check if it is zero
if (instrinsic_value == 0):
continue
info_gain_ratio = info_gain / instrinsic_value
if (info_gain_ratio > best_info_gain_ratio):
best_info_gain_ratio = info_gain_ratio
best_feature = i
'''
if after_split_entropy > best_after_split_entropy:
best_after_split_entropy = after_split_entropy
best_feature = i
return best_feature
def create_decision_tree(dataset, header_names):
# Example: [[0, 0, 0, 0, 'N'], [0, 0, 0, 1, 'N'], [1, 0, 0, 0, 'Y']]
# Example: ['N', 'N', 'Y']
labels = [instance[-1] for instance in dataset]
if labels.count(labels[0]) == len(labels):
# Return if all the values are the same
return labels[0]
# Example: ['N']
if len(dataset[0]) == 1:
label_count_map = {}
for label in labels:
if label not in label_count_map.keys():
label_count_map[label] = 0
label_count_map[label] += 1
sorted_label_count_map = sorted(
label_count_map.iteritems(), key=operator.itemgetter(1), reversed=True)
return sorted_label_count_map[0][0]
best_feature_id = choose_best_feature_to_split(dataset)
header_name = header_names[best_feature_id]
decision_tree = {header_name: {}}
# TODO: don't modify the input parameter
del (header_names[best_feature_id])
all_feature_values = [instance[best_feature_id] for instance in dataset]
unique_feature_values = set(all_feature_values)
for value in unique_feature_values:
sub_header_names = header_names[:]
sub_dataset = split_dataset(dataset, best_feature_id, value)
decision_tree[header_name][value] = create_decision_tree(
sub_dataset, sub_header_names)
return decision_tree
def predict(decision_tree, header_names, test_dataset):
# Example: {'outlook': {0: 'N', 1: 'Y', 2: {'windy': {0: 'Y', 1: 'N'}}}}
# print("Current tree: {}".format(decision_tree))
# Example: "outlook"
root_key = list(decision_tree.keys())[0]
# Example: {0: 'N', 1: 'Y', 2: {'windy': {0: 'Y', 1: 'N'}}}
sub_decision_tree = decision_tree[root_key]
# Example: 0
feature_index = header_names.index(root_key)
for key in sub_decision_tree.keys():
if test_dataset[feature_index] == key:
if type(sub_decision_tree[key]).__name__ == 'dict':
predict_label = predict(sub_decision_tree[key], header_names,
test_dataset)
else:
predict_label = sub_decision_tree[key]
return predict_label
def main():
# Train
dataset = [[0, 0, 0, 0, 'N'], [0, 0, 0, 1, 'N'], [1, 0, 0, 0, 'Y'],
[2, 1, 0, 0, 'Y'], [2, 2, 1, 0, 'Y'], [2, 2, 1, 1,
'N'], [1, 2, 1, 1, 'Y']]
header_names = ['outlook', 'temperature', 'humidity', 'windy']
decision_tree = create_decision_tree(dataset, header_names)
print("Train and get decision tree: {}".format(decision_tree))
# Test
header_names = ['outlook', 'temperature', 'humidity', 'windy']
test_dataset = [2, 1, 0, 0]
result = predict(decision_tree, header_names, test_dataset)
print("Predict decision tree and get result: {}".format(result))
if __name__ == "__main__":
main()