Build a decision tree from training set. Parameters ---------- X : array-like Feature dataset. target : dictionary or array-like Target values. max_features : int or None The number of features to consider when looking for
(
self,
X,
target,
max_features=None,
min_samples_split=10,
max_depth=None,
minimum_gain=0.01,
loss=None,
)
| 124 | self._calculate_leaf_value(target) |
| 125 | |
| 126 | def train( |
| 127 | self, |
| 128 | X, |
| 129 | target, |
| 130 | max_features=None, |
| 131 | min_samples_split=10, |
| 132 | max_depth=None, |
| 133 | minimum_gain=0.01, |
| 134 | loss=None, |
| 135 | ): |
| 136 | """Build a decision tree from training set. |
| 137 | |
| 138 | Parameters |
| 139 | ---------- |
| 140 | |
| 141 | X : array-like |
| 142 | Feature dataset. |
| 143 | target : dictionary or array-like |
| 144 | Target values. |
| 145 | max_features : int or None |
| 146 | The number of features to consider when looking for the best split. |
| 147 | min_samples_split : int |
| 148 | The minimum number of samples required to split an internal node. |
| 149 | max_depth : int |
| 150 | Maximum depth of the tree. |
| 151 | minimum_gain : float, default 0.01 |
| 152 | Minimum gain required for splitting. |
| 153 | loss : function, default None |
| 154 | Loss function for gradient boosting. |
| 155 | """ |
| 156 | |
| 157 | if not isinstance(target, dict): |
| 158 | target = {"y": target} |
| 159 | |
| 160 | # Loss for gradient boosting |
| 161 | if loss is not None: |
| 162 | self.loss = loss |
| 163 | |
| 164 | if not self.regression: |
| 165 | self.n_classes = len(np.unique(target["y"])) |
| 166 | |
| 167 | self._train( |
| 168 | X, |
| 169 | target, |
| 170 | max_features=max_features, |
| 171 | min_samples_split=min_samples_split, |
| 172 | max_depth=max_depth, |
| 173 | minimum_gain=minimum_gain, |
| 174 | ) |
| 175 | |
| 176 | def _calculate_leaf_value(self, targets): |
| 177 | """Find optimal value for leaf.""" |