Guest User

Untitled

a guest
Oct 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class RandomForest():
  2. def __init__(self, x, y, n_trees, sample_sz, min_leaf=5, depth = 10):
  3. np.random.seed(42)
  4. self.x, self.y, self.sample_sz, self.min_leaf, self.depth = x, y, sample_sz, min_leaf, depth
  5. self.trees = [self.create_tree() for i in range(n_trees)]
  6.  
  7. def create_tree(self):
  8. rnd_idxs = np.random.permutation(len(self.y))[:self.sample_sz] #bagging
  9. return DecisionTree(self.x.iloc[rnd_idxs], self.y[rnd_idxs], min_leaf=self.min_leaf, depth = 10)
  10.  
  11. def predict(self, x):
  12. return np.mean([t.predict(x) for t in self.trees], axis=0)
Add Comment
Please, Sign In to add comment