MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / _train

Method _train

mla/ensemble/tree.py:70–124  ·  view source on GitHub ↗
(
        self,
        X,
        target,
        max_features=None,
        min_samples_split=10,
        max_depth=None,
        minimum_gain=0.01,
    )

Source from the content-addressed store, hash-verified

68 return max_col, max_val, max_gain
69
70 def _train(
71 self,
72 X,
73 target,
74 max_features=None,
75 min_samples_split=10,
76 max_depth=None,
77 minimum_gain=0.01,
78 ):
79 try:
80 # Exit from recursion using assert syntax
81 assert X.shape[0] > min_samples_split
82 assert max_depth > 0
83
84 if max_features is None:
85 max_features = X.shape[1]
86
87 column, value, gain = self._find_best_split(X, target, max_features)
88 assert gain is not None
89 if self.regression:
90 assert gain != 0
91 else:
92 assert gain > minimum_gain
93
94 self.column_index = column
95 self.threshold = value
96 self.impurity = gain
97
98 # Split dataset
99 left_X, right_X, left_target, right_target = split_dataset(
100 X, target, column, value
101 )
102
103 # Grow left and right child
104 self.left_child = Tree(self.regression, self.criterion, self.n_classes)
105 self.left_child._train(
106 left_X,
107 left_target,
108 max_features,
109 min_samples_split,
110 max_depth - 1,
111 minimum_gain,
112 )
113
114 self.right_child = Tree(self.regression, self.criterion, self.n_classes)
115 self.right_child._train(
116 right_X,
117 right_target,
118 max_features,
119 min_samples_split,
120 max_depth - 1,
121 minimum_gain,
122 )
123 except AssertionError:
124 self._calculate_leaf_value(target)
125
126 def train(
127 self,

Callers 1

trainMethod · 0.95

Calls 4

_find_best_splitMethod · 0.95
_calculate_leaf_valueMethod · 0.95
split_datasetFunction · 0.90
TreeClass · 0.85

Tested by

no test coverage detected