[Numpy-discussion] A correction to numpy trapz function
Ryan May
rmay31@gmail....
Sat Jul 12 14:24:52 CDT 2008
Nadav Horesh wrote:
> Here is what I get with the orriginal trapz function:
>
> IDLE 1.2.2
>>>> import numpy as np
>>>> np.__version__
> '1.1.0'
>>>> y = np.arange(24).reshape(6,4)
>>>> x = np.arange(6)
>>>> np.trapz(y, x, axis=0)
>
> Traceback (most recent call last):
> File "<pyshell#4>", line 1, in <module>
> np.trapz(y, x, axis=0)
> File "C:\Python25\Lib\site-packages\numpy\lib\function_base.py", line 1536, in trapz
> return add.reduce(d * (y[slice1]+y[slice2])/2.0,axis)
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
(Try not to top post on this list.)
I can get it to work like this:
import numpy as np
y = np.arange(24).reshape(6,4)
x = np.arange(6).reshape(-1,1)
np.trapz(y, x, axis=0)
From the text of the error message, you can see this is a problem with
broadcasting. Due to broadcasting rules (which will *prepend*
dimensions with size 1), you need to manually add an extra dimension to
the end. Once I resize x, I can get this to work. You might want to
look at this: http://www.scipy.org/EricsBroadcastingDoc
Ryan
--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
More information about the Numpy-discussion
mailing list