日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python中的常見數據集打亂方法_python

作者:starky0729 ? 更新時間: 2023-04-02 編程語言

python常見的數據集打亂方法

第一種方法

通過index?

x_train, y_train=train_load()

index = [i for i in range(len(x_train))]

np.random.shuffle(index)

x_train= x_train[index]

y_train = y_train[index]

第二種方法

zip()+shuffle()方法

x_train, y_train=train_load()
result = list(zip(x_train, y_train)) ?# 打亂的索引序列
np.random.shuffle(result)
x_train,y_train = zip(*result)

第三種方法

seed()+shuffle

x_batch, y_batch = train_load()
#加載我所有的數據,這里想x_batch,Y_batch是list的格式,要注意

seed=100
random.seed(seed)
random.shuffle(x_batch)
random.seed(seed)#一定得重復在寫一遍,和上面的seed要相同,不然y_batch和x_batch打亂順序會不一樣
random.shuffle(y_batch)

PS:numpy中函數shuffle與permutation都是對原來的數組隨機打亂原來的順序,shuffle中文含義為洗牌,permutation中文含義為排列,區別在于shuffle直接在原來的數組上進行操作,改變原來數組的順序,無返回值。

而permutation不直接在原來的數組上進行操作,而是返回一個新的打亂順序的數組,并不改變原來的數組。

python手動打亂數據集

x_train, y_train = np.array(x_train),np.array(y_train)
index = [i for i in range(len(y_train))]
np.random.shuffle(index)
x_train = x_train[index]
y_train = y_train[index]

總結

原文鏈接:https://blog.csdn.net/weixin_40964777/article/details/100050263

欄目分類
最近更新