I have a numpy array, a, a.shape=(48,90,144). I want to take the weighted average of a along the first axis using the weights in array b, b.shape=(90,144). So the output should be a numpy array of shape (48,).
I know this can be done with a list comprehension:
np.array([np.average(a[i], weights=b) for i in range(48)])
But I’d like to avoid having to convert from a list back to a numpy array.
Can anyone help? I’m sure this is possible using numpy functions and slicing, but I’m stuck. Thanks!
In a single line:
You can test it with: