You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
...it has masked the nan and inf values even though they are not -1
If we divide xm by y and get just the data we get:
(xm/y).dataOut[242]: array([0., 1., 0., 1.])
🤯.. now we have data where I would expect nan and inf. I'm not very experienced in Python but this looks like an unexpected result and I thought I should report it (I spent alot of time tracing unexpected results from a function that turns out to be due to this).
Edit: Where I found this in the wild is even more insidious because there was no specific .data step - it happened silently as follows:
Suppose our calculation was done in a function:
def somefunc3(a,b):
c = a / b
return c
somefunc3(xm, y)
Out[76]:
masked_array(data=[--, --, --, --],
mask=[ True, True, True, True],
fill_value=-1.0,
dtype=float64)
It output the masked array without nan and inf.
Now suppose we were stuffing the result of somefunc into a larger array:
d = np.zeros((4, 2))
d[:,0] = somefunc3(xm, y)
d
Out[77]:
array([[0., 0.],
[1., 0.],
[0., 0.],
[1., 0.]])
Now it silently converted the masked array back to a regular array and put in 1 or 0 when it should be nan or inf. Note that when I ran this on my machine I got a divide by zero warning only one time, but all other times I ran it I did not (I have no idea why).
NumPy/Python version information:
1.18.1 3.7.6 (default, Jan 8 2020, 13:42:34)
[Clang 4.0.1 (tags/RELEASE_401/final)]
Edit: same behaviour on my other machine with versions:
1.19.2 3.8.5
[Clang 10.0.0]
The text was updated successfully, but these errors were encountered:
jpkrooney
changed the title
Possible masked array divide by zero bug
BUG (Possible): masked array divide by zero array seems to screen out nan and inf
Apr 9, 2021
While reproducing this myself, I found something more interesting, the division actually doesn't mask outputs that are actually -1, but incorrectly masks inf and nan
I tried division on other masked_arrays but the values are not being masked at all after division. I am guessing division with masked arrays is supposed to work this way, and doesn't actually mask values on its own. I further assumed that nans and infs get masked by default since they're invalid numbers in most cases, though that is not the case when I do this:
This makes me think it's not a bug/feature with the masking itself, but rather the way masked_arrays interact with the divide operator. Even if it's not a bug, it could create a lot of misunderstandings.
Edit: Looked at some other issues regarding masked arrays. Seemingly masked arrays give unexpected results when used with functions not part of the ma module.
Under certain circumstances dividing a masked array by regular array with zeros seems to unexpactantly screen out
nan
andinf
answers.Reproducing code example:
If we divide x by y we get:
If we divide xm by y we get:
...it has masked the
nan
andinf
values even though they are not -1If we divide xm by y and get just the data we get:
🤯.. now we have data where I would expect
nan
andinf
. I'm not very experienced in Python but this looks like an unexpected result and I thought I should report it (I spent alot of time tracing unexpected results from a function that turns out to be due to this).Edit: Where I found this in the wild is even more insidious because there was no specific
.data
step - it happened silently as follows:Suppose our calculation was done in a function:
It output the masked array without nan and inf.
Now suppose we were stuffing the result of
somefunc
into a larger array:Now it silently converted the masked array back to a regular array and put in 1 or 0 when it should be
nan
orinf
. Note that when I ran this on my machine I got a divide by zero warning only one time, but all other times I ran it I did not (I have no idea why).NumPy/Python version information:
1.18.1 3.7.6 (default, Jan 8 2020, 13:42:34)
[Clang 4.0.1 (tags/RELEASE_401/final)]
Edit: same behaviour on my other machine with versions:
1.19.2 3.8.5
[Clang 10.0.0]
The text was updated successfully, but these errors were encountered: