Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffling two numpy arrays for a NN

I have two numpy arrays for input data X and output data y.

X = np.array(([2, 3],                    # sample 1 x
              [16, 4]), dtype=float)     # sample 2 x
y = np.array(([1, 0],                    # sample 1 y
              [0, 1]), dtype=float)      # sample 2 y

I am wanting to use mini batches in order to train a NN, how can I shuffle both arrays knowing that the corresponding output is still aligned?

like image 408
Wizard Avatar asked Oct 19 '25 14:10

Wizard


1 Answers

You can have an array of indexes with same shape as the respective arrays and each time shuffle the index array. In that case you can use the shuffled indexes to realign both arrays in a same way.

In [122]: indices = np.indices((2, 2))

In [125]: np.random.shuffle(indices)

In [126]: indices
Out[126]: 
array([[[0, 0],
        [1, 1]],

       [[0, 1],
        [0, 1]]])

In [127]: x[indices[0], indices[1]]
Out[127]: 
array([[ 2.,  3.],
       [16.,  4.]])

In [128]: y[indices[0], indices[1]]
Out[128]: 
array([[1., 0.],
       [0., 1.]])
like image 144
Mazdak Avatar answered Oct 22 '25 04:10

Mazdak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!