Skip to content

DOC: argmin, argmax, argort, etc. should link to np.take_along_axis in their documentation #6078

Closed

Description

@mhvk

In #6075, I wondered whether it might be possible to

to have some simple way to use the output of np.arg[min|max|sort] as a proper index (to avoid hackery as in https://github.com/astropy/astropy/blob/master/astropy/time/core.py#L896 -- if there is a simpler way already, let me know!)

and @jaimefrio suggested that it might be simpler to have

a make_me_an_indexing_tuple=False new keyword argument for the argxxx functions?

That indeed seems better, so I'm raising this as a separate issue. The code linked to above could be used as a template on how to generate the index tuple.

Activity

  1. shoyer commented on Jul 17, 2015

    @shoyer
    Member

    Indeed, this problem has also flummoxed me.

    Here's the helper function I wrote, which may be slightly more interpretable:

    def argsort_indices(a, axis=-1):
        """Like argsort, but returns an index suitable for sorting the
        the original array even if that array is multidimensional
        """
        a = np.asarray(a)
        ind = list(np.ix_(*[np.arange(d) for d in a.shape]))
        ind[axis] = a.argsort(axis)
        return tuple(ind)
  2. mhvk commented on Jul 17, 2015

    @mhvk
    ContributorAuthor

    @shoyer - yours is definitely easier to read, in that it leaves the proper shaping to np.ix_... also, it doesn't deal with keepdims, right?

  3. shoyer commented on Jul 17, 2015

    @shoyer
    Member

    also, it doesn't deal with keepdims, right?

    I guess it's less general in that it only works for argsort -- I didn't think about min/max, so I didn't need anything for keepdims.

  4. ElieGouzien commented on May 11, 2017

    @ElieGouzien
    Contributor

    I think it would be lot better than today's implementation if the axis=None option returned something equivalent to :

    np.unravel_index(np.argsort(a, axis=None), a.shape)
  5. shoyer commented on May 12, 2017

    @shoyer
    Member

    I think it would be lot better than today's implementation if the axis=None option returned something equivalent to :
    np.unravel_index(np.argsort(a, axis=None), a.shape)

    As a rule, NumPy doesn't change existing behavior. So this will need to be new API of some sort.

  6. ElieGouzien commented on Jun 29, 2017

    @ElieGouzien
    Contributor

    @shoyer Oh that's true.
    I've just submitted a pull request to add my proposition to the doc so that the user can easily find how to retrieve indices.

  7. seberg commented on May 3, 2019

    @seberg
    Member

    I think this issue is now irrelevant, since we have np.take_along_axis instead, which solves the issue. I will however transform it into a Documentation issue, since those functions should list it under the See Also section at least.

  8. changed the title [-]Optional output of full fancy index for argmin, argmax, argort?[/-] [+]DOC: argmin, argmax, argort, etc. should link to `np.take_along_axis` in their documentation[/+] on May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions