在python中如何打乱数据?

2023-12-13 10:20:34 网络知识 匿名

在玩python学习机器时,对于那种对随机性不太敏感的模型,理论上说可以不打乱。但敏感不敏感也跟数据量级,复杂度,算法内部计算机制都有关,目前并没有一个经纬分明的算法随机度敏感度列表。既然打乱数据并不会得到一个更差的结果,一般推荐的做法就是打乱全量数据。那怎么打乱呢?今天小编就教大家在python中打乱数据集和标签,来看看吧。

方法一、打乱索引Index

importnumpyasnp

index=[iforiinrange(len(test_data))]#test_data为测试数据

np.random.shuffle(index)#打乱索引

test_data=test_data[index]

test_label=test_label[index]

方法二:通过数组来shuffle来打乱

image_list=[]#listofimages

label_list=[]#listoflabels

temp=np.array([image_list,label_list])

temp=temp.transpose()

np.random.shuffle(temp)

images=temp[:,0]#arrayofimages(N,)

labels=temp[:,1]

方法三:通过随机数打乱

importnumpyasnp

np.random.seed(12)

np.random.shuffle(test_data)

np.random.seed(12)

np.random.shuffle(test_label)

以上内容为大家介绍了在python中如何打乱数据?希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:瀚银百科。

发表评论: