[Numpy-discussion] Weird problem with subtracting ndarrays

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jun 12 15:44:32 EDT 2013


On Wed, Jun 12, 2013 at 3:38 PM, Moroney, Catherine M (398D)
<Catherine.M.Moroney at jpl.nasa.gov> wrote:
> Thanks for the tip.  I thought it must be something simple like that.  When I convert
> the arrays to numpy.int32 things behave normally.
>
> Another question though:  a numpy.all() on the signed-int arrays shows that they
> are equal.  So why would the subtraction of the unsigned arrays wrap around like that?

in your code
>>> numpy.all(counts1 == counts2)
False

Josef

>
> Catherine
>
> On Jun 12, 2013, at 12:25 PM, <numpy-discussion-request at scipy.org>
>  wrote:
>
>> Message: 9
>> Date: Wed, 12 Jun 2013 15:30:22 -0400
>> From: Warren Weckesser <warren.weckesser at gmail.com>
>> Subject: Re: [Numpy-discussion] weird problem with subtracting
>>       ndarrays
>> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
>> Message-ID:
>>       <CAGzF1udhTAmxPWwHsd9xQ386CBz2iXymmYC4kJJ3AOTRPZQzyg at mail.gmail.com>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> On Wed, Jun 12, 2013 at 3:25 PM, Moroney, Catherine M (398D) <
>> Catherine.M.Moroney at jpl.nasa.gov> wrote:
>>
>>> Hello,
>>>
>>> I've got two arrays of the same shape that I read in from a file, and I'm
>>> trying to
>>> difference them.  Very simple stuff, but I'm getting weird answers.
>>>
>>> Here is the code:
>>>
>>>>>> counts1 = hfile1.read_grid_field("CFbA",
>>> "TerrainReferencedRCCMFraction_Num")
>>>>>> counts2 = hfile2.read_grid_field("CFbA",
>>> "TerrainReferencedRCCMFraction_Num")
>>>>>> counts1.max(), counts2.max()
>>> (13, 13)
>>>>>> counts1.min(), counts2.min()
>>> (0, 0)
>>>>>> numpy.all(counts1 == counts2)
>>> False
>>>>>> diff = counts1 - counts2
>>>>>> diff.max()
>>> 4294967295      !! WHAT IS HAPPENING HERE ??
>>>>>> sum = counts1 + counts2
>>>>>> sum.max()
>>> 26
>>>
>>> As you can see, the range of values in both arrays is 0 to 13, and the sum
>>> behaves normally, but the difference gives this weird number.
>>>
>>> When I create dummy arrays, the subtraction works fine.  So there must be
>>> some funny value
>>> lurking in either the counts1 or counts2 array, but the numpy.isnan() test
>>> returns False.
>>>
>>> Any ideas for how I debug this?
>>>
>>> Catherine
>>>
>>>
>> Check the dtype of the arrays.  They are probably unsigned integers, and
>> the subtraction leads to wrap-around in some cases.
>>
>> For example:
>>
>> In [1]: x = np.array([0, 1, 2], dtype=np.uint32)
>>
>> In [2]: y = np.array([1, 1, 1], dtype=np.uint32)
>>
>> In [3]: x - y
>> Out[3]: array([4294967295,          0,          1], dtype=uint32)
>>
>>
>> Warren
>>
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list