Skip to content Skip to sidebar Skip to footer

Vectorized/broadcasted Dot Product Of Numpy Arrays With Different Dimensions

The Problem: I want to calculate the dot product of a very large set of data. I am able to do this in a nested for-loop, but this is way too slow. Here is a small example: import n

Solution 1:

You can use np.einsum -

np.einsum('ij,ikj->kj',a,b)

Explanation :

  • Keep the last axes aligned for the two inputs.

  • Sum-reduce the first from those.

  • Let the rest stay, which is the second axis of b.

Usual rules on whether to use einsum or stick to a loopy-dot based method apply here.

Solution 2:

numpy.dot does not reduce the first dimension. From the docs:

For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

That is exactly what the error is telling you: it is attempting to match axis 2 in the first vector to axis 1 in the second.

You can fix this using numpy.rollaxis or better yet numpy.moveaxis. Instead of a = a[:, None, :], do

a = np.movesxis(a, 0, -1)
b = np.moveaxis(b, 0, -2)
Z = np.dot(a, b)

Better yet, you can construct your arrays to have the correct shape up front. For example, transpose lines and do a = np.diff(lines, axis=0).

Post a Comment for "Vectorized/broadcasted Dot Product Of Numpy Arrays With Different Dimensions"