Skip to content

Instantly share code, notes, and snippets.

@fluffywaffles
Last active May 10, 2016 05:54
Show Gist options
  • Save fluffywaffles/9b36210560d0c410e255025a2087013b to your computer and use it in GitHub Desktop.
Save fluffywaffles/9b36210560d0c410e255025a2087013b to your computer and use it in GitHub Desktop.
def classify(self, instance):
'''
given a single observation, will return the output of the tree
'''
attr = self.decision_attribute
if self.label is not None:
return self.label
elif self.splitting_value:
if instance[attr] < self.splitting_value:
return self.children[0].classify(instance)
else:
return self.children[1].classify(instance)
else:
key = instance[attr]
try:
child = self.children[str(key) if key is None else key]
except KeyError: # Oh no! something in the validation set we've
# never seen before; probably (hopefully) a None
key = random.choice(self.children.keys())
child = self.children[key]
return child.classify(instance)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment