Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created June 14, 2018 06:00
Show Gist options
  • Select an option

  • Save monkey-codes/cc4dfeab7650d6cfd23417f49b488577 to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/cc4dfeab7650d6cfd23417f49b488577 to your computer and use it in GitHub Desktop.
Simple implementation of finding the best split when building a tree for a random forest
def find_better_split(self, var_idx):
x,y = self.x.values[self.idxs,var_idx], self.y[self.idxs]
for i in range(1,self.n-1):
lhs = x<=x[i]
rhs = x>x[i]
if rhs.sum()==0: continue
lhs_std = y[lhs].std()
rhs_std = y[rhs].std()
curr_score = lhs_std*lhs.sum() + rhs_std*rhs.sum()
if curr_score<self.score:
self.var_idx,self.score,self.split = var_idx,curr_score,x[i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment