I have a 2-d numpy array that I would like to shuffle. Is the best way to reshape it to 1-d, shuffle and reshape again to 2-d or is it possible to shuffle without reshaping?
just using the random.shuffle doesn't yield expected results and numpy.random.shuffle shuffles only rows:
import random
import numpy as np
a=np.arange(9).reshape((3,3))
random.shuffle(a)
print a
[[0 1 2]
[3 4 5]
[3 4 5]]
a=np.arange(9).reshape((3,3))
np.random.shuffle(a)
print a
[[6 7 8]
[3 4 5]
[0 1 2]]
You can tell
np.random.shuffle
to act on the flattened version:You could shuffle
a.flat
:I think this is very important to note.
You can use
random.shuffle(a)
ifa
is 1-D numpy array. If it is N-D (where N > 2) thanwill spoil your data and return some random thing. As you can see here:
This is a known bug (or feature?) of numpy.
So, use only
numpy.random.shuffle(a)
for numpy arrays.