Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
euclidean distance
#1
Dear all,
I have two 2D arrays (size nxm) and I want to calculate the Euclidean distance between them. Meaning that the distance will be a single number whereas my arrays are nxm.
Is there a quick way to do it besides going into double loop?
Thank you!
Jenya
Reply
#2
You can use SciPy library, look at cdist function.
Reply
#3
Does cdist return a single number? Because I thought it would return a matrix? Thank you!
Reply
#4
Yes, it returns a matrix. Look at the following minimal example:

from scipy.spatial.distance import cdist
import numpy as np
np.random.seed(10)
X = np.random.rand(3, 10)
Y = np.random.rand(4, 10)
cdist(X, Y)
Output:
array([[1.00025041, 1.11616128, 1.49867252, 1.33747696], [1.76040012, 1.26928995, 1.4958863 , 1.31219674], [1.21858991, 1.08164983, 1.0274891 , 1.11686588]])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  broadcasting euclidean distance error glennford49 1 3,988 Nov-19-2020, 07:01 PM
Last Post: Gribouillis
  Visualize Geo Map/Calculate distance zarize 1 1,891 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  organizing by distance gonzo620 7 3,895 Oct-16-2018, 01:41 AM
Last Post: stullis
  Python Ble Distance Problem aleynasarcanli 10 12,495 Feb-09-2018, 10:48 PM
Last Post: aleynasarcanli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020