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
This might actually be done best by making argmin a gufunc?
The challenge here is that only ufunc.reduce has the logic for axis handling. That's not something that exists for general ufuncs, though it probably should (#5197), at least for simple cases like these where the ufunc signature looks like (n)->().
I would be glad about this features. As I was thinking of trying to contribute to numpy for some time, would somebody mind if I simply tried to implement the feature according to the development workflow?
Folks, below is the workaround I'm currently using. Please reply if you see a better way to preserve original dimensions. Thanks.
defargmax_keepdims(x, axis):
""" Returns the indices of the maximum values along an axis. The axis which is reduced is left in the result as dimension with size one. The result will broadcast correctly against the input array. Original numpy.argmax() implementation does not currently support the keepdims parameter. See https://github.com/numpy/numpy/issues/8710 for further information. """output_shape=list(x.shape)
output_shape[axis] =1returnnp.argmax(x, axis=axis).reshape(output_shape)
Reacted by David Refaeli, Yuantao Feng, Discreater and Jianyu Li
In my case np.unravel_index(np.argmax(x), x.shape) did what I needed (flattens the array, takes the argmax, and then reinterprets the index with respect to the original shape). Your mileage may vary depending on what you are trying to do of course.
Activity
eric-wieser commentedon Feb 27, 2017
This might actually be done best by making
argminagufunc?eric-wieser commentedon Feb 28, 2017
Trivial implementation:
shoyer commentedon Feb 28, 2017
The challenge here is that only
ufunc.reducehas the logic for axis handling. That's not something that exists for general ufuncs, though it probably should (#5197), at least for simple cases like these where the ufunc signature looks like(n)->().eric-wieser commentedon Dec 1, 2018
The next problem is that
argminsupportsaxis=None, which ufuns do not. Perhaps argmin should just wrap a ufunc?FabianSchuetze commentedon Mar 9, 2019
I would be glad about this features. As I was thinking of trying to contribute to numpy for some time, would somebody mind if I simply tried to implement the feature according to the development workflow?
yarki commentedon Jul 14, 2019
Folks, below is the workaround I'm currently using. Please reply if you see a better way to preserve original dimensions. Thanks.
arvoelke commentedon Sep 12, 2020
In my case
np.unravel_index(np.argmax(x), x.shape)did what I needed (flattens the array, takes the argmax, and then reinterprets the index with respect to the original shape). Your mileage may vary depending on what you are trying to do of course.