I have a 3D Numpy array and would like to take the average value along one axis, given some elements from two other dimensions.
This is an example code depicting my problem:
import numpy as np myarray = np.random.random((5,10,30)) yy = [1,2,3,4] xx = [20,21,22,23,24,25,26,27,28,29] mymean = [ np.mean(myarray[t,yy,xx]) for t in np.arange(5) ]
However, this leads to:
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Why does indexing, for example, myarray [:, [1,2,3,4], [1,2,3,4]] work, but not my code above?
source share