For 1D vector/array it's easier. For example:
array1 = [1, 2, 3]
array2 = [1, 1, 1]
manhattan distance will be: (0+1+2) which is 3
import numpy as np
def cityblock_distance(A, B):
result = np.sum([abs(a - b) for (a, b) in zip(A, B)])
return result
The output for 2 points will be: 3 But what about a 2D array/vector. For example, what will be the manhattan(or L1 or cityblock) for two 2D vector like these (below):
arr1 = [[29, 30, 36, 30, 18],[37, 37, 49, 54, 23]]
arr2 = [[31, 33, 37, 34, 22],[37, 38, 50, 58, 26]]
if I use the code I mentioned above, it is giving 3 as output for 1 D vector. For the 2D vector the output it's showing as 2281. In my sense the logical manhattan distance should be like this :
difference of the first item between two arrays: 2,3,1,4,4 which sums to 14
difference of the second item between two array:0,1,1,4,3 which is 9.
The total sum will be 23 as so manhattan distance between those two 2D array will be 23.
Is my calculation going wrong or is there any problem with my concept of L1 distance?