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
{{ message }}
Repository navigation
random choice should support multidimensional arrays #2724
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.
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.
Reacted by Ciprian Tomoiaga, Raghavendra Kotikalapudi, Alex Nichol, Tibor Pilz, weibz, Miguel Cortes, Karol Kubicki, aquintero, Carlos Martin, Joop and 2 more
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.
Activity
seberg commentedon 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=Noneand ravel in that case and replace makeaxis=1. Then setpop_size = a.shape[axis]and passaxisintotake. (Plus possibly an error ifais just anintand axis is given).Actually thinking about it, it could also be reasonable to have
size=Noneas 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 toimport random; random.choicemostly.One could think about also allowing an
outargument which is just passed intotakemostly. While none of the other random functions have that it could be a nice performance option in some cases.ENH: Add multidimensional array support to numpy.random.choice
samuela commentedon Nov 23, 2016
+1
jhollowed commentedon Feb 28, 2017
+1. Is this still not implemented?
jbachh commentedon Mar 5, 2018
+1
slidenerd commentedon Apr 22, 2018
Is this implemented?
mikesmith1611 commentedon Apr 30, 2018
Is this ever going to be implemented?
JulianKlug commentedon Jun 19, 2018
+1
ENH: Add axis functionality to random.choice
stephenpardy commentedon Jul 11, 2018
+1
I also tried implementing this, before realizing that @masasin had already done it.
mattip commentedon 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.