-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_criteria.py
421 lines (318 loc) · 13.8 KB
/
split_criteria.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
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
import numpy as np
import pandas as pd
def split_df(split_value, df:pd.DataFrame):
'''
Split the data according to the split value.
Args:
split_value:
The split value.
df (DataFrame):
The DataFrame for spliting.
Returns:
A tuple with a DataFrame contains attribute values smaller or equal to split_value at position 0 and another half at position 1.
'''
df_l = df[df.iloc[:,0]<=split_value]
df_r = df[df.iloc[:,0]>split_value]
return df_l, df_r
def MSE(df_each:pd.DataFrame, df:pd.DataFrame) -> float:
'''
Calculate the mean square error according to the split category.
Args:
df_each (DataFrame):
A DataFrame contains one category of the attribute in df.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
mse (float):
The mean square error of spliting according to the specified category.
'''
each_mean = np.mean(df_each['label'])
df_rest = df[df.iloc[:,0]!=df_each.iloc[0,0]]
rest_mean = np.mean(df_rest['label'])
mse = (np.sum(np.square(df_each['label']-each_mean)) + np.sum(np.square(df_rest['label']-rest_mean))) / df.shape[0]
return mse
def mean_square_error(df:pd.DataFrame) -> pd.Series:
'''
Calculate all the mean square error of choosing each category of the attribute as the split category.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
mse (Series):
A Series contains all the mean square error of choosing each category of the attribute as the split category.
'''
mse = df.groupby(df.columns[0]).apply(MSE, df=df)
return mse
def SSE(split_value:float, df:pd.DataFrame) -> float:
'''
Calculate the sum square error according to the split value.
Args:
split_value (float):
The split value.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
sse (float):
The sum square error.
'''
#split the data according to the split value
df_l, df_r = split_df(split_value, df)
#calculate each labels mean
mean_l = np.mean(df_l["label"])
mean_r = np.mean(df_r["label"])
#calculate the sum square error
sse = np.sum(np.square(df_l['label']-mean_l)) + np.sum(np.square(df_r['label']-mean_r))
return sse
def sum_square_error(df:pd.DataFrame):
'''
Calculate all the sum square errors of choosing each value of the attribute as the split value.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
sses (Series):
A Series contains all the sum square errors of choosing each value of the attribute as the split value.
'''
#the sum square errors of choosing each value of the attribute as the split value
sses = df.iloc[:,0].apply(SSE, df=df)
return sses
def entropy(df:pd.DataFrame) -> list:
'''
Calculate the entropy for the given attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
entropy (list):
A list with the entropy at position 0 and the number of rows in df at position 1.
'''
proportion = np.array(df.groupby(df.columns[1]).apply("count").iloc[:,0])/df.shape[0] #the proportion of each label category
entr = -np.sum(proportion*np.log2(proportion)) #calculate the entropy
return [entr, df.shape[0]]
def entropy_continuous(split_value, df:pd.DataFrame):
'''
Calculate the entropy for each split value.
Args:
split_value:
The split value.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
entr (Series):
A Series contains all the entropy of spliting by each split value.
'''
#split the data according to the split value
df_l, df_r = split_df(split_value, df)
#calculate the sum of entropy * proportion
entr_l, entr_r = entropy(df_l), entropy(df_r)
entr = entr_l[0] * entr_l[1] + entr_r[0] * entr_r[1]
entr /= df.shape[0]
return entr
def gain_ratio_continuous(df:pd.DataFrame) -> pd.Series:
'''
Calculate the inverse of Gain ratio for choosing each value of the given continuous attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
gain (Series):
A Series contains the inverse of Gain ratios of choosing each value as the split value for the attribute.
'''
#the entropy of this attribute
node_entropy = entropy(df)[0]
#calculate the entropy of selecting each value as the split value
all_entr = df.iloc[:,0].apply(entropy_continuous, df=df)
#calculate the Gain ratio
gain = 1 / (node_entropy - all_entr)
return gain
def entropy_categorical(split_cat, df:pd.DataFrame):
'''
Calculate the entropy for the categories besides the selected one.
Args:
split_cat:
The split category.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
entropy_rest (list):
A list with the entropy at position 0 and the number of rows in df at position 1.
'''
df_rest = df[df.iloc[:,0]!=split_cat]
entropy_rest = entropy(df_rest)
return entropy_rest
def gain_ratio_categorical(df:pd.DataFrame) -> pd.Series:
'''
Calculate the inverse of Gain ratio for choosing each category of the given categorical attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
gain (Series):
A Series contains the inverse of Gain ratios of choosing each category as the split category for the attribute.
'''
#the entropy of this attribute
node_entropy = entropy(df)[0]
#calculate the entropy of each attribute category
each_entropy = df.groupby(df.columns[0]).apply(entropy)
each_entropy = pd.DataFrame(list(each_entropy), index=each_entropy.index, columns=["entropy", "count"])
#calculate the entropy of the rest categories
not_entropy = pd.Series(each_entropy.index).apply(entropy_categorical, df=df)
not_entropy = pd.DataFrame(list(not_entropy), index=each_entropy.index, columns=["not_entropy", "not_count"])
#concate each_entropy with not_entropy
each_entropy = pd.concat([each_entropy, not_entropy], axis=1)
#calculate the Gain ratio
gain = each_entropy['entropy'] * each_entropy['count'] + each_entropy['not_entropy'] * each_entropy['not_count']
gain /= df.shape[0]
gain = 1 / (node_entropy - gain)
return gain
def each_gini(df:pd.DataFrame):
'''
Calculate the Gini index for each category of the given attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
gini_each (list):
A list with Gini index at position 0 and the number of rows in df at position 1.
'''
proportion = np.array(df.groupby(df.columns[1]).apply("count").iloc[:,0])/df.shape[0] #the proportion of each label category
gini_each = 1 - np.sum(np.square(proportion))
return [gini_each, df.shape[0]]
def rest_gini_categorical(split_cat, df:pd.DataFrame):
'''
Calculate the Gini index for the categories besides the selected one.
Args:
split_cat:
The split category.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
entropy_rest (list):
A list with the Gini index at position 0 and the number of rows in df at position 1.
'''
df_rest = df[df.iloc[:,0]!=split_cat]
gini_rest = each_gini(df_rest)
return gini_rest
def gini_categorical(df:pd.DataFrame):
'''
Calculate the overall Gini index of the given categorical attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
gini_index (Series)
A Series contains the Gini index of choosing each category as the split category for the attribute.
'''
#calculate the Gini index of each attribute category
gini_each = df.groupby(df.columns[0]).apply(each_gini)
gini_each = pd.DataFrame(list(gini_each), index=gini_each.index, columns=["each gini", "count"])
#calculate the entropy of the rest categories
not_gini = pd.Series(gini_each.index).apply(rest_gini_categorical, df=df)
not_gini = pd.DataFrame(list(not_gini), index=gini_each.index, columns=["not_gini", "not_count"])
#concat gini_each with not_gini
gini_each = pd.concat([gini_each, not_gini], axis=1)
#calculate the Gini index
gini_index = gini_each['each gini'] * gini_each['count'] + gini_each['not_gini'] * gini_each['not_count']
gini_index /= df.shape[0]
return gini_index
def gini_each_continuous(split_value, df:pd.DataFrame):
'''
Calculate the Gini index for each split value.
Args:
split_value:
The split value.
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
entr (Series):
A Series contains all the Gini index of spliting by each split value.
'''
#split the data according to the split value
df_l, df_r = split_df(split_value, df)
#calculate the sum of Gini index * proportion
gini_l, gini_r = each_gini(df_l), each_gini(df_r)
gini = gini_l[0] * gini_l[1] + gini_r[0] * gini_r[1]
gini /= df.shape[0]
return gini
def gini_continuous(df:pd.DataFrame):
'''
Calculate the overall Gini index of the given continuous attribute.
Args:
df (DataFrame):
A DataFrame contains one column of a specified attribute and one column of labels.
Returns:
gini_index (Series)
A Series contains the Gini index of choosing each value as the split value for the attribute.
'''
#calculate the Gini index of selecting each value as the split value
all_gini = df.iloc[:,0].apply(gini_each_continuous, df=df)
return all_gini
if __name__ == "__main__":
#watermelon
df1 = pd.DataFrame([["green", "yes"],
["black", "yes"],
["black", "yes"],
["green", "yes"],
["white", "yes"],
["green", "yes"],
["black", "yes"],
["black", "yes"],
["black", "no"],
["green", "no"],
["white", "no"],
["white", "no"],
["green", "no"],
["white", "no"],
["black", "no"],
["white", "no"],
["green", "no"],], columns=['root color', 'label'])
#defaults
df2 = pd.DataFrame([["yes", "no"],
["yes", "no"],
["yes", "no"],
["no", "no"],
["no", "no"],
["no", "no"],
["no", "no"],
["no", "yes"],
["no", "yes"],
["no", "yes"],], columns=["has house", "label"])
df3 = pd.DataFrame([["single", "not"],
["married", "not"],
["single", "not"],
["married", "not"],
["divorce", "yes"],
["married", "not"],
["divorce", "not"],
["single", "yes"],
["married", "not"],
["single", "yes"],], columns=["marital status", "label"])
df4 = pd.DataFrame([[1, 5.56],
[2, 5.7],
[3, 5.91],
[4, 6.4],
[5, 6.8],
[6, 7.05],
[7, 8.9],
[8, 8.7],
[9, 9],
[10, 9.05]], columns=['x', 'label'])
df5 = pd.DataFrame([[5.56, 1],
[5.7, 1],
[5.91,1],
[6.4, 2],
[6.8, 1],
[7.05, 2],
[8.9, 3],
[8.7, 3],
[9, 2],
[9.05, 3]], columns=['x', 'label'])
df6 = pd.concat([df3['marital status'], df4['label']], axis=1)
a = gain_ratio_continuous(df5)
print(a)
#print(a[np.argmin(a)])
#print(sum_square_error(df4))
#print(gini_continuous(df5))
#print(gini_categorical(df3))
""" print(df6)
print(mean_square_error(df6)) """