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]:
In [3]:
rand(5)
Out[3]:
In [4]:
rand(2,3)
Out[4]:
In [5]:
from numpy.random import randint
randint(1,7)
Out[5]:
In [6]:
randint(1,7,10)
Out[6]:
In [7]:
randint(1,7,(3,5))
Out[7]:
In [8]:
from numpy.random import normal
normal(loc=0,scale=3,size=10)
Out[8]:
In [9]:
normal(loc=0,scale=3,size=(3,2))
Out[9]:
In [10]:
val = normal(10,3,size=1000)
plt.hist(val, bins=20)
Out[10]:
In [11]:
from numpy.random import multivariate_normal
c = np.array([[3,2],[2,3]])
c
Out[11]:
In [12]:
multivariate_normal(mean=[50,10],cov=c,size=4)
Out[12]:
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]:
In [14]:
np.random.seed(10)
print randint(1,10,10)
print randint(1,10,10)
In [15]:
np.random.seed(10)
print randint(1,10,10)
print randint(1,10,10)
(1) 2個のサイコロを振った結果をシュミレーションします。次の例のように、1〜6の整数のペアを含むarrayを1つ乱数で生成してください。
(2) 2個のサイコロを振った結果を10回分用意します。次の例のように、1〜6の整数のペア(リスト)を10組含むarrayを生成して、変数 dice に保存してください。
(3) 変数 dice に保存されたそれぞれの結果に対して、次の例のように、2個のサイコロの目の合計を計算してください。(計算結果はリストに保存すること。)
(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) を散布図に表示しなさい。