python计算坐标点欧式距离_python numpy行向量矩阵之间的欧式距离计算

I am new to Numpy and I would like to ask you how to calculate euclidean distance between points stored in a vector.

Let's assume that we have a numpy.array each row is a vector and a single numpy.array. I would like to know if it is possible to calculate the euclidean distance between all the points and this single point and store them in one numpy.array.

Here is an interface:

points #2d list of row-vectors

singlePoint #one row-vector

listOfDistances= procedure( points,singlePoint)

Can we have something like this?

Or is it possible to have one command to have the single point as a list of other points and at the end we get a matrix of distances?

Thanks

解决方案

While you can use vectorize, @Karl's approach will be rather slow with numpy arrays.

The easier approach is to just do np.hypot(*(points - single_point).T). (The transpose assumes that points is a Nx2 array, rather than a 2xN. If it's 2xN, you don't need the .T.

However this is a bit unreadable, so you write it out more explictly like this (using some canned example data...):

import numpy as np

single_point = [3, 4]

points = np.arange(20).reshape((10,2))

dist = (points - single_point)**2

dist = np.sum(dist, axis=1)

dist = np.sqrt(dist)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值