1

I have a numpy array a of shape (x, y) and another array b of shape (x, z)

I'm trying to get the dot product between each row (dimension x) of a and b. Desired result: x amount of matrixes of shape (x, y) or a matrix of shape (x, y, z).

2
  • Are you sure you want a dot? Which axis are you summing? The result dimension looks more like an outer product. a[:,:,None] * b[:,None,:]
    – hpaulj
    Commented Jan 17, 2021 at 7:47
  • matmul can also handle this by adding a size 1 dimension for the sum-of-products, a[:,:,None]@b[:,None,:]
    – hpaulj
    Commented Jan 17, 2021 at 18:00

1 Answer 1

1

Simply use einsum -

import numpy as np

a = np.random.rand(10, 20)
b = np.random.rand(10, 30)

c = np.einsum('ij,ik->ijk', a, b)

print(c.shape)

Not the answer you're looking for? Browse other questions tagged or ask your own question.