Select cells randomly from NumPy array - without replacement Select cells randomly from NumPy array - without replacement numpy numpy

Select cells randomly from NumPy array - without replacement


How about using numpy.random.shuffle or numpy.random.permutation if you still need the original array?

If you need to change the array in-place than you can create an index array like this:

your_array = <some numpy array>index_array = numpy.arange(your_array.size)numpy.random.shuffle(index_array)print your_array[index_array[:10]]


All of these answers seemed a little convoluted to me.

I'm assuming that you have a multi-dimensional array from which you want to generate an exhaustive list of indices. You'd like these indices shuffled so you can then access each of the array elements in a randomly order.

The following code will do this in a simple and straight-forward manner:

#!/usr/bin/pythonimport numpy as np#Define a two-dimensional array#Use any number of dimensions, and dimensions of any sized=numpy.zeros(30).reshape((5,6))#Get a list of indices for an array of this shapeindices=list(np.ndindex(d.shape))#Shuffle the indices in-placenp.random.shuffle(indices)#Access array elements using the indices to do cool stufffor i in indices:  d[i]=5print d

Printing d verified that all elements have been accessed.

Note that the array can have any number of dimensions and that the dimensions can be of any size.

The only downside to this approach is that if d is large, then indices may become pretty sizable. Therefore, it would be nice to have a generator. Sadly, I can't think of how to build a shuffled iterator off-handedly.


Extending the nice answer from @WoLpH

For a 2D array I think it will depend on what you want or need to know about the indices.

You could do something like this:

data = np.arange(25).reshape((5,5))x, y  = np.where( a = a)idx = zip(x,y)np.random.shuffle(idx)

OR

data = np.arange(25).reshape((5,5))grid = np.indices(data.shape)idx = zip( grid[0].ravel(), grid[1].ravel() )np.random.shuffle(idx)

You can then use the list idx to iterate over randomly ordered 2D array indices as you wish, and to get the values at that index out of the data which remains unchanged.

Note: You could also generate the randomly ordered indices via itertools.product too, in case you are more comfortable with this set of tools.