What's the easiest way to shuffle an array with python?
What's the easiest way to shuffle an array with python?
You can sort your array with random key
sorted(array, key = lambda x: random.random())
key only be read once so comparing item during sort still efficient.
but look like random.shuffle(array) will be faster since it written in C
this is O(log(N)) btw
# arr = numpy array to shuffle
def shuffle(arr):
a = numpy.arange(len(arr))
b = numpy.empty(1)
for i in range(len(arr)):
sel = numpy.random.random_integers(0, high=len(a)-1, size=1)
b = numpy.append(b, a[sel])
a = numpy.delete(a, sel)
b = b[1:].astype(int)
return arr[b]
Be aware that random.shuffle() should not be used on multi-dimensional arrays as it causes repetitions.
Imagine you want to shuffle an array along its first dimension, we can create the following test example,
import numpy as np
x = np.zeros((10, 2, 3))
for i in range(10):
x[i, ...] = i*np.ones((2,3))
so that along the first axis, the i-th element corresponds to a 2x3 matrix where all the elements are equal to i.
If we use the correct shuffle function for multi-dimensional arrays, i.e. np.random.shuffle(x), the array will be shuffled along the first axis as desired. However, using random.shuffle(x) will cause repetitions. You can check this by running len(np.unique(x)) after shuffling which gives you 10 (as expected) with np.random.shuffle() but only around 5 when using random.shuffle().