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?

2

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Good one. I think it can not get better than this. I got stuck with dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.