| 29 | |
| 30 | |
| 31 | class BaggedTreeRegressor: |
| 32 | def __init__(self, n_estimators, max_depth=None): |
| 33 | self.B = n_estimators |
| 34 | self.max_depth = max_depth |
| 35 | |
| 36 | def fit(self, X, Y): |
| 37 | N = len(X) |
| 38 | self.models = [] |
| 39 | for b in range(self.B): |
| 40 | idx = np.random.choice(N, size=N, replace=True) |
| 41 | Xb = X[idx] |
| 42 | Yb = Y[idx] |
| 43 | |
| 44 | model = DecisionTreeRegressor(max_depth=self.max_depth) |
| 45 | model.fit(Xb, Yb) |
| 46 | self.models.append(model) |
| 47 | |
| 48 | def predict(self, X): |
| 49 | predictions = np.zeros(len(X)) |
| 50 | for model in self.models: |
| 51 | predictions += model.predict(X) |
| 52 | return predictions / self.B |
| 53 | |
| 54 | def score(self, X, Y): |
| 55 | d1 = Y - self.predict(X) |
| 56 | d2 = Y - Y.mean() |
| 57 | return 1 - d1.dot(d1) / d2.dot(d2) |
| 58 | |
| 59 | |
| 60 | class BaggedTreeClassifier: |