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

Class NotAsRandomForest

supervised_class2/rf_vs_bag2.py:36–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34# Xtrain, Ytrain, Xtest, Ytest = get_data()
35
36class NotAsRandomForest:
37 def __init__(self, n_estimators):
38 self.B = n_estimators
39
40 def fit(self, X, Y, M=None):
41 N, D = X.shape
42 if M is None:
43 M = int(np.sqrt(D))
44
45 self.models = []
46 self.features = []
47 for b in range(self.B):
48 tree = DecisionTreeClassifier()
49
50 # sample features
51 features = np.random.choice(D, size=M, replace=False)
52
53 # sample training samples
54 idx = np.random.choice(N, size=N, replace=True)
55 Xb = X[idx]
56 Yb = Y[idx]
57
58 tree.fit(Xb[:, features], Yb)
59 self.features.append(features)
60 self.models.append(tree)
61
62 def predict(self, X):
63 N = len(X)
64 P = np.zeros(N)
65 for features, tree in zip(self.features, self.models):
66 P += tree.predict(X[:, features])
67 return np.round(P / self.B)
68
69 def score(self, X, Y):
70 P = self.predict(X)
71 return np.mean(P == Y)
72
73
74T = 500

Callers 1

rf_vs_bag2.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected