-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
ENH: Add where argument to np.mean #15852
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
6795608 to
cb24386
Compare
cb24386 to
046c7fd
Compare
|
Thanks for the |
046c7fd to
8abc301
Compare
numpy/core/_methods.py
Outdated
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.
We should check which cases ufunc.reduce (c function name is something like GenericReduction) has this optimization for, and try the same ones. In particular, I'm trying to remember if we optimized for np.bool_.
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 found the PyArray_GenericReduceFunction in numpy/core/src/multiarray/number.c however that function looks more like a wrapper, being only 15 lines long. Could you point me in the right direction?
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 think @eric-wieser is indicating at this function : PyUFunc_GenericReduction in numpy/core/src/umath/ufunc_object.c . I wasn't able to find any special casing for np.bool except that where = True is being treated as if no where is provided.
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.
OK thank you for checking @anirudh2290.
8abc301 to
5d72822
Compare
5d72822 to
771126a
Compare
771126a to
6ca4f60
Compare
numpy/core/_methods.py
Outdated
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.
This doesn't do the right thing for arrays containing np.inf. I recommend we skip ptp for now, and attempt it in a future PR - it's sufficiently different from mean and var that I think it's worth handling separately, so that we can get the rest merged.
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.
Alright, I will remove changes made to ptp and upload a commit without it.
adb7428 to
08b0ccc
Compare
|
@eric-wieser I removed all changes related to |
numpy/core/_methods.py
Outdated
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.
The kwarg will have some performance implications for small arrays <1000 elements or so, until my parsing improvements would be merged. But just a note, to be honest there is not much we can do about it. It seems passing by position is thwarted by the initial argument (though that could possibly be changed).
@eric-wieser do you want to persue this for 1.19? I will push it off for now.
|
@eric-wieser would you have time to take a last quick glance at this so it could maybe go in? |
seberg
left a comment
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.
Hmm, sorry for not moving this forward earlier. @eric-wieser you were happy with this? It seems good to me, would do another more careful path before merging maybe, but seems all good.
@sgasse would you be willing to add an improvement release note for this? since these are all reduction like functions, adding an argument that all reductions accept seems like a straight forward API decision.
|
Hey @seberg , nice to hear back on this pull request :) |
|
Thanks, releaes notes would go into |
Harmonize the signature of np.mean, np.var np.std, np.any, np.all, and their respective nd.array methods with np.sum by adding a where argument, see numpygh-15818.
ba45f07 to
4ec1dbd
Compare
|
Alright, thank you @seberg for the location of of the release notes. I added a document there and updated all the |
|
Sorry, I was AFK for a bit. The release notes seem long but fine. @eric-wieser if there was anything you felt still doing here please have a quick look. I somewhat remembered that you almost had signed this off. But maybe that was not the case. As a general note, the slowdown is: vs. For |
|
Ping everyone here. Sounds like this is ready. |
|
I am OK with putting it in as is. The only thing that made me pause in the first place was the slowdown, which is a silly reason probably. |
|
In it goes, thanks @sgasse . |
Harmonize the signature of np.mean with np.sum by adding a where
argument, closes gh-15818.