码迷,mamicode.com
首页 > 其他好文 > 详细

numpy利用下标打乱数据集

时间:2019-09-03 13:42:40      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:code   pre   shuff   python   index   rand   led   print   int   

利用下标打乱数据集

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]]

numpy利用下标打乱数据集

标签:code   pre   shuff   python   index   rand   led   print   int   

原文地址:https://www.cnblogs.com/redo19990701/p/11452369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!