Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manhattan Distance between (P, Q) and (R,)

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.

like image 908
Mass17 Avatar asked Jan 28 '23 05:01

Mass17


1 Answers

Pure 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

Alternative: 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.]])
like image 70
sacuL Avatar answered Jan 29 '23 20:01

sacuL