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

Method _find_best_split

mla/ensemble/tree.py:45–68  ·  view source on GitHub ↗

Find best feature and value for a split. Greedy algorithm.

(self, X, target, n_features)

Source from the content-addressed store, hash-verified

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,

Callers 1

_trainMethod · 0.95

Calls 4

_find_splitsMethod · 0.95
splitFunction · 0.90
split_datasetFunction · 0.90
xgb_criterionFunction · 0.90

Tested by

no test coverage detected