numpy.random 的函數基本用法

import numpy as np
”’

1)rand(d0, d1, …, dn)

Create an array of the given shape and populate(居住於) it with
random samples from a uniform distribution
over [0, 1). 0~1的均勻分佈

        Parameters
        ----------
        d0, d1, ..., dn : int, optional
            The dimensions of the returned array, should all be positive.
            If no argument is given a single Python float is returned.

        Returns
        -------
        out : ndarray, shape ``(d0, d1, ..., dn)``
        Random values.

”’
rand=np.random.rand(3,2)
print(“rand:”,rand)
”’

2)np.random.randn(d0, d1, …, dn) 0~1 標準正態分佈 standard normal

”’
randn=np.random.randn(3,2)
print(“randn:”,randn)

”’

3)randint(low[,high,shape]) []中爲可選參數

”’
randint=np.random.randint(100,200,(2,3))
”’

4)

shuffle [ˈʃʌfl]亂序 shuffle(x) 改變數組x本身
permutation 置換順序 permutation(x) 不改變數組x

根據數組最外維進行隨機排列,內部的順序不變
”’
x=np.arange(20,40,step=1,dtype=int).reshape(4,5)
np.random.shuffle(x)
print(“shuffle_x:”,x)
”’

5)

choice(x[,size,replace,p])
從一維數組x中抽取元素,每個元素的抽取概率爲p(p爲與x同shape的概率數組)中的對應概率,形成size形狀的新數組
replace 默認爲false 不可重用元素
”’
x.resize((20,))
print(“x_resize:”,x)
print(“choice:”,np.random.choice(x,size=(3,4),p=x/np.sum(x)))

6)均勻分佈

print(“np.random.uniform(20,40,(2,4)):”,np.random.uniform(20,40,(2,4)))

7)正態分佈(均值,方差,shape)

print(“np.random.normal(0,1,(3,4)):”,np.random.normal(0,1,(3,4)))

8)泊松分佈

print(“np.random.poisson(0.3,(2,4)):”,np.random.poisson(0.3,(2,4)))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章