Find best feature and value for a split. Greedy algorithm.
(self, X, target, n_features)
| 43 | return list(split_values) |
| 44 | |
| 45 | def _find_best_split(self, X, target, n_features): |
| 46 | """Find best feature and value for a split. Greedy algorithm.""" |
| 47 | |
| 48 | # Sample random subset of features |
| 49 | subset = random.sample(list(range(0, X.shape[1])), n_features) |
| 50 | max_gain, max_col, max_val = None, None, None |
| 51 | |
| 52 | for column in subset: |
| 53 | split_values = self._find_splits(X[:, column]) |
| 54 | for value in split_values: |
| 55 | if self.loss is None: |
| 56 | # Random forest |
| 57 | splits = split(X[:, column], target["y"], value) |
| 58 | gain = self.criterion(target["y"], splits) |
| 59 | else: |
| 60 | # Gradient boosting |
| 61 | left, right = split_dataset( |
| 62 | X, target, column, value, return_X=False |
| 63 | ) |
| 64 | gain = xgb_criterion(target, left, right, self.loss) |
| 65 | |
| 66 | if (max_gain is None) or (gain > max_gain): |
| 67 | max_col, max_val, max_gain = column, value, gain |
| 68 | return max_col, max_val, max_gain |
| 69 | |
| 70 | def _train( |
| 71 | self, |
no test coverage detected