Python 中常见数据集打乱 方法 | 您所在的位置:网站首页 › 如何把数据打乱顺序显示 › Python 中常见数据集打乱 方法 |
第一种方法: 通过index x_train, y_train=train_load() index = [i for i in range(len(x_train))] np.random.shuffle(index) x_train= x_train[index] y_train = y_train[index] 第二种方法.zip()+shuffle()方法 x_train, y_train=train_load() result = list(zip(x_train, y_train)) # 打乱的索引序列 np.random.shuffle(result) x_train,y_train = zip(*result)第三种方法:seed()+shuffle x_batch, y_batch = train_load() #加载我所有的数据,这里想x_batch,Y_batch是list的格式,要注意 seed=100 random.seed(seed) random.shuffle(x_batch) random.seed(seed)#一定得重复在写一遍,和上面的seed要相同,不然y_batch和x_batch打乱顺序会不一样 random.shuffle(y_batch) PS:numpy中函数shuffle与permutation都是对原来的数组随机打乱原来的顺序,shuffle中文含义为洗牌,permutation中文含义为排列,区别在于shuffle直接在原来的数组上进行操作,改变原来数组的顺序,无返回值。而permutation不直接在原来的数组上进行操作,而是返回一个新的打乱顺序的数组,并不改变原来的数组。 |
CopyRight 2018-2019 实验室设备网 版权所有 |