Guest User

Untitled

a guest
Feb 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. def nearest_neighbours(X, Y, norm=2.0):
  2. """Index the rows of `Y` in increasing order of distance from each row in `X`.
  3.  
  4. Parameters
  5. ----------
  6. X : array, 2 dimensional, shape = (n_samples, n_dim)
  7. The matrix, for the rows of which to find the closest row in `Y`.
  8.  
  9. Y : array, 2 dimensional, shape = (n_reference, n_dim)
  10. The matrix, among the rows of which to seek the closest one for `X`.
  11.  
  12. norm : float, positive
  13. The order of the l^p norm to use for computing the pairwise distances.
  14.  
  15. Returns
  16. -------
  17. index : 2 dimensional
  18. The matrix of indices of the rows of `Y` for each vector in `X`
  19. in increasing order of distnace with respect to the l^p norm.
  20.  
  21. """
  22. assert X.ndim == Y.ndim and X.ndim == 2
  23.  
  24. ### BEGIN Solution
  25. indixesY = list(range(0, Y.shape[0]))
  26. def myfunction(x):
  27. return list(map(lambda y_i: np.linalg.norm(x - Y[y_i,:], ord=norm), indixesY))
  28.  
  29. out = np.argsort(np.apply_along_axis(myfunction, axis=1, arr=X))
  30.  
  31.  
  32. ### END Solution
  33.  
  34. return out
Add Comment
Please, Sign In to add comment