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

Class BaggedTreeClassifier

supervised_class2/util.py:60–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58
59
60class BaggedTreeClassifier:
61 def __init__(self, n_estimators, max_depth=None):
62 self.B = n_estimators
63 self.max_depth = max_depth
64
65 def fit(self, X, Y):
66 N = len(X)
67 self.models = []
68 for b in range(self.B):
69 idx = np.random.choice(N, size=N, replace=True)
70 Xb = X[idx]
71 Yb = Y[idx]
72
73 model = DecisionTreeClassifier(max_depth=self.max_depth)
74 model.fit(Xb, Yb)
75 self.models.append(model)
76
77 def predict(self, X):
78 # no need to keep a dictionary since we are doing binary classification
79 predictions = np.zeros(len(X))
80 for model in self.models:
81 predictions += model.predict(X)
82 return np.round(predictions / self.B)
83
84 def score(self, X, Y):
85 P = self.predict(X)
86 return np.mean(Y == P)

Callers 1

rf_vs_bag2.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected