Reputation: 2837
Suppose I have an mXd
matrix called X
, and an mX1
array called Y
(using numpy
). The rows of X
correspond to the rows of Y
.
Now suppose I need to shuffle the data (the rows) in X
. I used:
random.shuffle(X)
Is there a way for me to keep track of the way X
has been shuffled, so I could shuffle Y
accordingly?
Thank you :)
Upvotes: 0
Views: 771
Reputation: 22882
You can use numpy.random.permutation
to create a permuted list of indices
, and then shuffle both X
and Y
using those indices
:
>>> import numpy
>>> m = 10
>>> X = numpy.random.rand(m, m)
>>> Y = numpy.random.rand(m)
>>> indices = numpy.random.permutation(m)
>>> indices
array([4, 7, 6, 9, 0, 3, 1, 2, 8, 5])
>>> Y
array([ 0.53867012, 0.6700051 , 0.06199551, 0.51248468, 0.4990566 ,
0.81435935, 0.16030748, 0.96252029, 0.44897724, 0.98062564])
>>> Y = Y[indices]
>>> Y
array([ 0.4990566 , 0.96252029, 0.16030748, 0.98062564, 0.53867012,
0.51248468, 0.6700051 , 0.06199551, 0.44897724, 0.81435935])
>>> X = X[indices, :]
Upvotes: 2