[Numpy-discussion] Bug or documentation omission with dtype parameter with numpy.mean

Bruce Southey bsouthey@gmail....
Thu Aug 6 12:25:21 CDT 2009


Hi,
Should numpy.mean() (and similar functions) maintain the original dtype 
or the dtype used for the dtype option?
I do understand the numerical issues involved but my question relates to 
whether this should be a bug or needs clarification in the documentation.

According to the help, the dtype parameter to numpy.mean() says:
     dtype : dtype, optional
         Type to use in computing the mean. For integer inputs, the default
         is float64; for floating point, inputs it is the same as the input
         dtype.

I interpret this to indicate the type used in the internal calculations 
and technically does not say what the output dtype should be. But the 
dtype option does change the output dtype as the simple example below 
shows.

With Python 2.6 and numpy '1.4.0.dev7282' on Linux 64-bit Fedora 11

 >>> import numpy as np
 >>> a=np.array([1,2,3])
 >>> a.dtype
dtype('int64')
 >>> a.mean().dtype
dtype('float64')
 >>> a.mean(dtype=np.float32).dtype
dtype('float64')
 >>> a.mean(dtype=np.float64).dtype
dtype('float64')
 >>> a.mean(dtype=np.float128).dtype
dtype('float128')
 >>> a.mean(dtype=np.int).dtype
dtype('float64')

Clearly the output dtype is float64 or higher as determined by the input 
dtype or dtype parameter.

Bruce


More information about the NumPy-Discussion mailing list