python numpy euclidean distance calculation between matrices of row vectors

后端 未结 5 1591
一生所求
一生所求 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:51

    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
    

提交回复
热议问题