X
and Y
are both 3d arrays with dimensions (a,b,c)
. My goal is to do a dot product.
Consider that case where index i
and j
are scalar, and (X[i,:,j].T).dot(Y[i,:,j])
would be simple and return a scalar.
However, if I try to do vectorization, i
and j
become 1d arrays, and (X[i,:,j].T).dot(Y[i,:,j])
return a matrix but I am expecting a 1d array as result. How do I get around this problem ?
asked Mar 20, 2020 at 12:42
-
1you can use np.einsum to calculate the product. if you can, add some random values with expected outputAly Hosny– Aly Hosny2020年03月20日 12:49:28 +00:00Commented Mar 20, 2020 at 12:49
-
it would be nice if you could give us coherent codebjornsing– bjornsing2020年03月20日 12:53:40 +00:00Commented Mar 20, 2020 at 12:53
1 Answer 1
Naive implementation using list comprehension:
a,b,c = X.shape
r1 = [(X[i,:,j].T).dot(Y[i,:,j]) for i in range(a) for j in range(c)]
Implementation using np.einsum:
r2 = np.einsum('ijk,ijk->ik', X,Y).flatten()
answered Mar 20, 2020 at 13:25
Comments
lang-py