import sys
import numpy as np
from numpy.random import shuffle
print 'Numpy version:', np.__version__
print 'Python version:', sys.version
print 'Platform:', sys.platform
Numpy version: 1.9.0.dev-573b3b0 Python version: 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] Platform: linux2
Shuffle is supposed to simply move elements around, and that's what it does for a simple array:
a = np.arange(5)
for i in range(5):
print a
shuffle(a)
[0 1 2 3 4] [1 3 4 0 2] [1 4 2 3 0] [2 3 0 1 4] [1 0 4 3 2]
But with a structured one, it is misbehaving. Let's first construct an array whose dtype has a character and an int:
b = np.zeros(5, dtype=[('n', 'S1'), ('i', int)])
b['i'] = range(5)
print b
print b.dtype
[('', 0) ('', 1) ('', 2) ('', 3) ('', 4)] [('n', 'S1'), ('i', '<i8')]
And now we shuffle it a few times:
for i in range(5):
print b
shuffle(b)
[('', 0) ('', 1) ('', 2) ('', 3) ('', 4)] [('', 0) ('', 1) ('', 0) ('', 1) ('', 0)] [('', 0) ('', 1) ('', 0) ('', 1) ('', 0)] [('', 0) ('', 0) ('', 0) ('', 0) ('', 1)] [('', 0) ('', 0) ('', 0) ('', 0) ('', 0)]
Somehow, shuffle is propagating the first element to the whole array...