| 59 | |
| 60 | |
| 61 | class RandomForestClassifier(RandomForest): |
| 62 | def __init__( |
| 63 | self, |
| 64 | n_estimators=10, |
| 65 | max_features=None, |
| 66 | min_samples_split=10, |
| 67 | max_depth=None, |
| 68 | criterion="entropy", |
| 69 | ): |
| 70 | super(RandomForestClassifier, self).__init__( |
| 71 | n_estimators=n_estimators, |
| 72 | max_features=max_features, |
| 73 | min_samples_split=min_samples_split, |
| 74 | max_depth=max_depth, |
| 75 | criterion=criterion, |
| 76 | ) |
| 77 | |
| 78 | if criterion == "entropy": |
| 79 | self.criterion = information_gain |
| 80 | else: |
| 81 | raise ValueError() |
| 82 | |
| 83 | # Initialize empty trees |
| 84 | for _ in range(self.n_estimators): |
| 85 | self.trees.append(Tree(criterion=self.criterion)) |
| 86 | |
| 87 | def _predict(self, X=None): |
| 88 | y_shape = np.unique(self.y).shape[0] |
| 89 | predictions = np.zeros((X.shape[0], y_shape)) |
| 90 | |
| 91 | for i in range(X.shape[0]): |
| 92 | row_pred = np.zeros(y_shape) |
| 93 | for tree in self.trees: |
| 94 | row_pred += tree.predict_row(X[i, :]) |
| 95 | |
| 96 | row_pred /= self.n_estimators |
| 97 | predictions[i, :] = row_pred |
| 98 | return predictions |
| 99 | |
| 100 | |
| 101 | class RandomForestRegressor(RandomForest): |
no outgoing calls