简体   繁体   English

Python 中的距离欧几里得

[英]distance euclidean in Python

I am trying to compute the distance between a list of coordinates and one coordinate named cord .我正在尝试计算坐标列表和一个名为cord的坐标之间的距离。

The expected result is a list with all distance between the i-th element of the list of coordinates and the cord.预期的结果是一个列表,其中包含坐标列表的第 i 个元素与绳索之间的所有距离。

Example:例子:

I have a DataFrame df which has a column Geo_Shape with a list of list.我有一个 DataFrame df ,它有一列Geo_Shape和一个列表。 And I need to compute a list with distance of all elements of this list with cord.我需要用绳子计算这个列表中所有元素的距离的列表。

Geo_Shape Geo_Shape Name姓名
[ [1.2,2.3], [0.3,1.7], [3.2,9.1] ] [ [[1.2,2.3], [0.3,1.7], [3.2,9.1] ] try尝试

cord = [1.2,5.3]绳子 = [1.2,5.3]

Code代码

I try do run this code我尝试运行此代码

df['Geo_Shape'].apply(lambda x: np.linalg.norm(x - [cord]*len(x), axis=1))

But i have this errors:但我有这个错误:

TypeError: unsupported operand type(s) for -: 'list' and 'list'

Someone know howto fix it?有人知道如何解决吗?

Thanks for your help!谢谢你的帮助!

You need to convert x of numpy.linalg.norm into numpy.ndarray :您需要将numpy.ndarrayx转换为numpy.linalg.norm

df['Geo_Shape'].apply(lambda x: np.linalg.norm(np.array(x) - cord, axis=1))

Output: Output:

0    [3.0, 3.7107950630558943, 4.294182110716777]
Name: Geo_Shape, dtype: object

Thanks for your help!谢谢你的帮助!

when i try:当我尝试:

df['Geo_Shape'].apply(lambda x: np.linalg.norm(np.array(x) - cord, axis=1))

i have this errors:我有这个错误:

TypeError: unsupported operand type(s) for -: 'list' and 'float'

so i try this:所以我试试这个:

df['Geo_Shape'].apply(lambda x: np.linalg.norm(np.array(x) - np.array([cord]*len(x)), axis=1))

And still the same error!仍然是同样的错误!

Do you knows what's wrong?你知道出了什么问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM