[Numpy-discussion] numpy.random.shuffle
Alan G Isaac
aisaac at american.edu
Wed Nov 22 09:50:11 CST 2006
On Wed, 22 Nov 2006, Robert apparently wrote:
>>>> numpy.random.shuffle(rr)
The docstring is incomplete. From the NumPy Book:
shuffle (sequence)
Randomly permute the items of any sequence. If sequence is an array, then it
must be 1-d.
>>> rr=N.array(zip(range(20),range(20)))
>>> N.random.shuffle(rr.flat)
>>> rr
array([[ 0, 16],
[10, 1],
[19, 13],
[18, 14],
[13, 11],
[12, 4],
[19, 9],
[18, 16],
[14, 7],
[ 5, 17],
[ 2, 3],
[ 9, 7],
[12, 3],
[ 1, 4],
[ 8, 17],
[ 6, 6],
[11, 0],
[ 5, 15],
[10, 8],
[ 2, 15]])
What surprised me more is that you cannot shuffle the rows
with Python's random.shuffle either: the exact same problem
you noted crops up! (Works fine with a list of tuples,
of course.) But you could do:
>>> rr=N.array(zip(range(20),range(20)))
>>> pid = N.random.permutation(rr.shape[0])
>>> rr=rr[pid]
>>> rr
array([[ 9, 9],
[ 2, 2],
[ 3, 3],
[15, 15],
[ 4, 4],
[11, 11],
[18, 18],
[17, 17],
[ 5, 5],
[12, 12],
[13, 13],
[ 1, 1],
[ 8, 8],
[14, 14],
[16, 16],
[19, 19],
[ 7, 7],
[ 6, 6],
[10, 10],
[ 0, 0]])
hth,
Alan Isaac
More information about the Numpy-discussion
mailing list