Skip to content

Instantly share code, notes, and snippets.

@pearcemc
Created October 22, 2010 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pearcemc/641079 to your computer and use it in GitHub Desktop.
Save pearcemc/641079 to your computer and use it in GitHub Desktop.
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