[Numpy-discussion] fix random.choice for 1.7?

Alan G Isaac alan.isaac at gmail.com
Mon Nov 12 09:50:26 EST 2012

On 11/12/2012 8:59 AM, Sebastian Berg wrote:
> https://github.com/numpy/numpy/blob/master/numpy/random/mtrand/mtrand.pyx#L919
> Sounds like it should be pretty simple to add axis=None which would
> change the current behavior very little, it would stop give an error
> anymore for none 1-d arrays though.

I believe Nathaniel suggested *retaining* this error
until two things were supported: flattening ndarrays,
and choice of subarrays for ndarrays.  If I understand
you, you are suggesting the first is easily supported.
Or are you also suggesting the 2nd is easily supported?

Alan Isaac

PS I'll repost the code (or similar) that Robert Kern
posted when this function was discussed in 2006.
However it did not support multiple samples.

def choice(x, axis=None):
     x = np.asarray(x)
     if axis is None:
         length = np.multiply.reduce(x.shape)
         n = random.randint(length)
         return x.flat[n]
         n = random.randint(x.shape[axis])
         idx = map(slice, x.shape)
         idx[axis] = n
         return x[tuple(idx)]

More information about the NumPy-Discussion mailing list