Shuffling two numpy arrays for a NN

北战南征 提交于 2019-12-23 15:44:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!