How can the Euclidean distance be calculated with NumPy?
Use numpy.linalg.norm
:
dist = numpy.linalg.norm(a-b)
This works because the Euclidean distance is the l2 norm, and the default value of the ord
parameter in numpy.linalg.norm
is 2.
For more theory, see Introduction to Data Mining:
python numpy euclidean distance calculation between matrices of row vectors
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)
Efficient numpy euclidean distance calculation for each element
I believe that this does what you want:
w, h = (512, 512)
cx, cy = (10, 376)
xcoords, ycoords = np.meshgrid(np.arange(w), np.arange(h))
target = np.sqrt((xcoords - cx)**2 + (ycoords - cy)**2)
Calculate Euclidean distance on numpy row-row cross product?
For your specific example you can do:
from scipy.spatial.distance import cdist
cdist(coords_b,coords_a)
In general, vectorizing depends on your function.
Calculate Euclidean distance between two python arrays
Here, you can just use np.linalg.norm
to compute the Euclidean distance. Your bug is due to np.subtract is expecting the two inputs are of the same length.
import numpy as np
list_a = np.array([[0,1], [2,2], [5,4], [3,6], [4,2]])
list_b = np.array([[0,1],[5,4]])
def run_euc(list_a,list_b):
return np.array([[ np.linalg.norm(i-j) for j in list_b] for i in list_a])
print(run_euc(list_a, list_b))
The code produces:
[[0. 5.83095189]
[2.23606798 3.60555128]
[5.83095189 0. ]
[5.83095189 2.82842712]
[4.12310563 2.23606798]]
Making an Euclidean Distance Matrix with Random Points in Python
If you want to use external modules, scipy
is very efficient for matrix calculations.
import random
import pandas as pd
from scipy.spatial import distance
npoints = int(input("Type the npoints:"))
width = float(input("Enter the Width you want:"))
height = float(input("Enter the Height you want:"))
sample = []
for _ in range(npoints):
sample.append((width * random.random(), height * random.random()))
print(*[f"({w:.2f}, {h:.2f})" for w, h in sample], sep=', ')
#Create a matrix from these points
mat_dist = distance.cdist(sample, sample, 'euclidean')
df_mat_dist = pd.DataFrame(mat_dist)
print(df_mat_dist)
Output
Type the npoints:4
Enter the Width you want:10
Enter the Height you want:10
(8.89, 8.85), (9.00, 9.43), (9.67, 9.45), (3.96, 5.68)
0 1 2 3
0 0.000000 0.584322 0.985072 5.856736
1 0.584322 0.000000 0.669935 6.277323
2 0.985072 0.669935 0.000000 6.839240
3 5.856736 6.277323 6.839240 0.000000
How to calculate the euclidean distance between two matrices using only matrix operations in numpy python (no for loops)?
Try numpy broadcasting
dist_mat = np.sum((a[:,None] - b)**2, axis=-1)**.5
Output:
array([[ 1.73205081, 10.77032961, 3.60555128, 10.81665383, 11.18033989,
8.77496439, 7.87400787, 7.87400787],
[ 9.21954446, 3.16227766, 9.64365076, 11.44552314, 7.54983444,
9.94987437, 3.46410162, 8.1240384 ],
[ 5.38516481, 8.60232527, 6.40312424, 5.38516481, 8.66025404,
6.70820393, 8.1240384 , 9.16515139],
[ 3.31662479, 11.83215957, 3. , 11. , 11.18033989,
8.06225775, 9.89949494, 7.34846923],
[ 1.73205081, 9.16515139, 1. , 10.04987562, 9. ,
6.70820393, 7.34846923, 5.09901951]])
How to broadcast this euclidean distance?
You can directly manipulate numpy
arrays in order to find euclidean distances here.
I am assuming either list1
or list2
contains 1
element and distances are to be calculated between each element of the other list and the single element. Rest is taken care of by numpy broadcasting.
import numpy as np
list1 =[(10.2,20.2),(5.3,9.2)]
list2 = [(2.2,3.3)]
a = np.array(list1)
b = np.array(list2)
dist = np.sqrt(((b - a)**2).sum(axis = 1))
Output: dist
array([18.69786084, 6.66483308])
where dist[0]
gives distance(list1[0], list2[0])
and dist[1]
gives distance(list1[1], list2[0])
.
It generalizes even when list1
has arbitrary number of points, the only constraint is the other list should have only one point.
Related Topics
What Is the Eafp Principle in Python
How to Write Json Data to a File
Python: Justifying Numpy Array
Getting a Map() to Return a List in Python 3.X
Changing One Character in a String
Pip Install' Fails For Every Package ("Could Not Find a Version That Satisfies the Requirement")
Pygame - How to Display Text With Font & Color
How to Properly Ignore Exceptions
"Large Data" Workflows Using Pandas
Most Efficient Way to Map Function Over Numpy Array
How to Safely Create a Nested Directory
Meaning of @Classmethod and @Staticmethod For Beginner
How to Step Through Python Code to Help Debug Issues