python numpy euclidean distance calculation between matrices of row vectors

血红的双手。 提交于 2019-12-03 11:13:01

问题


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


回答1:


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)



回答2:


To get the distance you can use the norm method of the linalg module in numpy:

np.linalg.norm(x - y)



回答3:


import numpy as np
def distance(v1, v2):
    return np.sqrt(np.sum((v1 - v2) ** 2))    



回答4:


To apply a function to each element of a numpy array, try numpy.vectorize.

To do the actual calculation, we need the square root of the sum of squares of differences (whew!) between pairs of coordinates in the two vectors.

We can use zip to pair the coordinates, and sum with a comprehension to sum up the results. That looks like:

sum((x - y) ** 2 for (x, y) in zip(singlePoint, pointFromArray)) ** 0.5



回答5:


import numpy as np
single_point = [3, 4]
points = np.arange(20).reshape((10,2))   
distance = euclid_dist(single_point,points)

def euclid_dist(t1, t2):
    return np.sqrt(((t1-t2)**2).sum(axis = 1))


来源:https://stackoverflow.com/questions/4370975/python-numpy-euclidean-distance-calculation-between-matrices-of-row-vectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!