Issue
I have this sample array:
In [38]: arr
Out[38]: array([ 0, 44, 121, 154, 191])
The above is just a sample whereas my actual array size is pretty huge. So, what is an efficient way to compute a distance matrix?
The result should be:
In [41]: res
Out[41]:
array([[ 0, 44, 121, 154, 191],
[ -44, 0, 77, 110, 147],
[-121, -77, 0, 33, 70],
[-154, -110, -33, 0, 37],
[-191, -147, -70, -37, 0]])
I wrote a for
loop based implementation which is too slow. Could this be vectorized for efficiency reasons?
Solution
There's subtract
.outer
, which effectively performs broadcasted subtraction between two arrays.
Apply the ufunc
op
to all pairs (a, b) with a in A and b in B.Let M = A.ndim, N = B.ndim. Then the result, C, of
op.outer(A, B)
is an array of dimension M + N such that:C[i_0, ..., i_{M-1}, j_0, ..., j_{N-1}] = op(A[i_0, ..., i_{M-1}],B[j_0, ..., j_{N-1}])
np.subtract.outer(arr, arr).T
Or,
arr - arr[:, None] # essentially the same thing as above
array([[ 0, 44, 121, 154, 191],
[ -44, 0, 77, 110, 147],
[-121, -77, 0, 33, 70],
[-154, -110, -33, 0, 37],
[-191, -147, -70, -37, 0]])
Answered By - cs95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.