Last active
November 8, 2022 15:20
-
-
Save jgoodie/4bf8a991ad35ab0f1fb6a435d5a0148d to your computer and use it in GitHub Desktop.
simple tree using dictionaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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