NumPy:【np.random】のあれこれ(実行結果で説明)
目次
サマリ
- .bytes
- バイト型ランダム値
- .random
- .random_sampleと同じ
- .permutation
- 重複なしランダム値
- .shuffle
- ndarrayの並び順シャッフル
- .choice
- ndarrayから抽出
- 重複OK、重複なし
- 出現確率指定、出現率
- .rand
- 0~1ランダム値
- .randn
- 標準正規分布に従ったランダム値
- .normal
- 正規分布に従ったランダム値
- .random_sample
- 0~1ランダム値
- .randint
- 範囲指定・整数値ランダム値
- .seed
- 乱数固定
はじめに
前提
- np.frombuffer
- np.arange
ドキュメント
import numpy as np
np.random?
Type: module String form:File: ~/miniconda3/lib/python3.9/site-packages/numpy/random/__init__.py Docstring: ======================== Random Number Generation ======================== Use ``default_rng()`` to create a `Generator` and call its methods. =============== ========================================================= Generator --------------- --------------------------------------------------------- Generator Class implementing all of the random number distributions default_rng Default constructor for ``Generator`` =============== ========================================================= ============================================= === BitGenerator Streams that work with Generator --------------------------------------------- --- MT19937 PCG64 PCG64DXSM Philox SFC64 ============================================= === ============================================= === Getting entropy to initialize a BitGenerator --------------------------------------------- --- SeedSequence ============================================= === Legacy ------ For backwards compatibility with previous versions of numpy before 1.17, the various aliases to the global `RandomState` methods are left alone and do not use the new `Generator` API. ==================== ========================================================= Utility functions -------------------- --------------------------------------------------------- random Uniformly distributed floats over ``[0, 1)`` bytes Uniformly distributed random bytes. permutation Randomly permute a sequence / generate a random sequence. shuffle Randomly permute a sequence in place. choice Random sample from 1-D array. ==================== ========================================================= ==================== ========================================================= Compatibility functions - removed in the new API -------------------- --------------------------------------------------------- rand Uniformly distributed values. randn Normally distributed values. ranf Uniformly distributed floating point numbers. random_integers Uniformly distributed integers in a given range. (deprecated, use ``integers(..., closed=True)`` instead) random_sample Alias for `random_sample` randint Uniformly distributed integers in a given range seed Seed the legacy random number generator. ==================== ========================================================= ==================== ========================================================= Univariate distributions -------------------- --------------------------------------------------------- beta Beta distribution over ``[0, 1]``. binomial Binomial distribution. chisquare :math:`\chi^2` distribution. exponential Exponential distribution. f F (Fisher-Snedecor) distribution. gamma Gamma distribution. geometric Geometric distribution. gumbel Gumbel distribution. hypergeometric Hypergeometric distribution. laplace Laplace distribution. logistic Logistic distribution. lognormal Log-normal distribution. logseries Logarithmic series distribution. negative_binomial Negative binomial distribution. noncentral_chisquare Non-central chi-square distribution. noncentral_f Non-central F distribution. normal Normal / Gaussian distribution. pareto Pareto distribution. poisson Poisson distribution. power Power distribution. rayleigh Rayleigh distribution. triangular Triangular distribution. uniform Uniform distribution. vonmises Von Mises circular distribution. wald Wald (inverse Gaussian) distribution. weibull Weibull distribution. zipf Zipf's distribution over ranked data. ==================== ========================================================= ==================== ========================================================== Multivariate distributions -------------------- ---------------------------------------------------------- dirichlet Multivariate generalization of Beta distribution. multinomial Multivariate generalization of the binomial distribution. multivariate_normal Multivariate generalization of the normal distribution. ==================== ========================================================== ==================== ========================================================= Standard distributions -------------------- --------------------------------------------------------- standard_cauchy Standard Cauchy-Lorentz distribution. standard_exponential Standard exponential distribution. standard_gamma Standard Gamma distribution. standard_normal Standard normal distribution. standard_t Standard Student's t-distribution. ==================== ========================================================= ==================== ========================================================= Internal functions -------------------- --------------------------------------------------------- get_state Get tuple representing internal state of generator. set_state Set state of generator. ==================== =========================================================
- random …… Uniformly distributed floats over
[0, 1)- 0~1のランダム(均一分布)
- bytes …… Uniformly distributed random bytes.
- バイト型のランダム(均一分布)
- permutation …… Randomly permute a sequence / generate a random sequence.
- 引数指定した数字までの数字を並び替えてNumPy配列で戻す
- np.arange+並び替え
- shuffle …… Randomly permute a sequence in place.
- 引数のNumPy配列をシャッフルして戻す
- choice …… Random sample from 1-D array.
- ランダム取り出し、出現確率指定して取り出し
- rand …… Uniformly distributed values.
- 0~1のランダム(均一分布)
- randomとの違いは
- randn …… Normally distributed values.
- 標準正規分布に従ったランダム
- normal …… Normal / Gaussian distribution.
- 正規分布、平均と標準偏差を指定できる
- random_sample …… Alias for random
- 0~1のランダム(均一分布)
- randomとの違いは
- seed …… Seed the legacy random number generator.
- ランダム値を固定
サンプルコード
np.random.bytes
生成出力
import numpy as np
a = np.random.bytes(10)
print(a)
b'\xda\xad\xf4o\xf8\x91Xw\x96\x98'
もう少し見やすく
b = np.frombuffer(a, dtype='|S1')
print(b)
print(b.shape)
[b'\xda' b'\xad' b'\xf4' b'o' b'\xf8' b'\x91' b'X' b'w' b'\x96' b'\x98'] (10,)
- dtype=’|S1’指定で、バイナリデータの羅列を1つずつに分ける
- 10個のバイナリデータが取得できていることが分かる
- \xで始まるものが16進数表記の数値
- np.frombufferの詳細については下記の記事参照
np.random.random
import numpy as np
print(np.random.random(10))
[0.03354005 0.95136527 0.13772939 0.62135773 0.01910842 0.26714388 0.40878306 0.34775321 0.6562178 0.4247621 ]
- 0~1の間
- .random_sampleのエイリアス
- 下記記事参照
np.random.permutation
import numpy as np
a = np.random.permutation(10)
print(a) #取得した値の確認
print(type(a)) #typeを確認
[6 5 8 3 1 2 7 0 9 4] <class 'numpy.ndarray'="">
- 重複なし
- 似たものにrandint、こちらは数値の重複ある
- NumPy配列が戻り値
np.random.shuffle
import numpy as np
a = np.arange(10)
print(a) #0~9まで並んでる
np.random.shuffle(a)
print(a) #シャッフルされた
[0 1 2 3 4 5 6 7 8 9] [2 9 1 7 4 0 8 3 5 6]
- ndarray(NumPy配列)内の並び順をシャッフル
np.random.choice
1000までの数字から10個
import numpy as np
print(np.random.choice(1000,10))
[682 437 557 401 419 940 761 331 555 972]
もう少し小さな数字で普通に抽出
import numpy as np
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(a)
print(np.random.choice(a, 8))
print(np.random.choice(a, 8))
print(np.random.choice(a, 8))
[0 1 2 3 4 5 6 7 8 9] [8 1 5 3 3 7 9 2] [3 3 6 1 8 9 2 1] [7 2 8 2 1 4 5 7]
- 重複ありで抽出されていることが確認できる
重複なしで抽出
import numpy as np
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(a)
print(a.shape)
print(np.random.choice(a, 8, False))
print(np.random.choice(a, 8, False))
print(np.random.choice(a, 8, False))
[0 1 2 3 4 5 6 7 8 9] (10,) [0 6 4 8 9 7 1 5] [0 9 1 6 5 7 2 4] [5 2 1 6 8 3 7 4]
- 重複していない
- aが10個のndarray(NumPy配列)なので、引数の8は11以上に変えることはできない
- 「ValueError: Cannot take a larger sample than population when 'replace=False’」
出現確率指定して抽出
import numpy as np
a = np.array([0,1,2])
b = np.random.choice(a, 10, replace=True, p=[0.2, 0.2, 0.6])
print(b)
[2 2 0 2 2 1 2 1 2 2]
- 0.6を指定した「2」が出やすくなっている
- replace=True設定したので、最初3個の数字を10個まで増やせる
np.random.rand
import numpy as np
print(np.random.rand(10))
[2 2 0 2 2 1 2 1 2 2]
- 0~1の間
- .random_sampleと使い方が違う
- 下記記事を参照
np.random.randn
import numpy as np
print(np.random.randn(10))
[-0.32692468 -0.04579719 -0.30446006 1.92301013 -0.078659 -0.58206572 -1.61798224 0.86726082 -1.04043709 0.65042142]
- 標準正規分布に従ったランダム値
- なだらかな山、中心(ゼロ)の出現確率が高く、端に行くほどでなくなる
np.random.normal
import numpy as np
print(np.random.normal(0, 2.5, size=(2,3)))
#print(np.random.normal(0, 2.5, size=(2,3))) #キーワード「size」はなくてもいい
[[ 0.33177074 -1.19035504 3.2711827 ] [ 0.4875332 1.00052497 -0.84408084]]
- 正規分布に従ったランダム値
- 正規分布の形(標準偏差+分散)は引数で指定
- 引数
- 第1 …… 平均
- 第2 …… 標準偏差
- 第3 …… 戻り値のサイズ指定
- 標準正規分布と山の形が違う
- 平均 …… 山が右に行ったり(数字大きく)、左に行ったり(数字小さく)
- 標準偏差 …… 山が横に広がったり(数字大きく)、縦に補足なったり(数字小さく)
np.random.randint
import numpy as np
a = np.random.randint(-5, 5, 3)
print(a)
print(a.shape)
b = np.random.randint(50, 100, (2,3))
print(b)
print(b.shape)
[ 2 -3 -5] (3,) [[86 79 96] [70 67 72]] (2, 3)
- 範囲と個数を指定する
- 整数値のみ
- -5~4の区間でランダム値3つ
- 50~99の区間でランダム値を指定したshapeで返す(指定はタプル)
np.random.seed
import numpy as np
np.random.seed(10)
a = np.random.randn(10)
np.random.seed(10)
b = np.random.randn(10)
np.random.seed(10)
c = np.random.randn(5)
print(a)
print(b)
print(c)
[ 1.3315865 0.71527897 -1.54540029 -0.00838385 0.62133597 -0.72008556 0.26551159 0.10854853 0.00429143 -0.17460021] [ 1.3315865 0.71527897 -1.54540029 -0.00838385 0.62133597 -0.72008556 0.26551159 0.10854853 0.00429143 -0.17460021] [ 1.3315865 0.71527897 -1.54540029 -0.00838385 0.62133597]
- いつも同じランダム値を選択する
- 取得する数を減らしても同じになる(上記例では10⇒5にしてみた)

