Nearest neighbours of points

Tom P werotizy at freent.dd
Sat Oct 31 17:28:08 EDT 2015


On 10/24/2015 10:05 PM, Poul Riis wrote:
> I have N points in 3D, organized in a list. I want to to point out the numbers of the two that have the smallest distance.
> With scipy.spatial.distance.pdist I can make a list of all the distances, and I can point out the number of the minimum value of that list (see simple example below - the line with pts.append... should be indented three times). But I guess there is a standard (numpy?) routine which points out the numbers of the corresponding two points but I cannot find it. Can someone help?
>
> Poul Riis
>
>
>
>
> import numpy as np
> import scipy
> from scipy.spatial.distance import pdist
>
>
> pts=[]
> for i in range(-1,2):
>      for j in range(-1,2):
>          for k in range(-1,2):                 pts.append((i+np.random.random()/10,j+np.random.random()/10,k+np.random.random()/10))
> for i in range(0,len(pts)):
>      print(pts[i])
> distances=scipy.spatial.distance.pdist(pts)
> n=np.argmin(distances)
> for i in range(0,len(distances)):
>      print(i,distances[i])
> print('The minimum distance is: ',min(distances),' which has number ',n)
>

I won't claim to have the definitive answer but - is this a clustering 
problem? Did you look at any machine learning packages?




More information about the Python-list mailing list