LoginSignup
1
1

More than 3 years have passed since last update.

数の集合を受け取って、正規分布を仮定して、その正規分布に従う乱数を返す

Posted at

難しい話ではないんですが、

ある数字の集合があったときに、それが正規分布に従うと仮定して、その正規分布に従う乱数をたくさん得たい。

これを実行する関数を作ってみました。

import numpy as np
def random_gauss(numbers, size=10):
    return np.random.normal(loc = np.mean(numbers), scale = np.std(numbers), size  = size)

これで、たとえば得られた数字が [5, 4, 5, 6, 5, 4, 5] で、それが正規分布に従うと仮定して、その正規分布に従う乱数を 10 個得たいとします。

obtained = [5, 4, 5, 6, 5, 4, 5]
random_gauss(obtained, 10)
array([4.77977069, 4.6853802 , 5.26933192, 4.53865225, 5.24104428,
       3.92201318, 4.31394991, 5.11750001, 5.14838062, 3.23263734])

10x10 の配列にしたければ、このように使えます。

obtained = [5, 4, 5, 6, 5, 4, 5]
random_gauss(obtained, (10, 10))
array([[4.29329647, 5.2471627 , 4.71340084, 5.85465204, 5.89516407,
        4.55110482, 6.25525269, 4.64901171, 5.42536685, 5.58770788],
       [5.67861129, 4.94102328, 6.77790586, 3.8582164 , 5.36784665,
        5.43751184, 5.14030784, 4.78585029, 4.7084496 , 4.25353288],
       [4.26591012, 4.24099113, 5.46428238, 5.27744576, 4.41300475,
        6.07647302, 5.05453331, 5.97655124, 4.98670422, 5.17337313],
       [4.40873499, 4.5629429 , 3.73268542, 4.82958227, 4.40850147,
        6.28637949, 4.65911079, 4.15151204, 4.89566688, 5.28266135],
       [4.7034054 , 5.17026093, 4.4403553 , 4.62355895, 4.7855689 ,
        4.67839428, 6.01478859, 5.74358323, 5.11884483, 5.11134914],
       [4.0797462 , 3.95483888, 4.51260295, 5.84880821, 2.84667954,
        5.31916493, 4.73691626, 4.75109749, 4.58356682, 4.55128143],
       [5.84606006, 4.38274727, 5.324277  , 5.12168242, 3.60557967,
        4.92809603, 4.78279947, 5.85618519, 4.4952449 , 4.61218377],
       [4.06391996, 5.49536423, 3.91027041, 5.23696996, 4.64761352,
        3.98023659, 4.73758287, 5.12500023, 5.6862004 , 5.72124111],
       [4.8539152 , 5.50043533, 5.88674704, 4.3603309 , 4.56167419,
        4.94060109, 5.64930582, 4.28465173, 4.79660025, 4.69518196],
       [4.59096486, 5.24698098, 4.95728241, 5.00961799, 4.35292064,
        4.02801381, 5.58337414, 5.71661697, 4.38613982, 5.57674788]])

簡単、簡単。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1