Skip to content

Instantly share code, notes, and snippets.

@pearcemc
Created October 22, 2010 18:16
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/641082 to your computer and use it in GitHub Desktop.
Save pearcemc/641082 to your computer and use it in GitHub Desktop.
class KNearest:
"""k-nearest neighbour inferer"""
def __init__(self, ds):
#set the dataset
self.ds = ds
def predict(self, p1, k=1):
"""Given a test point p1, return the modal class of its knearest neighbours"""
distances = []
#calculate the distance between the test point and known data points.
for i, clas in enumerate( self.ds.classes ):
for p2 in clas.data:
dist = self._calc_distance(p1, p2)
distances.append( ( dist, i, p2 ) )
#rank the distances
distances = sorted( distances )
#the following is a bit scruffy, I should really be using mean
return int( stats.mode( [dist[1] for dist in distances[:k]] )[0] )
def _calc_distance(self, p1, p2):
""" Calculate the Euclidean distance between the two points """
return ( sum( [(p1[i] - p2[i])**2 for i in range( len(p1) )] ) )**0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment