numpy利用下标打乱数据集

利用下标打乱数据集

import numpy as np
#导入numpy库
def shuffleData(data):
    index=np.arange(len(data))
    #生成一个数据集行数大小的顺序数组[0,1,...,len(data)](顺序下标)
    np.random.shuffle(index)
    #打乱顺序下标
    data=data[index]
    #按照乱序下标重新排列数据集
    return data
data=np.arange(18)
data.resize((6,3))
#生成一个4*2的数据集
print("orign:\n",data)
data=shuffleData(data)
print("Shuffled:\n",data)
#输出:
# orign:
#  [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]
#  [12 13 14]
#  [15 16 17]]
# Shuffled:
#  [[ 0  1  2]
#  [15 16 17]
#  [ 3  4  5]
#  [ 9 10 11]
#  [ 6  7  8]
#  [12 13 14]]

猜你喜欢

转载自www.cnblogs.com/redo19990701/p/11452369.html