Skip to content
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

random choice should support multidimensional arrays #2724

Closed
alan-isaac opened this issue Nov 12, 2012 · 9 comments
Closed

random choice should support multidimensional arrays #2724

alan-isaac opened this issue Nov 12, 2012 · 9 comments

Comments

@alan-isaac
Copy link

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.

@seberg
Copy link
Member

seberg commented Nov 12, 2012

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.

masasin added a commit to masasin/numpy that referenced this issue Jul 7, 2016
Backwards compatible by adding an `axis` keyword to the end of the
signature for choice. Default is 0, which selects by row. The default
keyword arguments make it functionally equivalent to random.choice from
Python's standard library. Note, however, that a list of tuples will
have the tuples returned as lists.

Issue numpy#2724 was originally opened with that request, but numpy#2727 seems to
have closed it accidentally.
@samuela
Copy link

samuela commented Nov 23, 2016

+1

@jhollowed
Copy link

+1. Is this still not implemented?

@jbachh
Copy link

jbachh commented Mar 5, 2018

+1

@slidenerd
Copy link

Is this implemented?

@mikesmith1611
Copy link

Is this ever going to be implemented?

@JulianKlug
Copy link

+1

stephenpardy added a commit to stephenpardy/numpy that referenced this issue Jul 11, 2018
Added keyword argument axis to the random.choice function that extends its functionality to N-d arrays.
Added additional documentation for this feature and checks to ensure that the sizes match.

Originally suggested in numpy#2724

Additional checks and documentation

Add tests
@stephenpardy
Copy link

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

@mattip
Copy link
Member

mattip commented May 29, 2019

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
Projects
None yet
Development

No branches or pull requests

10 participants