I am working on Manhattan distance. It works well with the simple for loop. But I am trying to avoid this for loop.
import numpy as np
import random
A = np.random.randint(5, size=(10, 5))
B = [1, 3, 5, 2, 4]
for i in range(10):
dist = sum(abs(A[i]-B))
print("Distances: ", dist)
Is there any optimal way than this? such as using advanced indexing.. Thank you for the guidance.
numpy
You can do this within numpy:
>>> np.sum(np.abs(A-B), axis=1)
array([10, 6, 9, 9, 7, 7, 9, 8, 14, 8])
Compare this to the output from your loop:
Distances: 10
Distances: 6
Distances: 9
Distances: 9
Distances: 7
Distances: 7
Distances: 9
Distances: 8
Distances: 14
Distances: 8
scipy
You could also use scipy
if you wanted (personally I prefer the numpy
method though):
from scipy.spatial.distance import cdist
>>> cdist(A,np.array(B).reshape(1,-1), metric='cityblock')
array([[10.],
[ 6.],
[ 9.],
[ 9.],
[ 7.],
[ 7.],
[ 9.],
[ 8.],
[14.],
[ 8.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With