Created
October 22, 2010 18:16
-
-
Save pearcemc/641082 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 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