numpyのrandomモジュールの詳細

6139 ワード

参照先:https://docs.scipy.org/doc/numpy/reference/routines.random.html https://blog.csdn.net/akadiao/article/details/78252840?locationNum=9&fps=1 https://blog.csdn.net/kancy110/article/details/69665164
####1、Simple random data
関数#カンスウ#
説明する
rand(d0, d1, …, dn)
Random values in a given shape.
randn(d0, d1, …, dn)
Return a sample (or samples) from the “standard normal” distribution.
randint(low[, high, size, dtype])
Return random integers from low (inclusive) to high (exclusive).
random_integers(low[, high, size])
Random integers of type np.int between low and high, inclusive.
random_sample([size])
Return random floats in the half-open interval [0.0, 1.0).
random([size])
Return random floats in the half-open interval [0.0, 1.0).
ranf([size])
Return random floats in the half-open interval [0.0, 1.0).
sample([size])
Return random floats in the half-open interval [0.0, 1.0).
choice(a[, size, replace, p])
Generates a random sample from a given 1-D array
bytes(length)
Return random bytes.
#####1.1、numpy.random.randomリファレンス:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html#numpy.random.random
機能:半開区間[0.0,1.0)の範囲の浮動小数点数を返す
numpy.random.random(size=None):         [0.0, 1.0)      

random_sample、random、ranf、sampleは同じ使い方です.
random_sample_type = type(np.random.random_sample())
print(random_sample_type)

random_sample = np.random.random_sample((5))# random_sample(5,)  
print(random_sample)

random_sample = np.random.random_sample((5,))
print(random_sample)

random_sample = np.random.random_sample((3,2))
print(random_sample)

# Three-by-two array of random numbers from [-5, 0):
random_sample_result = 5 * np.random.random_sample((3, 2)) - 5
print(random_sample_result)

印刷:


[ 0.58452144  0.17618506  0.95080302  0.66095854  0.34928887]

[ 0.39012758  0.27384807  0.30607608  0.46398196  0.88590116]

[[ 0.12028886  0.57902902]
 [ 0.87015091  0.1462187 ]
 [ 0.43734193  0.09571964]]
 
[[-4.94102089 -1.79261502]
 [-4.34365906 -3.16519113]
 [-1.75801587 -1.88706362]]


#####1.2、numpy.random.rand生成ランダム浮動小数点数参照:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand
機能:半開区間[0.0,1.0)の範囲の浮動小数点数を返す
numpy.random.rand(d0, d1, ..., dn)

デフォルトでは、[0,1]の範囲のランダムな浮動小数点数を生成します.パラメータsize設定でデータを返すsizeを設定することもできます.
とnumpy.random.randomの違いは:numpy.random.randomが受信したメタグループnumpy.random.randいいえ
random_rand_type = type(np.random.rand())
print(random_rand_type)

random_rand = np.random.rand(5)# random_rand(5,)  
print(random_rand)

random_rand = np.random.rand(5,)
print(random_rand)

random_rand = np.random.rand(3,2)
print(random_rand)

# Three-by-two array of random numbers from [-5, 0):
random_rand_result = 5 * np.random.rand(3, 2) - 5
print(random_rand_result)

印刷:


[ 0.26487271  0.0281932   0.8042671   0.2643821   0.33199909]

[ 0.91690691  0.3412498   0.11569359  0.53687716  0.19945599]

[[ 0.09106275  0.64573293]
 [ 0.04541494  0.04964684]
 [ 0.24620085  0.81908924]]
 
[[-1.70500552 -1.19790261]
 [-2.56920771 -1.32168807]
 [-4.77324942 -2.59249556]]

#####1.3、numpy.random.randintはランダム整数参照を生成します.https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html#numpy.random.randint
numpy.random.randint(low, high=None, size=None, dtype='l')


1、ランダムに[low,high]の範囲内の整数を生成する
random_randint = np.random.randint(3, 6, size=10)
print(random_randint)

印刷:
[3 4 4 3 4 3 4 5 5 3]#  3    , 6     

2、size sizeを指定する:int型またはint型のメタグループで、デフォルトはNoneが単一の整数を返す
random_randint = np.random.randint(3, 10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 5))
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 2, 5))
print(random_randint)


印刷:
9

[4 3 3 4 5 5 5 6 6 7]

[[3 3 4 3 8]
 [7 6 4 6 9]]
 
[[[6 3 6 3 5]
  [5 5 5 5 7]]
 [[9 5 8 8 4]
  [7 8 3 9 6]]]

3、highがNoneの場合、結果の範囲は[0,low).
random_randint = np.random.randint(3)
print(random_randint)

random_randint = np.random.randint(3,size=10)
print(random_randint)

印刷:
2
[2 2 0 0 1 0 1 0 0 1]

#####1.4、numpy.random.random_integersリファレンス:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_integers.html#numpy.random.random_integers
numpy.random.random_integers(low, high=None, size=None)

整数またはN次元の整数配列を生成します.値範囲:highがNoneでない場合は、[low,high]間のランダム整数を取り、そうでない場合は[1,low]間のランダム整数を取ります.
使い方とrandom.randintは同様である、異なるのは取値区間が閉区間[low,high]であり、highがNoneの場合の取値範囲が[1,low]である.
原文:similar to randint,only for the closed interval[low,high],and 1 is the lowest value if high is omitted.In particular, this other one is the one to use to generate uniformly distributed discrete non-integers.
random_integers = np.random.random_integers(3, 6, size=10)
print(random_integers)

random_integers = np.random.random_integers(3,size=10)
print(random_integers)

印刷:
[4 4 6 3 6 6 3 3 6 4]
[2 1 2 2 2 3 3 2 1 2]

#####1.5、numpy.random.randn numpy.random.randn(d 0,d 1,...,dn):浮動小数点数またはN次元浮動小数点配列を生成し、取数範囲:正規分布のランダムサンプル数.
randn = np.random.randn()
print(randn)

印刷:
1.387157144507402

使用式:s i g m a∗n p.r a n d o m . r a n d n ( . . . ) + m u sigma * np.random.randn(...) + mu sigma∗np.random.randn(...)+mu$N(mu,sigma^2)$からサンプルを取得
#Two-by-four array of samples from N(3, 6.25):
randn = 2.5 * np.random.randn(2, 4) + 3
print(randn)

印刷:
[[ 6.03858058  4.47342334 -1.37679171  2.20495446]
 [ 5.70048472  1.28674501 -1.06387771  3.38788724]]