Guest User

Untitled

a guest
Jun 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. """
  2. Quick and dirty experimental implementation of k-nearest neighbours technique
  3. """
  4.  
  5. from scipy import randn
  6. import scipy.stats as stats
  7. import random
  8.  
  9. from kdata import *
  10.  
  11. class KNearest:
  12. """k-nearest neighbour inferer"""
  13. def __init__(self, ds):
  14. #set the dataset
  15. self.ds = ds
  16.  
  17. def predict(self, p1, k=1):
  18. """Given a test point p1, return the modal class of its knearest neighbours"""
  19. distances = []
  20. #calculate the distance between the test point and known data points.
  21. for i, clas in enumerate( self.ds.classes ):
  22. for p2 in clas.data:
  23. dist = self._calc_distance(p1, p2)
  24. distances.append( ( dist, i, p2 ) )
  25. #rank the distances
  26. distances = sorted( distances )
  27. #the following is a bit scruffy, I should really be using mean
  28. return int( stats.mode( [dist[1] for dist in distances[:k]] )[0] )
  29.  
  30. def _calc_distance(self, p1, p2):
  31. """ Calculate the Euclidean distance between the two points """
  32. return ( sum( [(p1[i] - p2[i])**2 for i in range( len(p1) )] ) )**0.5
  33.  
  34.  
  35. class TestPredictor:
  36. """Iterate a KNearest predictor and return its success rate."""
  37.  
  38. def __init__(self, predictor, times=1000):
  39. #set the KNearest predictor
  40. self.predictor = predictor
  41. #set number of iterations
  42. self.times = times
  43.  
  44. def test(self, k=1):
  45. results = []
  46. #iteration
  47. for i in range(self.times):
  48. #pick a random class
  49. ac = random.randint(0, len(self.predictor.ds.classes)-1)
  50. #use it to generate a point
  51. tp = self.predictor.ds.classes[ac].generate()
  52. #test the predictor on it
  53. pc = self.predictor.predict(tp, k=k)
  54. #if it got it right it gets a cookie
  55. if pc == ac:
  56. results.append(1)
  57. else:
  58. results.append(0)
  59. #return the mean result
  60. return float(sum(results))/float(len(results))
  61.  
  62.  
  63. if __name__=="__main__":
  64.  
  65. c = PDimClass([(3,2), (7,1), (2,1)])
  66. d = PDimClass([(2,1), (3,1), (4,1)])
  67. e = PDimClass([(0,1), (0,1), (0,2)])
  68.  
  69. ds = MultiClassDS([c, d, e], length=120)
  70.  
  71. k = KNearest(ds)
  72. t = TestPredictor(k, times=100)
  73. print t.test(k=3)
  74.  
  75. vis3d(ds)
Add Comment
Please, Sign In to add comment