Numpy: random choice should support multidimensional arrays

Created on 12 Nov 2012  ·  9Comments  ·  Source: numpy/numpy

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.

01 - Enhancement numpy.random

Most helpful comment

+1. Is this still not implemented?

All 9 comments

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.

+1

+1. Is this still not implemented?

+1

Is this implemented?

Is this ever going to be implemented?

+1

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcsaba89 picture dcsaba89  ·  3Comments

Kreol64 picture Kreol64  ·  3Comments

qualiaa picture qualiaa  ·  3Comments

keithbriggs picture keithbriggs  ·  3Comments

dmvianna picture dmvianna  ·  4Comments