0

I have the following function to compute distance between two points that are represented by matrices. i.e. points is a list of matrices, and other_points is an array of matrices.

def find_dist(points: list, other_points: np.array,) -> int:
    min_dists = [np.min(np.linalg.norm(other_points - i, axis=1), axis=0) for i in points] 

Is there a better way to achieve the above to optimize performance? I tried to use np.ufunc.outer as following but the logic gets messed up. I am not sure how to use np.ufunc.outer to achieve the above:

   min_dists = [np.min(np.subtract.outer(other_points, i), axis=0) for i in points]

Thanks!

  • since you need to compute minimal distance you do not need to compute all distances, you can calculate dot product, find smallest and calculate its square root. Side note: why you do not represent all `points` as `np.array`? – dankal444 Mar 31 '23 at 15:33
  • I forgot to mention that the data points are from the MNIST dataset, for example, so I wouldn't be able to find the dot product between two matrices. Am I missing something? thanks – vm finance Mar 31 '23 at 18:15
  • Does this answer your question? [Finding index of nearest point in numpy arrays of x and y coordinates](https://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-of-x-and-y-coordinates) – Armali Apr 03 '23 at 05:38

0 Answers0