Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. from scipy.spatial import distance
  2. def euc(a,b):
  3.  
  4. return distance.euclidean(a, b)
  5.  
  6. class ScrappyKNN():
  7.  
  8. def fit(self, x_train, y_train):
  9.  
  10. self.x_train = x_train
  11.  
  12. self.y_train = y_train
  13.  
  14. predictions = []
  15.  
  16. for row in x_test:
  17.  
  18. label = self.closest(row)
  19.  
  20. predictions.append(label)
  21.  
  22. return predictions
  23.  
  24. best_dist = euc(row, self.x_train[0])
  25.  
  26. best_index = 0
  27.  
  28. for i in range(1, len(self.x_train)):
  29.  
  30. dist = euc(row, self.x_train[i])
  31.  
  32. if dist < best_dist:
  33.  
  34. best_dist = dist
  35.  
  36. best_index = i
  37.  
  38. return self.y_train[best_index]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement