[Numpy-discussion] Masked Array Usage Problems
Sun Apr 11 00:00:10 CDT 2010
On Apr 10, 2010, at 5:17 AM, firstname.lastname@example.org wrote:
> On Sat, Apr 10, 2010 at 3:49 AM, Lane Brooks <email@example.com> wrote:
>> I am trying out masked arrays for the first time and having some
>> problems. I have a 2-D image as dtype=numpy.int16
>> I create a mask of all False to not mask out any pixels.
>> I calculate the mean of the image original image and it comes out
>> I calculate the mean of the masked array and it comes out differently
>> around -179. It produces the same negative mean value no matter what
>> masks I try, e.g. (all True, all False, etc). Furthermore there
>> are no
>> negative samples in the entire array.
>> Any ideas on what am I doing wrong?
>> Here is some sample code showing the behavior:
>> In : img.dtype, img.shape
>> Out: (dtype('int16'), (3200, 3456))
>> In : mask = numpy.zeros(img.shape, dtype=bool)
>> In : imgma = ma.masked_array(img, mask)
>> In : img.mean()
>> Out: 597.15437617549185
>> In : imgma.mean()
>> Out: -179.56858678747108
>> In : imgma.min()
>> Out: 25
>> In : numpy.__version__
>> Out: '1.3.0'
>> In : numpy.ma.__version__
>> Out: '1.0'
> Just a guess untill Pierre replies:
> It looks to me like an integer overflow bug. Can you try
> to do the accumulation with floating points?
Indeed using dtype=float solved the problem. The numpy.mean doc string
says the default accumulator type for all int types is a float. Why is
ma.mean different, especially since the ma.mean doc string says to
see the numpy.mean doc string?
More information about the NumPy-Discussion