[Numpy-tickets] [NumPy] #435: numpy.mean(): accumulator default type should not be single precision
NumPy
numpy-tickets at scipy.net
Wed Jan 24 12:18:06 CST 2007
#435: numpy.mean(): accumulator default type should not be single precision
------------------------+---------------------------------------------------
Reporter: chanley | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone:
Component: numpy.core | Version: devel
Severity: normal | Keywords:
------------------------+---------------------------------------------------
The accumulator used in the mean algorithm should not be single precision
by default. This default can cause unexpected results. Please see the
following example:
{{{
In [5]: a.dtype
Out[5]: dtype('>f4')
In [6]: print a
[[ 132. 132. 132. ..., 132. 132. 132.]
[ 132. 132. 132. ..., 132. 132. 132.]
[ 132. 132. 132. ..., 132. 132. 132.]
...,
[ 132. 132. 132. ..., 132. 132. 132.]
[ 132. 132. 132. ..., 132. 132. 132.]
[ 132. 132. 132. ..., 132. 132. 132.]]
In [7]: a.min()
Out[7]: 132.0
In [8]: a.max()
Out[8]: 389.0
In [9]: a.mean()
Out[9]: 129.742439153
}}}
However, if you recast the array as float64 you get the correct result:
{{{
In [11]: a.astype(numpy.float64).mean()
Out[11]: 132.062805059
}}}
I believe that double precision would be a more appropriate default type
for the accumulator.
--
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/435>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.
More information about the Numpy-tickets
mailing list