1、完成了數據的組合,shuffle, 拆分,是很有效的數據處理方法。
b = [1, 2, 3, 4, 5, 6 , 7, 8 ,9]
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
c = list(zip(a, b))
print(c)
random.shuffle(c)
print(c)
a, b = zip(*c)
print(a)
print(b)
輸出結果
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7), ('h', 8), ('i', 9)]
[('a', 1), ('g', 7), ('c', 3), ('i', 9), ('h', 8), ('e', 5), ('f', 6), ('d', 4), ('b', 2)]
('a', 'g', 'c', 'i', 'h', 'e', 'f', 'd', 'b')
(1, 7, 3, 9, 8, 5, 6, 4, 2)
2、通過設置相同的隨機種子,再shuffle的方式來實現。代碼如下:
import random
randnum = random.randint(0,100)
random.seed(randnum)
random.shuffle(train_x)
random.seed(randnum)
random.shuffle(train_y)