What's the easiest way to shuffle an array with python?
11 Answers
Alternative way to do this using sklearn
from sklearn.utils import shuffle
X=[1,2,3]
y = ['one', 'two', 'three']
X, y = shuffle(X, y, random_state=0)
print(X)
print(y)
Output:
[2, 1, 3]
['two', 'one', 'three']
Advantage: You can random multiple arrays simultaneously without disrupting the mapping. And 'random_state' can control the shuffling for reproducible behavior.
The other answers are the easiest, however it's a bit annoying that the random.shuffle
method doesn't actually return anything - it just sorts the given list. If you want to chain calls or just be able to declare a shuffled array in one line you can do:
import random
def my_shuffle(array):
random.shuffle(array)
return array
Then you can do lines like:
for suit in my_shuffle(['hearts', 'spades', 'clubs', 'diamonds']):
When dealing with regular Python lists, random.shuffle()
will do the job just as the previous answers show.
But when it come to ndarray
(numpy.array
), random.shuffle
seems to break the original ndarray
. Here is an example:
import random
import numpy as np
import numpy.random
a = np.array([1,2,3,4,5,6])
a.shape = (3,2)
print a
random.shuffle(a) # a will definitely be destroyed
print a
Just use: np.random.shuffle(a)
Like random.shuffle
, np.random.shuffle
shuffles the array in-place.
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()
.
new_array = random.sample( array, len(array) )
. – Charlie Parker