问题描述
你好
在尝试编写处理一些numpy数组的函数时,
计算欧几里德距离,我最后得到了这段代码
(虽然我使用numpy,我相信我的问题更多与python有关
编码风格..所以我在这里张贴)
....
#我正在使用这些numpy.ndarrays进行计算
facespace #of shape(totalimgs,imgpixels)
weight #of shape( totalimgs,selectedfacespaces)
input_wk形状#(selectedfacespaces,)
距离#形状(selectedfacespaces,)最初全部0.0's
精神#of shape(selectedfacespaces),最初全部
0.0's
....
....
#here是计算部分
范围内的图像(numimgs):
distance = abs(input_wk - weights [image,:] )
if image == 0:
#copy from distance to mindistance
mindistance = distance.copy()
如果总和(精神抵抗)总和(距离):
imgindex = image
mindistance = distance.copy()
if max (精神抵抗)0.0:
#normalise mindistance
mindistance = mindistance /(max(mindistance)+1)
dist = sum(mindistance)
这让我获得了欧氏距离值。我想知道我是否用b $ b编码它的方式可以改进,使其更加紧凑....如果有人可以提供
建议这将是一个很大的帮助。
谢谢
D
hello
while trying to write a function that processes some numpy arrays and
calculate euclidean distance ,i ended up with this code
(though i used numpy ,i believe my problem has more to do with python
coding style..so am posting it here)
....
# i am using these numpy.ndarrays to do the calculation
facespace # of shape(totalimgs,imgpixels)
weights # of shape(totalimgs,selectedfacespaces)
input_wk # of shape(selectedfacespaces,)
distance # of shape(selectedfacespaces,) initally all 0.0 ''s
mindistance #of shape(selectedfacespaces,) initally all
0.0 ''s
....
....
#here is the calculations part
for image in range(numimgs):
distance = abs(input_wk - weights[image, :])
if image==0:
#copy from distance to mindistance
mindistance=distance.copy()
if sum(mindistance) sum(distance):
imgindex=image
mindistance=distance.copy()
if max(mindistance) 0.0:
#normalise mindistance
mindistance=mindistance/(max(mindistance)+1)
dist=sum(mindistance)
this gets me the euclidean distance value.I want to know if the way i
coded it can be improved,made more compact....if someone can give
suggestions it will be a great help .
thanks
D
推荐答案
看起来你正在计算从1-norm得到的距离
(坐标的总和;就像在一个城市一样)使用方块)。
我会保存总和(思维)值以避免在每次
迭代中重新计算它。
It looks like you''re rather computing a distance derived from the 1-norm
(sum of coordinates; like in a city with square blocks).
I''d save the sum(mindistance) value to avoid recomputing it in every
iteration.
代码现在非常清晰。无论如何,使用min()和
生成器:
_,imgindex = min((sum(abs(input_wk - weights [image,:]) ),image)for image
in xrange(numimgs))
mindistance = abs(input_wk - weights [imgindex,:])
#规范化并再次总结
-
Gabriel Genellina
The code is pretty legible as it is now. Anyway, using min() and a
generator:
_, imgindex = min((sum(abs(input_wk - weights[image, :])),image) for image
in xrange(numimgs))
mindistance = abs(input_wk - weights[imgindex, :])
# normalize and sum again
--
Gabriel Genellina
感谢Gabriel
D
thanks Gabriel
D
hi
这个计算出的距离真的是欧几里德距离吗?当我检查
维基百科
它显示的计算涉及
elements之差的平方和。在此代码中,使用坐标总和?这是一个不同的措施吗?
oharry
hi
is this calculated distance really Euclidean distance? When i checked
wikipedia http://en.wikipedia.org/wiki/Euclidean_distance
it shows a calculation involving sum of squares of the differences of
elements.Here in this code ,the sum of coordinates are used? is that a
different measure?
oharry
这篇关于找到欧几里德距离,更好的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!