確率分布と乱数の取得


In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame

In [2]:
from numpy.random import rand
rand()


Out[2]:
0.9101617717352775

In [3]:
rand(5)


Out[3]:
array([ 0.19219039,  0.84879349,  0.93428742,  0.87510281,  0.18682109])

In [4]:
rand(2,3)


Out[4]:
array([[ 0.83129068,  0.20356972,  0.51369904],
       [ 0.46578993,  0.10993605,  0.43850937]])

In [5]:
from numpy.random import randint
randint(1,7)


Out[5]:
1

In [6]:
randint(1,7,10)


Out[6]:
array([3, 3, 2, 1, 6, 2, 4, 6, 5, 1])

In [7]:
randint(1,7,(3,5))


Out[7]:
array([[3, 3, 1, 2, 6],
       [3, 4, 4, 6, 4],
       [1, 1, 1, 5, 2]])

In [8]:
from numpy.random import normal
normal(loc=0,scale=3,size=10)


Out[8]:
array([ 0.8830914 , -1.57187196,  0.29676155,  2.49913635,  0.24391729,
       -0.95171119,  0.07932686, -5.52585846, -4.06982189, -0.10235083])

In [9]:
normal(loc=0,scale=3,size=(3,2))


Out[9]:
array([[-4.04028066, -1.06313702],
       [ 1.5675749 ,  1.59420409],
       [ 0.14059873, -0.12570023]])

In [10]:
val = normal(10,3,size=1000)
plt.hist(val, bins=20)


Out[10]:
(array([   5.,    3.,   13.,   25.,   30.,   53.,   73.,   88.,   94.,
         139.,  117.,   99.,   86.,   62.,   43.,   30.,   15.,   15.,
           8.,    2.]),
 array([  0.87864973,   1.79379669,   2.70894364,   3.6240906 ,
          4.53923755,   5.45438451,   6.36953146,   7.28467842,
          8.19982537,   9.11497233,  10.03011928,  10.94526623,
         11.86041319,  12.77556014,  13.6907071 ,  14.60585405,
         15.52100101,  16.43614796,  17.35129492,  18.26644187,  19.18158883]),
 <a list of 20 Patch objects>)

In [11]:
from numpy.random import multivariate_normal
c = np.array([[3,2],[2,3]])
c


Out[11]:
array([[3, 2],
       [2, 3]])

In [12]:
multivariate_normal(mean=[50,10],cov=c,size=4)


Out[12]:
array([[ 51.0919981 ,  10.35096788],
       [ 50.10280803,  10.96030018],
       [ 51.53171846,  13.65363085],
       [ 53.71290106,  13.07782919]])

In [13]:
vals = multivariate_normal([50,10],c,200)
data_x = [x for (x,y) in vals]
data_y = [y for (x,y) in vals]
plt.scatter(data_x, data_y)


Out[13]:
<matplotlib.collections.PathCollection at 0x603de10>

In [14]:
np.random.seed(10)
print randint(1,10,10)
print randint(1,10,10)


[5 1 2 1 2 9 1 9 7 5]
[4 1 5 7 9 2 9 5 2 4]

In [15]:
np.random.seed(10)
print randint(1,10,10)
print randint(1,10,10)


[5 1 2 1 2 9 1 9 7 5]
[4 1 5 7 9 2 9 5 2 4]

練習問題

(1) 2個のサイコロを振った結果をシュミレーションします。次の例のように、1〜6の整数のペアを含むarrayを1つ乱数で生成してください。

array([3, 5])

(2) 2個のサイコロを振った結果を10回分用意します。次の例のように、1〜6の整数のペア(リスト)を10組含むarrayを生成して、変数 dice に保存してください。

array([[6, 5], [3, 5], [1, 4], [6, 4], [4, 3], [2, 3], [1, 5], [4, 1], [3, 4], [2, 5]])

(3) 変数 dice に保存されたそれぞれの結果に対して、次の例のように、2個のサイコロの目の合計を計算してください。(計算結果はリストに保存すること。)

[11, 8, 5, 10, 7, 5, 6, 5, 7, 7]

(4) 2個のサイコロの目の合計を1000回分用意して、2〜12のそれぞれの回数をヒストグラムに表示してください。

ヒント:オプション bins=11, range=(1.5, 12.5) を指定するときれいに描けます。

(5) 0≦x≦1 の範囲を等分した10個の点 data_x = np.linspace(0,1,10) に対して、sin(2πx) の値を格納したarrayを作成して、変数 data_y に保存しなさい。

さらに、data_y に含まれるそれぞれの値に、平均 0、標準偏差 0.3 の正規分布に従う乱数を加えたarrayを作成して、変数 data_t に保存した後、(data_x, data_t) を散布図に表示しなさい。