-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: np.ma.mean and var should return scalar if no mask #8142
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
Conversation
f40633e
to
2f792b1
Compare
Hmm this led me to notice some other (old) problems with |
cnt = self.count(axis=axis, **kwargs) - ddof | ||
danom = self - self.mean(axis, dtype, keepdims=True) | ||
if iscomplexobj(self): | ||
danom = umath.absolute(danom) ** 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to check for a complex object, but might as well do (there was a PR to make a ufunc out of this...):
danom = danom.real**2 + danom.imag**2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to leave this alone for now, as outside the scope of this PR. (this code is already present).
@ahaldane Still planning more for this PR? |
Yeah, I need a day or two more to decide what to do in I have an old branch lying around with a "proper" overhaul of |
2f792b1
to
d8d7c25
Compare
I finished my changes here, it should be ready to go. |
@@ -5057,7 +5057,7 @@ def mean(self, axis=None, dtype=None, out=None, keepdims=np._NoValue): | |||
|
|||
if self._mask is nomask: | |||
result = super(MaskedArray, self).mean(axis=axis, | |||
dtype=dtype, **kwargs) | |||
dtype=dtype, **kwargs)[()] | |||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the super mean returns a 0-d array scalar for relevant cases. I note the following odd behavior of ordinary arrays that is new to me.
>>> np.float64(1)
1.0
>>> np.float64(1)[()]
array(1.0)
>>> np.float64(1)[()][()]
1.0
That is, the [()]
construct flips the result back and forth between arrays and scalars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For anyone coming here in future, the above is no longer (1.11.1) the case. [()]
does not promote scalars to 0d-arrays
I note some odd behavior of
|
What numpy version? I don't get that on 1.11, and I think there is a lot of numpy code depends on |
Hmm, but actually maybe you are right. I just remembered #7267. And actually grepping shows the empty tumple indexing is only used in a few places in python, and I know of only one place in C it is used. |
I'm running on my travel machine that has numpy 1.8. I can't upgrade until I figure out how to get Cython installed. Yeah, my mac environment is a mess, I don't use it often enough... |
I'm not particularly worried as long as the tests cover the relevant possibilities. |
If you can't replicate what I'm seeing on 1.8 let's give it a shot. Thanks Allan. |
Thanks Chuck! |
As discussed in my comments for issue numpy#8145, this patch adds the equal_nan argument to assert_array_compare(), and assert_allclose() passes the value it receives for the same argument through to assert_array_compare(). Closes numpy#8142.
Fixes #5769
This is a followup to #7350 (first added in 1.12), and makes sure
np.ma.mean
andnp.ma.var
return a scalar if appropriate in thenomask
branch of the code.