对numpy.array重新排列:
- numpy.random.shuffle(x):修改本身,打乱顺序
import numpy as np
arr = np.array(range(0, 21, 2))
np.random.shuffle(arr)
arr #打乱顺序后的数组, 如[2, 6, 4, 8, 12, 16, 0, 18, 10, 14, 20]
arr = np.array(range(12)).reshape(3, 4)
np.random.shuffle(arr)
arr # 默认对第一维打乱,[[3, 4, 5], [0, 1, 2], [9, 10, 11], [6, 7, 8]]
- numpy.random.permutation(x):返回一个随机排列
arr = np.array(range(0, 21, 2))
r = np.random.permutation(arr)
r #打乱的索引序列,如[2, 6, 4, 8, 12, 16, 0, 18, 10, 14, 20]
arr = np.array(range(12)).reshape(3, 4)
r = np.random.permutation(arr)
arr # 默认对第一维打乱,如[[3, 4, 5], [0, 1, 2], [9, 10, 11], [6, 7, 8]]
对训练集打乱
# train_X:3列, train_y
per = np.random.permutation(train_X.shape[0]) #打乱后的行号
new_train_X = train_X[per, :, :] #获取打乱后的训练数据
new_train_y = trainy[per]