在玩python學(xué)習(xí)機(jī)器時(shí),對(duì)于那種對(duì)隨機(jī)性不太敏感的模型,理論上說(shuō)可以不打亂。但敏感不敏感也跟數(shù)據(jù)量級(jí),復(fù)雜度,算法內(nèi)部計(jì)算機(jī)制都有關(guān),目前并沒(méi)有一個(gè)經(jīng)緯分明的算法隨機(jī)度敏感度列表。既然打亂數(shù)據(jù)并不會(huì)得到一個(gè)更差的結(jié)果,一般推薦的做法就是打亂全量數(shù)據(jù)。那怎么打亂呢?今天小編就教大家在python中打亂數(shù)據(jù)集和標(biāo)簽,來(lái)看看吧。
方法一、打亂索引Index
importnumpyasnp
index=[iforiinrange(len(test_data))]#test_data為測(cè)試數(shù)據(jù)
np.random.shuffle(index)#打亂索引
test_data=test_data[index]
test_label=test_label[index]
方法二:通過(guò)數(shù)組來(lái)shuffle來(lái)打亂
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]
方法三:通過(guò)隨機(jī)數(shù)打亂
importnumpyasnp
np.random.seed(12)
np.random.shuffle(test_data)
np.random.seed(12)
np.random.shuffle(test_label)
以上內(nèi)容為大家介紹了在python中如何打亂數(shù)據(jù)?希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。