sklearn.utils.shuffle-训练数据打乱的最佳方法

时间:2021-11-11 07:21:34

  在进行模型训练前,我们要将数据打乱,以获得更好的训练效果。可以使用sklearn.utils中的shuffle,获得打乱后的数据索引,最后,迭代生成打乱后的batch数据,一个写好的模块如下。

  思路是:1.先shuffle  2.再迭代生成

 def fill_feed_dict(data_X, data_Y, batch_size):
"""Generator to yield batches"""
# Shuffle data first.
shuffled_X, shuffled_Y = shuffle(data_X, data_Y)
# print("before shuffle: ", data_Y[:10])
# print(data_X.shape[0])
# perm = np.random.permutation(data_X.shape[0])
# data_X = data_X[perm]
# shuffled_Y = data_Y[perm]
# print("after shuffle: ", shuffled_Y[:10])
for idx in range(data_X.shape[0] // batch_size):
x_batch = shuffled_X[batch_size * idx: batch_size * (idx + 1)]
y_batch = shuffled_Y[batch_size * idx: batch_size * (idx + 1)]
yield x_batch, y_batch