Mel*_*aye 8 python algorithm performance numpy distance
我在未知的维空间中有一系列点,例如:
data=numpy.array(
[[ 115, 241, 314],
[ 153, 413, 144],
[ 535, 2986, 41445]])
Run Code Online (Sandbox Code Playgroud)
我想找到所有点之间的平均欧氏距离.
请注意,我有超过20,000点,所以我想尽可能高效地完成这项工作.
谢谢.
小智 11
如果您可以访问scipy,则可以尝试以下操作:
scipy.spatial.distance.cdist(data,data)
好吧,我不认为有一种超快速的方法可以做到这一点,但这应该做到:
tot = 0.
for i in xrange(data.shape[0]-1):
tot += ((((data[i+1:]-data[i])**2).sum(1))**.5).sum()
avg = tot/((data.shape[0]-1)*(data.shape[0])/2.)
Run Code Online (Sandbox Code Playgroud)