numpy.random.permutation(x.shape[0]) 可以用于随机打散训练数据,同时保持训练数据与标签的对齐
numpy.random.permutation(length)用来产生一个随机序列作为索引,再使用这个序列从原来的数据集中按照新的随机顺序产生随机数据集。length 为训练数据的个数
example:
import numpy
data_x = [[1,2,3],
[4,5,6],
[7,8,9]]
data_y = [1,2,3]
data_x = numpy.array(data_x)
data_y = numpy.array(data_y)
indices = numpy.random.permutation(data_x.shape[0])
print(indices)
rand_data_x = data_x[indices]
rand_data_y = data_y[indices]
print(rand_data_x)
print(rand_data_y)
output:
[2 0 1]
[[7 8 9]
[1 2 3]
[4 5 6]]
[3 1 2]