Created
October 22, 2010 18:14
-
-
Save pearcemc/641079 to your computer and use it in GitHub Desktop.
This file contains 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 TestPredictor: | |
"""Iterate a KNearest predictor and return its success rate.""" | |
def __init__(self, predictor, times=1000): | |
#set the KNearest predictor | |
self.predictor = predictor | |
#set number of iterations | |
self.times = times | |
def test(self, k=1): | |
results = [] | |
#iteration | |
for i in range(self.times): | |
#pick a random class | |
ac = random.randint(0, len(self.predictor.ds.classes)-1) | |
#use it to generate a point | |
tp = self.predictor.ds.classes[ac].generate() | |
#test the predictor on it | |
pc = self.predictor.predict(tp, k=k) | |
#if it got it right it gets a cookie | |
if pc == ac: | |
results.append(1) | |
else: | |
results.append(0) | |
#return the mean result | |
return float(sum(results))/float(len(results)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment