-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Feature request: axis and keepdims for gufuncs (and use it in np.median) #8810
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
Comments
There's some extended discussion about this on the mailing list. (and in #5197) I think it might be worth special-casing signatures of the form |
Ah, yes, I should have checked for duplication; maybe this issue can be specifically about implementation. In that respect, good point about |
I don't think gufuncs are really a good fit for an axis argument as they are currently conceived. It might be good to make a list of the functions we would like to implement as ufuncs to see what would be the best solution. |
Thinking more carefully, any reduction of the form |
@charris - I think it will actually be quite trivial to implement (but realise that I may have to prove that...) |
I don't think adding We could certainly add another override for functions with the standard logic for handling |
@shoyer - maybe p.s. Just to be clear: my goal is not to make every numerical function fit in the |
I'm not so much interested in the difficulty as the usefulness. Would it be correct to say that the main interest in adding an axis is to reuse the override functionality? |
@charris - It is a combination of liking the idea of being able to override numerical functions and direct usefulness. For the latter, I am thinking in particular of #8528 ( |
See #8819 for a trial implementation of adding an Added notes:
|
Looking at this issue again, I realized it can actually be closed: gufuncs now have |
EDITS following different comments:
keepdims
. See also previous discussion in Add an axis argument to generalized ufuncs? #5197 and on https://mail.scipy.org/pipermail/numpy-discussion/2014-October/071455.html (which independently suggested more or less the same syntax).axes
; adjusted implementation notes accordingly.Rationale
Following the suggestion that, ideally,
np.median
and similar functions would begufuncs
so that they could be overridden with__array_ufunc__
(#8247), it was realised that the main show-stopper is the absence of anaxis
argument for gufuncs (also annoying in the context of the newall_equal
gufuncs (#8528). Furthermore, for functions likeargmax
, etc., akeepdims
would be very useful (#8710).Specification
In practice, the new
axis
argument would use underneath a more generalaxes
argument, which is a list of tuples of axes for each of the core dimensions. For normalufunc
,axes
obviously has to be empty (or absent); forgufunc
, defaults would be-1
,-2
, etc., as needed. For the output, the tuple would be extended by one (or possibly more) from that implied by the signature ifkeepdims
is set . Short-cuts may be possible, e.g., for gufuncs with only one index, a single number (or perhaps evenNone
for ravelling all dimensions); one would distinguish the general case ofaxes
with the simple case ofaxis
.Implementation
Looking at the reference, it would seem that the C-api would not need to change, but the routine actually calling the underlying iteration machinery in
umath/ufunc_object.c
would need to do appropriate transposes of the input and output arrays. Going through the call sequence to see places where change would be necessary (not marked withX
):ufunc_generic_call
(all OK)PyUFunc_CheckOverride
(OK,axis
should already be inkwds
)PyUFunc_GenericFunction
: signal that non-emptyaxis
should error.PyUFunc_GeneralizedFunction
: do a remap of axes for the checks and before passing on to the iterator;get_ufunc_arguments
: interpretaxes
argument (new return argument); error if not needed. ENH: Implement axes keyword argument for gufuncs. #8819axis
one for just a single axis. ENH: Implement axis for generalized ufuncs. #11018keep_dims
; ENH: Implement axis for generalized ufuncs. #11018Excluded
In principle, it might be nice to allow
axis
to refer to multiple axes or all of them (None
), but this is substantially more complicated, and it is not obvious it isn't better to let wrapping code deal with this. Indeed, this is done fornp.median
, which ravels any requested axes as needed before passing the array on topartition
(which can handle only a single axis).The text was updated successfully, but these errors were encountered: