Reputation: 17621
I have two numpy arrays, one shaped (3000,) and the other is an array of twenty 3000 by 3000 matrices, i.e. shape (20, 3000, 3000)
first.shape = (3000,)
second.shape = (20, 3000, 3000)
I being doing a numpy dot product.
import numpy as np
dotprod1 = np.dot( second, first)
this works, and the output dotprod1
is an array shaped (20, 3000).
But what if I wish to take the dot product again?
dotprod2 = np.dot( first, dotprod1)
This gives an error.
ValueError: shapes (3000,) and (20,3000) not aligned: 3000 (dim 0) != 20 (dim 0)
I would like to have an output of 20 values. How does one use broadcasting to do this?
Upvotes: 3
Views: 8507
Reputation: 33997
dotprod2 = np.dot( first, dotprod1)
fails because first
is of shape (3000, )
and dotprod1
is of shape (20, 3000)
, swap them and the error will go (if that's your intention):
dotprod2 = np.dot(dotprod1, first)
besides, you can also use np.ndarray.dot
to make the semantics clear:
dotprod2 = dotprod1.dot(first)
Upvotes: 3