-1

currently im facing a problem regarding the permutation of 2 numpy arrays of different row sizes, i know how to to utilize the np.random.shuffle function but i cannot seem to find a solution to my specific problem, the examples from the numpy documentation only refers to nd arrays with the same row sizes, e.g x.shape=[10][784] y.shape=[10][784]

I want to permute/random shuffle the column values in a consistent order for both arrays with those shapes:x.shape=[60000][784], y.shape=[10000][784].

e.g.
x[59000] = [0,1,2,3,4,5,6,7,8,9] y[9999] = [0,1,2,3,4,5,6,7,8,9]

After the permutation, both of them should be shuffled in the same consistent way e.g.

x[59000] = [3,0,1,6,7,2,9,8,4,5] y[9999] = [3,0,1,6,7,2,9,8,4,5]

The shuffle order needs to be consistent over the two arrays which have different row sizes. I seem to get a ValueError: Found input variables with inconsistent numbers of samples: [60000, 10000]" Any ideas on how to fix this issue? Really appreciate any help!

A. K.
  • 31
  • 4
  • Possible duplicate of [Numpy shuffle multidimensional array by row only, keep column order unchanged](http://stackoverflow.com/questions/35646908/numpy-shuffle-multidimensional-array-by-row-only-keep-column-order-unchanged) – Arya McCarthy Apr 07 '17 at 16:17

2 Answers2

1

Stick the arrays together and permute the combined array:

merged = numpy.concatenate([x, y])
numpy.shuffle(merged.T)
x, y = numpy.split(merged, [x.shape[0]])
user2357112
  • 260,549
  • 28
  • 431
  • 505
0

Check also old threads Better way to shuffle two numpy arrays in unison

Or compute a permutation ahead

your_permutation = np.shuffle(np.array([0, 1, 2, 3, 4, 5]))
i = np.argsort(your_permutation)
x = x[i]
y = y[i]
Community
  • 1
  • 1
Serge
  • 3,387
  • 3
  • 16
  • 34