Machine-Learning-From-Scratch icon indicating copy to clipboard operation
Machine-Learning-From-Scratch copied to clipboard

DecisionTree code - suggestion

Open sabn0 opened this issue 1 year ago • 0 comments

Hi, In the DecisionTree implementation, line 33 there is a call to the _most_common_label() method. Line 33 is reached only if the y numpy array has one unique value, so instead of calling most common you can simply take the value from any index of the array.

Maybe something like:

# check the stopping criteria
if (depth>=self.max_depth or n_labels==1 or n_samples<self.min_samples_split):
    return Node(value=y[0])

instead of :

# check the stopping criteria
if (depth>=self.max_depth or n_labels==1 or n_samples<self.min_samples_split):
    leaf_value = self._most_common_label(y)
    return Node(value=leaf_value)

sabn0 avatar Aug 14 '23 15:08 sabn0