[Numpy-discussion] fix random.choice for 1.7?
Alan G Isaac
Mon Nov 12 08:50:26 CST 2012
On 11/12/2012 8:59 AM, Sebastian Berg wrote:
> 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?
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)
n = random.randint(x.shape[axis])
idx = map(slice, x.shape)
idx[axis] = n
More information about the NumPy-Discussion