How can I calculate the element-wise euclidean distance between 2 numpy arrays? For example; I have 2 arrays both of dimensions 3x3 (known as array A and array B) and I want to calculate the euclidean distance between value A[0,0] and B[0,0]. Then I want to calculate the euclidean distance between value A[0,1] and B[0,1]. And so on. So the output array would be 3x3 aswell.
If I try to use scipy.spatial.distancecdist
I get the error ValueError: XA must be a 2-dimensional array.
import numpy as np
from scipy.spatial.distance import cdist
a = np.array([
[(0,255,0),(255,255,0),(0,255,0)],
[(0,255,0),(255,255,0),(0,255,0)],
[(0,255,0),(255,255,0),(0,255,0)],
])
b = np.array([
[(255,255,0),(255,255,0),(0,255,0)],
[(255,255,0),(255,255,0),(0,255,0)],
[(255,255,0),(255,255,0),(0,255,0)],
])
dists = cdist(a, b, 'euclidean')
print(dists)
- I would really like to use a scipy function because I can easily use a different distance measure with their functions. For example;
cdist(a,b,'cityblock')
,cdist(a,b,'sqeuclidean')
, etc.
Edit My desired output is like so (the maths has been made up but the array dimensions are correct 3x3):
[[100, 0, 100]
[100, 0, 100]
[100, 0, 100]]
Ie, I am expecting:
[[cdist((0,255,0), (255,255,0)), cdist((0,255,0), (255,255,0)), cdist((0,255,0), (255,255,0)),
[...]
[...]]