【Numpy】Python Numpyによる、様々なランダムプロット
はじめに
NumPyでは様々なランダム関数を提供しています。今回はその中でもよく用いられる以下の表の分布をpython NumPyで描写したいと思います。
idx | Name | method |
---|---|---|
1 | 正規分布正規分布*1 | np.random.normal(loc=0.0, scale=1.0, size=None)np.random.standard_normal(size=None) |
2 | 対数正規分布 | np.random.lognormal(mean=0.0, sigma=1.0, size=None) |
3 | 二項分布 | np.random.binomial(n, p, size=None) |
4 | ベータ分布 | np.random.beta(a, b, size=None) |
5 | ガンマ分布 | np.random.gamma(shape, scale=1.0, size=None) |
6 | ポアソン分布 | np.random.poisson(lam=1.0, size=None) |
7 | 指数分布 | np.random.exponential(scale=1.0, size=None) |
8 | 一様分布 | np.random.uniform(low=0.0, high=1.0, size=None) |
*1 normal distribution (mean=0, std=1)
※np: numpy
Random sampling (numpy.random) — NumPy v1.12 Manual
E[X]: Mean, V[X]: Standard deviationとして、それぞれの分布をNumpyで描きます 描写にはSeabornのkdeplotを用います。
正規分布
import numpy as np np.random.seed(57) mu, sigma = 1, 1 data = np.random.normal(mu, sigma, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
対数正規分布
import numpy as np np.random.seed(57) mu, sigma = 1, 1 data = np.random.lognormal(mu, sigma, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
二項分布
N: 試行回数(number of trials)、θ: 成功の確率(probability of each trial)
import numpy as np np.random.seed(57) n, p = 10, 0.6 data = np.random.lognormal(n, p, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
ベータ分布
import numpy as np np.random.seed(57) a, b = 2, 5 data = np.random.beta(a, b, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
ガンマ分布
import numpy as np np.random.seed(57) k, theta = 2, 2 data = np.random.gamma(k, theta, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
ポアソン分布
import numpy as np np.random.seed(57) lam = 10 data = np.random.poisson(lam, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
指数分布
import numpy as np np.random.seed(57) theta = 2 data = np.random.exponential(theta, 1000)
import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram sns.kdeplot(data, label="KDE", ax=ax1, color="r") #density plot ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()
一様分布
import numpy as np np.random.seed(57) a, b = 0.2, 0.8 data = np.random.uniform(a, b , 1000)
import matplotlib.pyplot as plt fig = plt.figure(figsize=(12,6)) plt.rcParams["font.size"] = 18 ax1 = plt.subplot(111) #density plot ax2 = ax1.twinx() #histogram ax1.hlines(1/(b-a), a, b, color="r") ax2.hist(data, bins=100, color="c") #histogram ax1.grid() ax1.set_ylabel("PDF") ax2.set_ylabel("Frequency") plt.show()