-
Notifications
You must be signed in to change notification settings - Fork 0
/
p45.py
1420 lines (1077 loc) · 42.2 KB
/
p45.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# P45 - A Python C4.5 implementation
#
# Copyright 2022 Fernando Esteban Barril Otero
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
from datetime import datetime
import math
import numpy as np
import pandas as pd
import random
from collections import Counter
from datetime import datetime
from halo import Halo
from json import load
from pandas.api.types import is_numeric_dtype
# minimum difference between continuous values
DELTA = 1e-5
# minumum number of instances for each node
MINIMUM_INSTANCES = 2
# limit for the minimum number of instances in a continuous interval
INTERVAL_LIMIT = 25
# correction value used in the threshold and error calculation (based on C4.5)
EPSILON = 1e-3
# precision correction
CORRECTION = 1e-10
# values below this are considered zero for gain ratio calculation
ZERO = 1e-7
# make sure it is not trying to modify a copy of a dataframe
pd.options.mode.chained_assignment = 'raise'
# sets a fixed random seed
random.seed(0)
# (dis/en)able the pruning
PRUNE = True
#
# Gain Ratio calculation
# ---------
def gain_ratio(data, attribute, weights):
"""Calculates the gain ratio of the specified attribute
Parameters
----------
data : DataFrame
The current data
attribute : str
The name of the attribute
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
tuple
a tuple representing the (gain ratio, gain, split information) of the attribute
"""
# class values present in the data
filtered = data[attribute].notna()
class_values = list(data.iloc[data[filtered].index, -1].unique())
S = []
for c in class_values:
membership = (data.iloc[data[filtered].index, -1] == c)
S.append(weights[membership & filtered].sum())
values = list(data.loc[filtered, attribute].unique())
# number of missing values
missing = weights[data[attribute].isna()].sum()
# sum of instances with known values
length = weights.sum() - missing
total_entropy = 0
# calculates the entropy of the whole data
for s in S:
p = s / length
total_entropy -= (p * np.log2(p))
# calculates the entropy of the partition based on the attribute
partition_entropy = 0
partition_split = 0
for v in values:
partition = (data[attribute] == v)
partition_length = weights[partition].sum()
entropy = 0
for c in class_values:
membership = (data.iloc[data[partition].index, -1] == c)
count = weights[membership & partition].sum()
if count > 0:
p = count / partition_length
entropy -= (p * np.log2(p))
partition_entropy += (partition_length / length) * entropy
split = partition_length / (length + missing)
partition_split -= split * np.log2(split)
if missing > 0:
m = missing / (length + missing)
partition_split -= m * np.log2(m)
gain = (length / (length + missing)) * (total_entropy - partition_entropy)
gain_ratio = 0 if gain == 0 else gain / partition_split
return gain_ratio, gain, partition_split
def candidate_thresholds(data, attribute, weights):
"""Generates the candidates threshold values for a continuous attribute
Parameters
----------
data : DataFrame
The current data
attribute : str
The name of the attribute
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
list
a list of values representing the candidate threshold values
"""
valid = data[attribute].notna()
values = list(zip(np.array(data.loc[valid, attribute]), weights[valid]))
values.sort()
length = weights[valid].sum()
interval_length = 0
thresholds = []
# minimum number of instances per interval (according to C4.5)
class_values = list(data.iloc[data[valid].index, -1].unique())
min_split = 0.1 * (length / len(class_values))
if min_split <= MINIMUM_INSTANCES:
min_split = MINIMUM_INSTANCES
elif min_split > INTERVAL_LIMIT:
min_split = INTERVAL_LIMIT
for s in range(len(values) - 1):
interval_length += values[s][1]
length -= values[s][1]
if (values[s][0] + DELTA) < values[s + 1][0] and (interval_length + CORRECTION) >= min_split and (length + CORRECTION) >= min_split:
thresholds.append((values[s][0] + values[s + 1][0]) / 2)
return thresholds
def gain_ratio_numeric(data, attribute, weights):
"""Calculates the gain ratio of the specified numeric attribute
This function evaluates multiple thresholds values to dynamically discretise the
continuous attribute and calculate the gain ratio information.
Parameters
----------
data : DataFrame
The current data
attribute : str
The name of the attribute
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
tuple
a tuple representing the (gain ratio, gain, threshold) of the attribute
"""
# the list of threshold values
thresholds = candidate_thresholds(data, attribute, weights)
if len(thresholds) == 0:
return 0, 0, 0
valid_data = data[data[attribute].notna()].copy().reset_index(drop=True)
valid_weights = weights[data[attribute].notna()]
# saves a copy of the original values
values = valid_data[attribute].copy()
# sum of instances with known outcome
length = valid_weights.sum()
gain_correction = length / weights.sum()
penalty = np.log2(len(thresholds)) / weights.sum()
gain_information = []
for t in thresholds:
# create a binary column representing the threshold division
binary_split = ['H' if v > t else 'L' for v in values]
valid_data[attribute] = binary_split
_, gain, split = gain_ratio(valid_data, attribute, valid_weights)
# apply a penalty for evaluating multiple threshold values (based on C4.5)
gain = (gain_correction * gain) - penalty
ratio = 0 if gain == 0 or split == 0 else gain / split
gain_information.append((ratio, gain))
thresholds_gain = [g[1] for g in gain_information]
selected = np.argmax(thresholds_gain)
return gain_information[selected][0], gain_information[selected][1], thresholds[selected]
#
# Attribute selection
# ---------
def search_best_attribute(data, weights):
"""Search for the best attribute to create a decision node
This function searches for the best attribute for a decision node. For each attribute,
it calculates the gain ratio. The selected attribute is the one with the highest
gain ratio.
Parameters
----------
data : DataFrame
The current data
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
str, tuple
the name of the selected attribute and its associated information. For categorical
attributes, it is a tuple (gain ratio, gain, split information); for continuous
attributes, it is a tuple (gain ratio, gain, threshold)
"""
predictors = data.iloc[:, 0:-1]
if len(predictors.columns) == 0:
# no attributes left to choose
return None, (0, 0, 0)
candidates = []
average_gain = 0
for attribute in predictors.columns:
if is_numeric_dtype(data[attribute]):
c = attribute, gain_ratio_numeric(data, attribute, weights)
else:
c = attribute, gain_ratio(data, attribute, weights)
# only consider positive gains
if c[1][1] > 0:
average_gain += c[1][1]
candidates.append(c)
if len(candidates) == 0:
# no suitable attribute
return None, (0, 0, 0)
average_gain = (average_gain / len(candidates)) - EPSILON
selected = None
# [0] gain ratio
# [1] gain
# [2] split informaton / threshold
best = (ZERO, ZERO, ZERO)
for attribute, c in candidates:
if c[0] > best[0] and c[1] >= average_gain:
selected = attribute
best = c
return selected, best
#
# Error estimation
# ---------
coefficient_value = [0, 0.001, 0.005, 0.01, 0.05, 0.10, 0.20, 0.40, 1.00]
deviation = [4.0, 3.09, 2.58, 2.33, 1.65, 1.28, 0.84, 0.25, 0.00]
CF = 0.25
coefficient_index = 0
while CF > coefficient_value[coefficient_index]:
coefficient_index += 1
coefficient = deviation[coefficient_index - 1] + (deviation[coefficient_index] - deviation[coefficient_index - 1]) * (
CF - coefficient_value[coefficient_index - 1]) / (coefficient_value[coefficient_index] - coefficient_value[coefficient_index - 1])
coefficient = coefficient * coefficient
def estimate_error(total, errors):
"""Estimates the prediction error.
Parameters
----------
total : int
The total number of predictions
errors : int
The number of incorrect predictions
Returns
-------
int
the estimated errors
"""
if total == 0:
return 0
elif errors < 1e-6:
return total * (1 - math.exp(math.log(CF) / total))
elif errors < 0.9999:
v = total * (1 - math.exp(math.log(CF) / total))
return v + errors * (estimate_error(total, 1.0) - v)
elif errors + 0.5 >= total:
return 0.67 * (total - errors)
else:
pr = (errors + 0.5 + coefficient / 2 + math.sqrt(coefficient * ((errors + 0.5)
* (1 - (errors + 0.5) / total) + coefficient / 4))) / (total + coefficient)
return (total * pr - errors)
#
# Data structures
# ---------
class Operator:
"""
Enum-like class to represent different operators
"""
EQUAL = 1
LESS_OR_EQUAL = 2
GREATER = 3
class Node:
"""
A class used to represent a node of the decision tree.
Each node can have a number of child nodes (internal nodes) or none (leaf nodes).
The root of the tree is also represented as a node.
"""
def __init__(self, attribute, parent=None, error=0, total=0, distribution=None):
"""
Parameters
----------
attribute : str
The name of the attribute represented by the node (internal nodes) or
the class value predicted (leaf nodes)
parent : Node, optional
The parent node of the node
error:
The number of prediction errors (leaf nodes)
total:
The number of instances reaching the node (leaf nodes)
"""
self.attribute = attribute
self.parent = parent
# private-like attributes
self._error = error
self._total = total
self._distribution = distribution
self.level = 0 if parent is None else parent.level + 1
self.children = []
self.conditions = []
self.operators = []
@property
def classes(self):
"""Return the list of classes that the node (tree) can predict.
This method can only be used on the root node of the tree.
Returns
-------
list
the list of classes that the node (tree) can predict.
"""
return self._classes
@classes.setter
def classes(self, classes):
"""Set the list of classes that the node (tree) can predict.
This list is used to determine the order of the classification probabilities.
Parameters
----------
classes : list
The list of class values that the node can predict.
"""
self._classes = classes
def add(self, node, condition, operator=Operator.EQUAL):
"""Adds a child node
The node will be added at the end of a branch. The condition and operator are
used to decide when to follow the branch to the node
Parameters
----------
node : Node
The node to add
condition : str or float
The value to decide when to choose to visit the node
operator : Operator, optional
The operator to decide when to choose to visit the node
"""
node.parent = self
self.children.append(node)
self.conditions.append(condition)
self.operators.append(operator)
def to_text(self):
"""Prints a textual representation of the node
This method prints the node and any of its child nodes
"""
self.__print_node("")
def __print_node(self, prefix):
"""Prints a textual representation of the node
This method prints the node and any of its child nodes recusively
Parameters
----------
prefix : str
The prefix to be used to print the node
"""
if self.is_leaf():
if self._error > 0:
print("{} ({:.1f}/{:.1f})".format(self.attribute,
self._total,
self._error), end="")
else:
print("{} ({:.1f})".format(self.attribute,
self._total), end="")
else:
if len(prefix) > 0:
print("")
for i, v in enumerate(self.conditions):
if i > 0:
print("")
operator = None
if self.operators[i] == Operator.EQUAL:
operator = "="
elif self.operators[i] == Operator.LESS_OR_EQUAL:
operator = "<="
elif self.operators[i] == Operator.GREATER:
operator = ">"
print("{}{} {} {}: ".format(
prefix, self.attribute, operator, v), end="")
self.children[i].__print_node(prefix + "| ")
def is_leaf(self):
"""Checks whether the node is a leaf node
Returns
-------
bool
True if the node is a leaf node; otherwise False
"""
return len(self.conditions) == 0
def predict(self, instance):
"""Classify the specified instance
This method expects that the instance (row) if a slice of a dataframe with the
same attributes names as the one used to create the tree
Parameters
----------
instance : DataFrame slice
The instance (row) to be classified
Returns
-------
str
The class value predicted
"""
probabilities = Node.__predict(instance, self, 1.0)
prediction = ("", 0)
for value, count in probabilities.items():
if count > prediction[1]:
prediction = (value, count)
if prediction[1] > 0:
return prediction[0]
raise Exception(
f"Could not predict a value: probabilities={str(dict(probabilities))}")
def probabilities(self, instance):
"""Classify the specified instance, returning the probability of each class value
prediction.
This method expects that the instance (row) is a slice of a dataframe with the
same attributes names as the one used to create the tree. The order of the class
values is determined by the ``self.classes`` property.
Parameters
----------
instance : DataFrame slice
The instance (row) to be classified
Returns
-------
list
list of class value probabilities
"""
probabilities = Node.__predict(instance, self, 1.0)
prediction = []
for value in self.classes:
prediction.append(probabilities[value])
return prediction
def __predict(instance, node, weight):
"""Classify the specified instance
This method expects that the instance (row) if a slice of a dataframe with the
same attributes names as the one used to create the tree
Parameters
----------
instance : DataFrame slice
The instance (row) to be classified
Returns
-------
str
The class value predicted
"""
probabilities = Counter()
# in case the node is a leaf
if node.is_leaf():
for value, count in node._distribution.items():
if count > 0:
probabilities[value] = weight * (count / node._total)
if node._total == 0:
probabilities[node.attribute] = weight
return probabilities
# if not, find the branch to follow
value = instance[node.attribute]
if pd.isna(value):
total = node.total()
for i, v in enumerate(node.conditions):
w = node.children[i].total() / total
probabilities += Node.__predict(instance,
node.children[i], weight * w)
else:
match = False
for i, v in enumerate(node.conditions):
if node.operators[i] == Operator.EQUAL and value == v:
match = True
elif node.operators[i] == Operator.LESS_OR_EQUAL and value <= v:
match = True
elif node.operators[i] == Operator.GREATER and value > v:
match = True
if match:
probabilities += Node.__predict(instance,
node.children[i], weight)
break
if not match:
raise Exception(
f"Cound not match value {value} for attribute {node.attribute}")
return probabilities
def total(self):
"""Returns the number of instances reaching the node
For internal nodes, this is the sum of the total from its child nodes
Returns
-------
int
the number of instances reaching the node
"""
if self.is_leaf():
return self._total
else:
t = 0
for node in self.children:
t += node.total()
return t
def error(self):
"""Returns the number of prediction errors observed during the creation of the tree
For internal nodes, this is the sum of the errors from its child nodes
Returns
-------
int
the number of prediction errors observed during the creation of the tree
"""
if self.is_leaf():
return self._error
else:
e = 0
for node in self.children:
e += node.error()
return e
def estimated(self):
"""Returns the number of estimated errors observed during the creation of the tree
For internal nodes, this is the sum of the estimated errors from its child nodes
Returns
-------
float
the number of estimated errors observed during the creation of the tree
"""
if self.is_leaf():
return self._error + estimate_error(self._total, self._error)
else:
e = 0
for node in self.children:
e += node.estimated()
return e
def adjust(self, data):
"""Replaces the threshold values of continuous attributes with values that occur
on the training data
The discretisation uses the average value between two consecutive values to
evaluate thresholds.
Parameters
----------
data : DataFrame
The training data
"""
if not self.is_leaf():
ordered = []
# we only need to look at one of the operators/conditions since the
# threshold value will be the same in both branches
operator = self.operators[0]
threshold = self.conditions[0]
if operator == Operator.LESS_OR_EQUAL or operator == Operator.GREATER:
sorted_values = np.array(data[self.attribute])
sorted_values.sort()
selected = threshold
for v in sorted_values:
if v > threshold:
break
selected = v
self.conditions = [selected] * len(self.conditions)
for child in self.children:
child.adjust(data)
def estimate_with_data(self, metadata, data, weights, update=False):
"""Returns the number of estimated errors observed on the specified data, updating
the values if update=True (default False)
For internal nodes, this is the sum of the estimated errors from its child nodes
Parameters
----------
metadata : dict
The attribute information
data : DataFrame
The data to use
weights : np.array
The instance weights to be used in the length calculation
update : bool
Indicate whether the error values should be updated or not
Returns
-------
float
the number of estimated errors
"""
if self.is_leaf():
class_attribute = data.iloc[:, -1]
total = weights.sum()
correct_predictions = 0 if total == 0 else weights[class_attribute == self.attribute].sum(
)
error = total - correct_predictions
if update:
# class value = count
distribution = Counter()
for value in metadata[data.columns[-1]]:
distribution[value] = weights[class_attribute == value].sum()
self._distribution = distribution
self._total = total
self._error = error
return error + estimate_error(total, error)
else:
missing = data[self.attribute].isna()
known_length = weights.sum() - weights[missing].sum()
total = 0.0
for i, v in enumerate(self.conditions):
if self.operators[i] == Operator.EQUAL:
partition = (data[self.attribute] == v)
elif self.operators[i] == Operator.LESS_OR_EQUAL:
partition = (data[self.attribute] <= v)
elif self.operators[i] == Operator.GREATER:
partition = (data[self.attribute] > v)
updated_weights = weights.copy()
w = weights[partition].sum() / known_length
updated_weights[missing] = updated_weights[missing] * w
updated_weights = updated_weights[partition | missing]
if is_numeric_dtype(data[self.attribute]):
branch_data = data[partition |
missing].reset_index(drop=True)
else:
branch_data = data[partition | missing].drop(
columns=self.attribute).reset_index(drop=True)
total += self.children[i].estimate_with_data(
metadata, branch_data, updated_weights, update)
return total
def sort(self):
"""Sort the branches of the node, placing leaf nodes at the start of the children
array.
This method improves the shape of the node (tree) for visualisation - there is
no difference it terms of the performance of the tree.
"""
for i in range(len(self.children)):
if not self.children[i].is_leaf():
to_index = -1
for j in range(i + 1, len(self.children)):
if self.children[j].is_leaf():
to_index = j
break
if to_index == -1:
self.children[i].sort()
else:
child = self.children[to_index]
condition = self.conditions[to_index]
operator = self.operators[to_index]
for j in range(to_index, i, -1):
self.children[j] = self.children[j - 1]
self.conditions[j] = self.conditions[j - 1]
self.operators[j] = self.operators[j - 1]
self.children[i] = child
self.conditions[i] = condition
self.operators[i] = operator
#
# Building the tree
# ---------
def calculate_majority(class_attribute, weights):
"""Finds the majority class value based on the weights of the instances
Note that in case there are more than one value with the same distribution,
a random value among those is returned.
Parameters
----------
class_attribute : Series
The class values of the instances
weights : np.array
The instance weights to be used in the majority calculation
Returns
-------
str
the majority class value
"""
majority = []
best = 0
for value in class_attribute.unique():
count = weights[class_attribute == value].sum()
if count > best:
majority.clear()
majority.append(value)
best = count
elif count == best:
majority.append(value)
return majority[random.randrange(len(majority))] if len(majority) > 0 else None
def pre_prune(metadata, data, majority, node, weights):
"""Performs a pre-pruning test to decide whether to replace an internal node by a leaf
node or not
Parameters
----------
metadata : dict
The attribute information
data : DataFrame
The training data
majority : str
The majority class value if the node is replaced by a leaf node
node : Node
The node undergoing pruning
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
Node
the node in case its error is lower; otherwise a leaf node to be used instead
"""
class_attribute = data.iloc[:, -1]
length = weights.sum()
correct_predictions = 0
if length > 0:
majority = calculate_majority(class_attribute, weights)
correct_predictions = weights[class_attribute == majority].sum()
leaf_error = length - correct_predictions
if node.error() >= leaf_error - EPSILON:
# class value : count
distribution = Counter()
for value in metadata[data.columns[-1]]:
distribution[value] = weights[class_attribute == value].sum()
return Node(majority, node.parent, leaf_error, length, distribution)
return node
def post_prune(metadata, data, node, majority, weights):
"""Performs a pessimistic prunnig on the newly created subtree, which prunes
nodes based on an estimated error penalty
The pruning procedure involves:
1. estimating the node (sub-tree) error
2. estimating the error if the node (sub-tree) is replaced by a leaf node
3. estimating the error if the node is replaced by the most frequent branch
In case of (2) and (3) generating a smaller error, the node is replaced by
the correspnding node.
Parameters
----------
metadata : dict
The attribute information
data : DataFrame
The training data
node : Node
The node undergoing pruning
majority : str
The majority class value if the node is replaced by a leaf node
weights : np.array
The instance weights to be used in the length calculation
Returns
-------
node
the pruned node
"""
# (1) subtree error
subtree_error = node.estimated()
# (2) leaf error
class_attribute = data.iloc[:, -1]
# class value = count
distribution = Counter()
leaf_total = 0
for value in metadata[data.columns[-1]]:
distribution[value] = weights[class_attribute == value].sum()
leaf_total += distribution[value]
correct_predictions = 0 if leaf_total == 0 else distribution[majority]
leaf_error = leaf_total - correct_predictions
leaf_error += estimate_error(leaf_total, leaf_error)
# (3) branch error
selected = node.children[0]
for i in range(1, len(node.children)):
if selected.total() < node.children[i].total():
selected = node.children[i]
# checks whether to prune the node or not
branch_error = float('inf')
if selected.is_leaf():
branch_error = leaf_error
else:
branch_error = selected.estimate_with_data(metadata, data, weights)
if leaf_error <= (subtree_error + 0.1) and leaf_error <= (branch_error + 0.1):
# replace by a leaf node
return Node(majority, node.parent, leaf_error, leaf_total, distribution)
elif branch_error <= (subtree_error + 0.1):
# replace by the most common branch
selected.estimate_with_data(metadata, data, weights, True)
selected.parent = node.parent
return selected