10000 Add `keepdims` argument to argmin and argmax · Issue #8710 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Add keepdims argument to argmin and argmax #8710

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
eric-wieser opened this issue Feb 27, 2017 · 7 comments · Fixed by #19211
Closed

Add keepdims argument to argmin and argmax #8710

eric-wieser opened this issue Feb 27, 2017 · 7 comments · Fixed by #19211

Comments

@eric-wieser
Copy link
Member
eric-wieser commented Feb 27, 2017

For consistency with min and max, so that the returned object is the same shape.

@eric-wieser
Copy link
Member Author

This might actually be done best by making argmin a gufunc?

@eric-wieser
Copy link
Member Author
eric-wieser commented Feb 28, 2017

Trivial implementation:

def argmin(*args, keepdims=False, **kwargs):
    res = np.argmin(*args, **kwargs)
    axis = kwargs.get('axis', none)
    if axis is not None:
        res = np.expand_dims(res, axis=axis)
    return res

@shoyer
Copy link
Member
shoyer commented Feb 28, 2017

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)->().

@eric-wieser
Copy link
Member Author

The next problem is that argmin supports axis=None, which ufuns do not. Perhaps argmin should just wrap a ufunc?

@FabianSchuetze
Copy link

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
Copy link
yarki commented 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.

def argmax_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] = 1
    return np.argmax(x, axis=axis).reshape(output_shape)

@arvoelke
Copy link
Contributor

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.

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

Successfully merging a pull request may close this issue.

5 participants
0