train_data 和 train_label保持对应
state = np.random.get_state()
np.random.shuffle(train_data)
np.random.set_state(state)
np.random.shuffle(train_label)
原理
numpy.random.
set_state
(state)
Set the internal state of the generator from a tuple.
import numpy as np
state = np.random.get_state()
chance = np.random.permutation(20)
np.random.set_state(state)
chance2 = np.random.permutation(20)
print(chance,chance2)
输出
[13 17 7 1 4 12 15 8 3 16 5 6 11 18 2 14 9 10 0 19] [13 17 7 1 4 12 15 8 3 16 5 6 11 18 2 14 9 10 0 19]
方法二
import numpy as np
train_label = np.arange(20)
train_data = np.arange(200).reshape(20,-1)
index = np.random.permutation(train_label.shape[0])#记下第一维度的打乱顺序
shuffled_data = train_data[index]#按照顺序索引
shuffled_labels = train_label[index]
print('index:',index)
('index:', array([10, 14, 13, 17, 12, 18, 5, 19, 8, 1, 16, 9, 7, 2, 15, 4, 3,
0, 11, 6]))