[SciPy-User] Order of numpy orperations is not equal to logic (and also octave)?

Andrew Jaffe a.h.jaffe at gmail.com
Sun Oct 31 11:33:11 EDT 2010


On 31/10/2010 08:34, Oz Nahum Tiram wrote:
> Hi Everyone,
>
> So here is something which I discovered lately and is making me wonder.
>
> I want to define a scalar which I call Net Absolute Mass Balance Error
> or in short NAMBE. This NAMBE is the absolute difference between a base
> vector and another vector, divided by the base vector and multiplied by
> a hundred, in pseudo-code notation:
>
> |NAMBE=sum(abs(a-b)/a)*100
>
> When I do it in python, I decided to break the line into two lines so
> the code is more readable:
>
> |>>>  a=np.array([0.1,0.1,0.1,0.1,0.1])
>
>>>>  b=np.array([0.1,0.1,0.1,0.1,0.1])*2
>
>>>>  b
> array([  0.2,    0.2,    0.2,    0.2,    0.2])
>
>>>>  a-b
> array([-0.1,  -0.1,  -0.1,  -0.1,  -0.1])
>
>>>>  s=np.sum(abs(a-b))
>
>>>>  s
> 0.5
>>>>  s/np.sum(a)
>
> 1.0
>
> I thought the numpy does everything element wise so if I do it one line,
> I noticed the the result is different:
>
> |>>>  s=np.sum(abs(a-b)/a)
>
>>>>  s
> 5.0

Math error here: sum((a-b)/a) IS NOT EQUAL TO sum(a-b)/sum(a).

Andrew





More information about the SciPy-User mailing list