17

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]]

3 Answers 3

20

You can tell np.random.shuffle to act on the flattened version:

>>> a = np.arange(9).reshape((3,3))
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.random.shuffle(a.flat)
>>> a
array([[3, 5, 8],
       [7, 6, 2],
       [1, 4, 0]])
Sign up to request clarification or add additional context in comments.

1 Comment

how to shuffle a matrix so that the correspondences between rows and columns are retained? For example, row and column 1 would be shuffled to row and column 3 in the shuffled matrix
8

You could shuffle a.flat:

>>> np.random.shuffle(a.flat)
>>> a
array([[6, 1, 2],
       [3, 5, 0],
       [7, 8, 4]])

Comments

7

I think this is very important to note.
You can use random.shuffle(a) if a is 1-D numpy array. If it is N-D (where N > 2) than

random.shuffle(a)

will spoil your data and return some random thing. As you can see here:

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]]

This is a known bug (or feature?) of numpy.

So, use only numpy.random.shuffle(a) for numpy arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.