|
From: Benjamin R. <ben...@ou...> - 2012-09-26 13:25:56
|
On Wed, Sep 26, 2012 at 4:31 AM, Pierre Haessig <pie...@cr...>wrote:
> Hi,
>
> Just a detail :
>
> Le 26/09/2012 04:29, Paul Tremblay a écrit :
>
> percent = (np.divide(the_cumsum, the_sum)) * 100
>
> This lines doesn't work on my computer (numpy 1.6.2)
>
> Indeed, there is a casting issue :
> In [2]: percent
> Out[2]: array([ 0, 0, 0, 0, 100])
>
> However, using the regular "/" operator instead of np.divide gives the
> proper result:
> In [8]: the_cumsum/the_sum*100
> Out[8]: array([ 42.10526316, 71.05263158, 90.78947368, 97.36842105,
> 100. ])
>
> Best,
> Pierre
>
>
Actually, if you are using the latest numpy (the 1.7 beta), that will also
not work unless you are using py3k or did "from __future__ import
division". Well, actually, using np.divide will always result in integer
division (this may or may not be a bug).
The correct thing to do until we move on to py3k with its "true division"
is to make sure we cast one of the operands as floats:
the_sum.astype('f'). Plus, using '/' is more concise and readable.
Cheers!
Ben Root
|