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

Class RandomForest

mla/ensemble/random_forest.py:9–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9class RandomForest(BaseEstimator):
10 def __init__(
11 self,
12 n_estimators=10,
13 max_features=None,
14 min_samples_split=10,
15 max_depth=None,
16 criterion=None,
17 ):
18 """Base class for RandomForest.
19
20 Parameters
21 ----------
22 n_estimators : int
23 The number of decision tree.
24 max_features : int
25 The number of features to consider when looking for the best split.
26 min_samples_split : int
27 The minimum number of samples required to split an internal node.
28 max_depth : int
29 Maximum depth of the tree.
30 criterion : str
31 The function to measure the quality of a split.
32 """
33 self.max_depth = max_depth
34 self.min_samples_split = min_samples_split
35 self.max_features = max_features
36 self.n_estimators = n_estimators
37 self.trees = []
38
39 def fit(self, X, y):
40 self._setup_input(X, y)
41 if self.max_features is None:
42 self.max_features = int(np.sqrt(X.shape[1]))
43 else:
44 assert X.shape[1] > self.max_features
45 self._train()
46
47 def _train(self):
48 for tree in self.trees:
49 tree.train(
50 self.X,
51 self.y,
52 max_features=self.max_features,
53 min_samples_split=self.min_samples_split,
54 max_depth=self.max_depth,
55 )
56
57 def _predict(self, X=None):
58 raise NotImplementedError()
59
60
61class RandomForestClassifier(RandomForest):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected