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?
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.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With