[NumPy-Tickets] [NumPy] #2246: random.choice: ndarrays

NumPy Trac numpy-tickets@scipy....
Fri Nov 9 12:23:39 CST 2012

#2246: random.choice: ndarrays
 Reporter:  aisaac       |       Owner:  somebody   
     Type:  enhancement  |      Status:  new        
 Priority:  normal       |   Milestone:  Unscheduled
Component:  numpy.core   |     Version:  devel      
 Keywords:               |  
 NumPy 1.7 is scheduled to add a random.choice function.
 However, it does not provide for array flattening,
 and it does not provide for subarray choice.  I think
 these are important to add.  Until they are added,
 random.choice should raise an exception for ndarrays.

 Back in 2006 (?) Robert Kern suggested something like this:

 def choice(x, axis=None):
     """Select an element or subarray uniformly randomly.
     If axis is None, then a single element is chosen from the entire
     Otherwise, a subarray is chosen from the given axis.
     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)]

Ticket URL: <http://projects.scipy.org/numpy/ticket/2246>
NumPy <http://projects.scipy.org/numpy>
My example project

More information about the NumPy-Tickets mailing list