找到欧几里德距离,更好的代码? [英] finding euclidean distance,better code?

查看:63
本文介绍了找到欧几里德距离,更好的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

在尝试编写处理一些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

推荐答案

En Mon,2008年3月17日03:04:16 -0200, de **** @ gmail.com < de **** @ gmail.com>

escribi ???:
En Mon, 17 Mar 2008 03:04:16 -0200, de****@gmail.com <de****@gmail.com>
escribi???:

在尝试编写函数时处理一些numpy数组和

计算欧几里德距离,我最后得到这个代码

(虽然我使用numpy,我相信我的问题更多地与python

编码风格.so我发布在这里)


范围内的图像(numimgs):

距离= abs(input_wk - 权重[图片,:])

if image == 0:

#copy from distance to mindistance

mindistance = distance.copy()

if sum(总和(距离):

imgindex = image

mindistance = distance.copy()

如果max(mindistance)0.0:

#normalise mindistance

mindistance = mindistance /(max(mindistance)+1)

dist = sum(mindistance)

>
这让我得到了欧几里德距离值。
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)

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.



看起来你正在计算从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.


我想知道如果我用b
编码它的方式可以改进,做得更紧凑....如果有人可以给

建议它会是一个很好的帮助。
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 .



代码现在非常清晰。无论如何,使用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


3月17日,6:17下午,Gabriel Genellina < gagsl -... @ yahoo.com.arwrote
On Mar 17, 6:17 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote

>

_,imgindex = min((sum(abs) input_wk - 权重[图片,:])),图片)图片

in xrange(numimgs))

mindistance = abs(input_wk - weights [imgindex,:])

#normalize并再次加总
>
_, 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

D


thanks Gabriel
D


代码现在非常清晰。无论如何,使用min()和
The code is pretty legible as it is now. Anyway, using min() and a

generator:

generator:



hi

这个计算出的距离真的是欧几里德距离吗?当我检查

维基百科 http://en.wikipedia。 org / wiki / Euclidean_distance

它显示的计算涉及

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


这篇关于找到欧几里德距离,更好的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆