Reputation: 451
I'm trying to generate specific array by calculating Euclid distance, I'm getting a different result
import numpy
def find_eucledian_distances(a_points, b_points):
return numpy.sqrt(numpy.sum((a_points-b_points)**2))
a = np.array([[3.0, 4.0],
[-3.0, -6.0],
[-2.5, 6.3]])
b = np.array([[0.0, 0.0],
[2.0, 6.0],
[4.5, -8.3]])
d = find_eucledian_distances(a, b)
print(d)
print(d.shape)
These are two expected results expected result: [ 5. 13. 16.19135572] expected result: (3,)
but I'm getting 21.35790251873999 as a result. Can anyone explain?
Upvotes: 2
Views: 77
Reputation: 3619
There is a numpy inbuilt way for this with the numpy's linear algebra library linalg
:
return numpy.linalg.norm(a_points-b_points, axis=-1)
Upvotes: 1
Reputation: 2585
you should return bellows:
return numpy.sqrt(numpy.sum((a_points-b_points)**2, axis=-1))
you should np.sum
along the last axis. if you don't specific the axis=-1
, the np.sum()
will sum all of the elements.
Upvotes: 1