MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / BaggedTreeRegressor

Class BaggedTreeRegressor

supervised_class2/util.py:31–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29
30
31class 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
60class BaggedTreeClassifier:

Callers 1

rf_vs_bag.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected