在玩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培訓機構:千鋒教育。