python numpy euclidean distance calculation between matrices of row vectors

后端 未结 5 1537
一生所求
一生所求 2020-12-28 14:58

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 ro

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 15:54

    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)
    

提交回复
热议问题