Skip to content

random choice should support multidimensional arrays #2724

Closed

Description

@alan-isaac

random.choice enhancement request:

random.choice should support multidimensional arrays by taking an axis number. With this enhancement, axis=None would choose from a flattened array, while an integer argument would chose from the subarrays along that axis.

Apologies if this is a duplicate. My previous submission seems to have vanished.

Activity

  1. seberg commented on Nov 12, 2012

    @seberg
    Member

    This should be simple if you are thinking about doing it. All that seems necessary is to add a special check for axis=None and ravel in that case and replace make axis=1. Then set pop_size = a.shape[axis] and pass axis into take. (Plus possibly an error if a is just an int and axis is given).

    Actually thinking about it, it could also be reasonable to have size=None as default and have it return a scalar/the given axes removed in that case. That would be a real change in functionality unfortunately, but it would make sense for similarity to import random; random.choice mostly.

    One could think about also allowing an out argument which is just passed into take mostly. While none of the other random functions have that it could be a nice performance option in some cases.

  2. samuela commented on Nov 23, 2016

    @samuela

    +1

  3. jhollowed commented on Feb 28, 2017

    @jhollowed

    +1. Is this still not implemented?

  4. jbachh commented on Mar 5, 2018

    @jbachh

    +1

  5. slidenerd commented on Apr 22, 2018

    @slidenerd

    Is this implemented?

  6. mikesmith1611 commented on Apr 30, 2018

    @mikesmith1611

    Is this ever going to be implemented?

  7. JulianKlug commented on Jun 19, 2018

    @JulianKlug

    +1

  8. stephenpardy commented on Jul 11, 2018

    @stephenpardy

    +1
    I also tried implementing this, before realizing that @masasin had already done it.

  9. mattip commented on May 29, 2019

    @mattip
    Member

    Implemented in the new randomgen API: np.random.Generator().choice(..., axis=int). Closing since we will not be updating the legacy code to support this. If you feel differently, please reopen for discussion.

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