改组NN的两个numpy数组 [英] Shuffling two numpy arrays for a NN

查看:112
本文介绍了改组NN的两个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用于输入数据X和输出数据y的numpy数组.

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.]])

这篇关于改组NN的两个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆