[Numpy-discussion] numpy.mean still broken for largefloat32arrays

Daπid davidmenhur at gmail.com
Mon Jul 28 09:30:24 EDT 2014


On 28 July 2014 14:46, Sebastian Berg <sebastian at sipsolutions.net> wrote:

> > To rephrase my most pressing question: may np.ones((N,2)).mean(0) and
> > np.ones((2,N)).mean(1) produce different results with the
> > implementation in the current master? If so, I think that would be
> > very much regrettable; and if this is a minority opinion, I do hope
> > that at least this gets documented in a most explicit manner.
> >
>
> This will always give you different results. Though in master. the
> difference is more likely to be large, since (often the second one)
> maybe be less likely to run into bigger numerical issues.
>


An example using float16 on Numpy 1.8.1 (I haven't seen diferences with
float32):

for N in np.logspace(2, 6):
        print N, (np.ones((N,2), dtype=np.float16).mean(0), np.ones((2,N),
dtype=np.float16).mean(1))

The first one gives correct results up to 2049, from where the values start
to fall. The second one, on the other hand, gives correct results up to
65519, where it blows to infinity.

Interestingly, in the second case there are fluctuations. For example, for
N = 65424, the mean is 0.99951172, but 1 for the next and previous numbers.
But I think they are just an effect of the rounding, as:

In [33]: np.ones(N+1, dtype=np.float16).sum() - N
Out[33]: 16.0

In [35]: np.ones(N+1, dtype=np.float16).sum() - (N +1)
Out[35]: 15.0

In [36]: np.ones(N-1, dtype=np.float16).sum() - (N -1)
Out[36]: -15.0

In [37]: N = 65519 - 20

In [38]: np.ones(N, dtype=np.float16).sum() - N
Out[38]: 5.0

In [39]: np.ones(N+1, dtype=np.float16).sum() - (N +1)
Out[39]: 4.0

In [40]: np.ones(N-1, dtype=np.float16).sum() - (N -1)
Out[40]: 6.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140728/3651dbd9/attachment.html>


More information about the NumPy-Discussion mailing list