I want to write a function to calculate the Euclidean distance between coordinates in list_a
to each of the coordinates in list_b
, and produce an array of distances of dimension a
rows by b
columns (where a
is the number of coordinates in list_a
and b
is the number of coordinates in list_b
.
NB: I do not want to use any libraries other than numpy, for simplicity.
list_a = np.array([[0,1], [2,2], [5,4], [3,6], [4,2]])
list_b = np.array([[0,1],[5,4]])
Running the function would generate:
>>> np.array([[0., 5.830951894845301],
[2.236, 3.605551275463989],
[5.830951894845301, 0.],
[5.830951894845301, 2.8284271247461903],
[4.123105625617661, 2.23606797749979]])
I have been trying to run the below
def run_euc(list_a,list_b):
euc_1 = [np.subtract(list_a, list_b)]
euc_2 = sum(sum([i**2 for i in euc_1]))
return np.sqrt(euc_2)
But I am getting the following error:
ValueError: operands could not be broadcast together with shapes (5,2) (2,2)
Thank you.