numpy中的隨機(jī)打亂數(shù)據(jù)方法np.random.shuffle解讀
numpy隨機(jī)打亂數(shù)據(jù)方法np.random.shuffle
import numpy as np #實(shí)驗(yàn)可得每次shuffle后數(shù)據(jù)都被打亂,這個(gè)方法可以在機(jī)器學(xué)習(xí)訓(xùn)練 #的時(shí)候在每個(gè)epoch結(jié)束后將數(shù)據(jù)重新洗牌進(jìn)入下一個(gè)epoch的學(xué)習(xí) num = np.arange(20) print(num) np.random.shuffle(num) print(num) num1 = np.arange(20) print(num1) np.random.shuffle(num1) print(num1) np.random.shuffle(num1) print(num1)
#打印輸出:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 1 5 19 9 14 2 12 3 6 18 4 8 16 0 10 17 13 7 15 11]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 2 4 13 14 11 17 9 19 5 12 15 7 18 16 3 10 1 8 0 6]
[ 8 11 13 6 19 7 9 12 4 3 10 14 15 2 1 0 17 18 16 5]
numpy隨機(jī)生成數(shù)據(jù)問題
用numpy.random模塊來生成隨機(jī)數(shù)組
1.np.random.rand 用于生成[0.0, 1.0)之間的隨機(jī)浮點(diǎn)數(shù), 當(dāng)沒有參數(shù)時(shí),返回一個(gè)隨機(jī)浮點(diǎn)數(shù),當(dāng)有一個(gè)參數(shù)時(shí),返回該參數(shù)長度大小的一維隨機(jī)浮點(diǎn)數(shù)數(shù)組,參數(shù)建議是整數(shù)型。
import numpy as np np.random.rand(8)
output [ 0.55958421 0.97358761 0.77753246 0.28072869 0.18467794 0.85755336
0.03976048 0.08161713]
2、np.random.randn這個(gè)函數(shù)返回一個(gè)樣本,具有標(biāo)準(zhǔn)正態(tài)分布。
np.random.randn(8)
output [ 0.5512808 1.32780895 -0.95738756 0.93710414 -2.0854875 -0.5100787 n 0.40982079 -1.235186 ]
3、np.random.randint(low[, high, size]) 返回隨機(jī)的整數(shù),位于半開區(qū)間 [low, high)。
np.random.randint(10,size=10)
output [3 3 8 7 3 2 6 2 3 6]
4、random_integers(low[, high, size]) 返回隨機(jī)的整數(shù),位于閉區(qū)間 [low, high]。
np.random.random_integers(5)
output = 4
5、 np.random.shuffle(x) 類似洗牌,打亂順序;np.random.permutation(x)返回一個(gè)隨機(jī)排列.
arr = np.arange(10) np.random.shuffle(arr) print(arr) np.random.permutation(10)
output[1 7 5 2 9 4 3 6 0 8 ]
array([1,7,4,3,0,9,2,5,8,6])
用random模塊自己構(gòu)造
1、random.randint(low, hight) -> 返回一個(gè)位于[low,hight]之間的整數(shù)。
該函數(shù)接受兩個(gè)參數(shù),這兩個(gè)參數(shù)必須是整數(shù)(或者小數(shù)位是0的浮點(diǎn)數(shù)),并且第一個(gè)參數(shù)必須不大于第二個(gè)參數(shù)
import random random.randint(1,10) random.randint(1.0,10.0)
2、random.random() -> 不接受參數(shù),返回一個(gè)[0.0, 1.0)之間的浮點(diǎn)數(shù)
random.random()
3、random.randrange(start, stop, step) -> 返回以start開始,stop結(jié)束,step為步長的列表中的隨機(jī)整數(shù),同樣,三個(gè)參數(shù)均為整數(shù)(或者小數(shù)位為0),若start大于stop時(shí) ,setp必須為負(fù)數(shù).step不能是0
random.randrange(1,100,2) ?#返回[1,100]之間的奇數(shù) random.randrange(100,1,-2) ?#返回[100,1]之間的偶數(shù)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python多進(jìn)程并發(fā)與同步機(jī)制超詳細(xì)講解
進(jìn)程(Process),顧名思義,就是進(jìn)行中的程序。有一句話說得好:程序是一個(gè)沒有生命的實(shí)體,只有處理器賦予程序生命時(shí),它才能成為一個(gè)活動的實(shí)體。進(jìn)程是資源分配的最小單元,也就是說每個(gè)進(jìn)程都有其單獨(dú)的內(nèi)存空間2022-12-12利用python操作SQLite數(shù)據(jù)庫及文件操作詳解
這篇文章主要給大家介紹了關(guān)于利用python操作SQLite數(shù)據(jù)庫及文件操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09Python數(shù)據(jù)類型之Set集合實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Set集合,結(jié)合實(shí)例形式詳細(xì)分析了Python數(shù)據(jù)類型中集合的概念、原理、創(chuàng)建、遍歷、交集、并集等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05