1

There are two arrays and I want to get distance between two arrays based on known individual elements distance.

dist = {(4,3): 0.25, (4,1):0.75, (0,0):0, (3,3):0, (2,1):0.25, (1,0): 0.25}
a = np.array([[4, 4, 0], [3, 2, 1]])
b = np.array([[3, 1, 0]])

a
array([[4, 4, 0],
       [3, 2, 1]])
b
array([[3, 1, 0]])

expected output based on dictionary dist:
array([[0.25, 0.75, 0.  ],
       [0.  , 0.25, 0.25]])

So, if we need which elements are different we can do a!=b. Similarly, instead of !=, I want to apply the below function -

def get_distance(a, b):
    return dist[(a, b)]

to get the expected output above.

I tried np.vectorize(get_distance)(a, b) and it works. But I am not sure if it is the best way to do the above in vectorized way. So, for two numpy arrays, what is the best way to apply custom function/operator?

ggaurav
  • 1,764
  • 1
  • 10
  • 10

1 Answers1

1

Instead of storing your distance mapping as a dict, use a np.array for lookup (or possibly a sparse matrix if size becomes an issue).

d = np.zeros((5, 4))
for (x, y), z in dist.items():
    d[x, y] = z

Then, simply index.

>>> d[a, b]
array([[0.25, 0.75, 0.  ],
       [0.  , 0.25, 0.25]])

For a sparse solution (code is almost identical):

In [14]: from scipy import sparse

In [15]: d = sparse.dok_matrix((5, 4))

In [16]: for (x, y), z in dist.items():
    ...:     d[x, y] = z
    ...:

In [17]: d[a, b].A
Out[17]:
array([[0.25, 0.75, 0.  ],
       [0.  , 0.25, 0.25]])
user3483203
  • 50,081
  • 9
  • 65
  • 94