Skip to content

MaskedArray.dot leaks masked values #5185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
abalkin opened this issue Oct 14, 2014 · 5 comments
Closed

MaskedArray.dot leaks masked values #5185

abalkin opened this issue Oct 14, 2014 · 5 comments

Comments

@abalkin
Copy link
Contributor

abalkin commented Oct 14, 2014

>>> numpy.__version__
'1.9.0'
>>> x = numpy.ma.masked_array([1., 3e10, 3], mask=[0, 1, 0])
>>> x.dot(x)
9e+20

Note that this is a regression from oldnumeric:

>>> import numpy.oldnumeric.ma
>>> numpy.__version__
'1.8.1'
>>> x = numpy.oldnumeric.ma.masked_array([1., 3e10, 3], mask=[0, 1, 0])
>>> x.dot(x)
array(10.0)
@charris
Copy link
Member

charris commented Oct 14, 2014

Curiously, the dot function behaves differently with the oldnumeric version.

In [6]: x.dot(x)
Out[6]: array(10.0)

In [7]: dot(x, x)
Out[7]: 9e+20

In [8]: np.__version__
Out[8]: '1.8.1'

@abalkin
Copy link
Contributor Author

abalkin commented Oct 14, 2014

Not really, ma.dot behaves the same in oldnumeric.ma and ma.

>>> numpy.ma.dot(x, x)
masked_array(data = 10.0,
             mask = False,
       fill_value = 1e+20)

@abalkin
Copy link
Contributor Author

abalkin commented Oct 14, 2014

Note that MaskedArray.sum properly skips masked values and we have

>>> (x*x).sum()
10.0
>>> x.dot(x)
9e+20

@abalkin
Copy link
Contributor Author

abalkin commented Oct 14, 2014

What is the "right" behavior here?

Since ma.dot(x, y) is defined as numpy.dot(x.filled(0), y.filed(0)), there is a strong precedent for having MaskedArray.dot do the same, but ma.dot has this unfortunate behavior:

>>> print x
[1.0 -- 3.0]
>>> print numpy.ma.dot(numpy.eye(3), x)
[1.0 0.0 3.0]

It would be better if masked values behaved as "weak nan" and eye.dot(x) be the same as x. ("Weak nan" has the property nan * 0 = 0.)

abalkin added a commit to abalkin/numpy that referenced this issue Mar 23, 2015
MaskedArray used to inherit ndarray.dot which ignored masks in the operands.

Fixes issue numpy#5185.
@jjhelmus
Copy link
Contributor

I believe this issue was fixed by PR #5709 as indicated in the comments. Can you verify @abalkin?

Also, the same issue seems to have been reported in gh-3219, did the PR fix this also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants