决策树构建

文章发布时间:

最后更新时间:

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
import numpy as np

import copy

# You can modify the code as what you want



class decision_tree:

    def __init__(self, features, output, dataset):

        self.features = features

        self.output = output

        self.dataset = dataset

        self.depth = 0        

    def log(self, x):

        return np.log2(x)

    # you can use this function to calculate the emprical probability of a random variable under a dataset

    def get_prob(self, array):

        (unique, counts) = np.unique(array, return_counts=True, axis=0)

        return counts/len(array)

    # you can use this function to calculate the emprical entropy of a random variable under a dataset

    def entropy(self, array):

        p = self.get_prob(array)

        return -np.sum(p*np.log2(p))

    def output_entropy(self):

        # calculate the emprical entropy of the output

        output_array = []

        for data in self.dataset:

            output_array.append(data.get(self.output))

        #print(self.entropy(output_array)) #测试用

        return self.entropy(output_array)

    def conditional_entropy(self, feature):

        # calculate the emprical conditional entropy of the output relative to the "feature"

        feature_array = []

        for data in self.dataset:

            feature_array.append(data.get(feature))

        # 计算权值

        weight_array = self.get_prob(feature_array)

        (unique, counts) = np.unique(feature_array, return_counts=True)

        # 条件下的经验熵

        entropy_array = []

        for u in unique:

            list = []

            for data in self.dataset:

                if data.get(feature) == u:

                    list.append(data.get(self.output))

            entropy_array.append(self.entropy(list))

        #加权计算得到最后的条件熵

        result = np.sum(np.array(weight_array) * entropy_array)

        return result

    def feature_selection(self):

        # select the feature has maximum mutual information

        v = 0

        selected = '我是字符串'

        for f in self.features:

            #print("互信息为",self.output_entropy()-self.conditional_entropy(f))

            v1 = self.output_entropy()-self.conditional_entropy(f)

            if v1 >= v:              # 这里要取等号,不然互信息为0的时候特征选择会选成“我是字符串”

                selected = f         # 出现这种现象很可能是分类已经很细了,样本数据太小(bug找了半天)

        #print("现在选择:",selected)

        return selected

# 自己加的一个函数

    def tof(self,classify_dataset): # 判断分类 true or false,就是比较分类里去图书馆的人多一些还不去的多一些

        (unique, counts) = np.unique([d.get(self.output) for d in classify_dataset],return_counts=True)

        # 对这个分类下结论

        #print(unique,counts)

        if len(counts) == 2:          # false 和 true 是不是同时有

            if counts[0] > counts[1]: # false 比 true 多

                return unique[0]

            else:

                return unique[1]

        else:

            return unique[0]

# 自己加的又一个函数,不然predict这个函数看起来太大了

    def classify(self,selected_feature,u): # 得到分类好的数据集

        # 划分数据集合

        classify_dataset = []

        for d in self.dataset:

            if d.get(selected_feature) == u:

                classify_dataset.append(d)

        #print("该分类的元素个数",len(classify_dataset))

        #print(classify_dataset) # 打印分类好的数据集

        return classify_dataset



# 又又加的

    def train(self, data):

        # 训练构建决策树

        global decision_order,depth,max_depth,tree,branch

        depth = depth + 1

        #print("depth -----------",depth)

        if depth <= max_depth:

            selected_feature = self.feature_selection()# 选出当前最优标签

            if  len(data) == 0: # 只有一个特征标签时就停止进入递归

                depth = depth - 1

                max_depth = depth

                print("最大深度为-------------",max_depth)

                return

            else:

                if max_depth == 100:

                    decision_order.append(selected_feature)   # 当前最优标签加入决策序列

                feature_array = []                        # 依据当前最优标签分成几叉

                for d in self.dataset:

                    feature_array.append(d.get(decision_order[depth-1]))

                (unique, counts) = np.unique(feature_array, return_counts=True)

                decision_feature=list(data)

                decision_feature.remove(selected_feature) # 接下来的决策标签 要除去当且最优标签

                # 遍历这个特征所有的值

                for u in unique:

                    #print("目前所在边为:",selected_feature,u)

                    branch.append(u)

                    classify_dataset=self.classify(selected_feature,u)

                    # 递归进入下一层

                    if depth != max_depth:

                        predict_tree=decision_tree(decision_feature,self.output,classify_dataset)

                        predict_tree.train(decision_feature)

                        #print("当前深度为",depth)

                    # 对这个分类下结论

                    if depth == max_depth:

                        a = self.tof(classify_dataset)

                        tree[tuple(branch)]=a

                    branch.pop(-1)

                depth = depth - 1

        return



    def predict(self, data):

        data1 = copy.deepcopy(data)

        if self.output in data1: # 如果输入的特征里有决策的特征就丢掉

            del data1[self.output]



        # make prediction for an arbitrary data input

        if tree == {}:

            self.train(data1) # 构建决策树

            #print(tree) # 打印决策树



        # 依据构建的决策树做判断

        if len(data1)==len(decision_order):

            #print(tree[tuple([data.get(d) for d in decision_order])])

            my_judge = tree[tuple([data1.get(d) for d in decision_order])]

            #print(my_judge,1231232131)

            return my_judge

        return




decision_order = [] # 控制决策顺序

depth = 0          # 深度

max_depth = 100    # 最大深度

tree = {}

branch = []



dataset = [

    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 19, "male": True,  "single": False, "visit_library_in_Sunday": True},

    {"age": 19, "male": True,  "single": True,  "visit_library_in_Sunday": True},

    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 20, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 20, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 20, "male": True,  "single": True,  "visit_library_in_Sunday": True},

    {"age": 20, "male": False, "single": True,  "visit_library_in_Sunday": True},

    {"age": 20, "male": False, "single": True,  "visit_library_in_Sunday": True},

    {"age": 21, "male": False, "single": True,  "visit_library_in_Sunday": True},

    {"age": 21, "male": False, "single": True,  "visit_library_in_Sunday": True},

    {"age": 21, "male": True,  "single": False, "visit_library_in_Sunday": True},

    {"age": 21, "male": True,  "single": False, "visit_library_in_Sunday": True},

    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": False},

    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": True}

]  

my_tree = decision_tree(\

    ["age", "male", "single"], "visit_library_in_Sunday", dataset)



# Test 1

print(my_tree.predict({'age': 19, 'male': False, 'single': False}), "should be 0 or False")



# Test 2

# use the feature "single" to classify as last time

def tree_example(data):

    if data["single"]:

        return True

    else:

        return False

true_classification = 0

for data in dataset:

    if tree_example(data) == data["visit_library_in_Sunday"]:

        true_classification += 1



print("One feature classification accuracy:", true_classification/len(dataset))



# use a decision tree to classify

true_classification = 0



for data in dataset:

    if my_tree.predict(data) == data["visit_library_in_Sunday"]:

        true_classification += 1

print("Decision tree classification accuracy:", round(true_classification/len(dataset),4), "should be around 0.9412")
1
2
3
4
最大深度为------------- 3
False should be 0 or False
One feature classification accuracy: 0.7647058823529411
Decision tree classification accuracy: 0.9412 should be around 0.9412