Last active
May 10, 2016 05:54
-
-
Save fluffywaffles/9b36210560d0c410e255025a2087013b to your computer and use it in GitHub Desktop.
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
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