问题
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?
回答1:
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.]])
回答2:
It's easy with sklearn:
X, null, y, null = train_test_split(X, y, test_size=0, random_state=42)
It keep aligned X and y
来源:https://stackoverflow.com/questions/50415972/shuffling-two-numpy-arrays-for-a-nn