Skip to content

Instantly share code, notes, and snippets.

@jgoodie
Last active November 8, 2022 15:20
Show Gist options
  • Save jgoodie/4bf8a991ad35ab0f1fb6a435d5a0148d to your computer and use it in GitHub Desktop.
Save jgoodie/4bf8a991ad35ab0f1fb6a435d5a0148d to your computer and use it in GitHub Desktop.
simple tree using dictionaries
class DecisionTree(object):
def __init__(self, max_depth=2):
self.max_depth = max_depth
self.tree = {}
self.sum = 0
self.data = []
def __str__(self):
return f"data: {self.data}"
def learn(self, data=np.random.rand(), depth=0):
self.tree = self._learn(self.tree, data=np.random.rand(), depth=0)
def _learn(self, tree, data=np.random.rand(), depth=0):
if depth <= self.max_depth:
depth+=1
tree['data'] = data
tree['left'] = self._learn(DecisionTree(self.max_depth).tree, data=np.random.randint(0,10), depth=depth)
tree['right'] = self._learn(DecisionTree(self.max_depth).tree, data=np.random.randint(0,50), depth=depth)
return tree
t = DecisionTree(max_depth=5)
t.learn()
pp.pprint(t.tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment