Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Tom P Newsgroups: comp.lang.python Subject: Re: Nearest neighbours of points Date: Sat, 31 Oct 2015 22:28:08 +0100 Lines: 30 Message-ID: References: <81947104-f6dd-48b3-ba7b-f03b0c5b6f39@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net mVVGVLyaS/xBKf30aKKpEwycUCGqxufsKb1PHcQiHvwEUmaYc= Cancel-Lock: sha1:BkxrZirWdYgN4WrCygyCfgKmbik= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <81947104-f6dd-48b3-ba7b-f03b0c5b6f39@googlegroups.com> Xref: csiph.com comp.lang.python:98024 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?