numpy.random 模块对 Python 内置的 random 进行了补充,增加了一些用于高效生成多种概率分
布的样本值的函数,如正态分布、泊松分布等。
- 生成种子:numpy.random.seed(seed=None)
二项分布
numpy.random.binomial(n, p, size=None)
表示对一个二项分布进行采样, size 表示采样的次数, n 表示做了n 重伯努利试验, p 表示成功的概率,函数的返回值表示n 中成功的次数。
- 野外正在进行9(n=9)口石油勘探井的发掘工作,每一口井能够开发出油的概率是0.1(p=0.1)。请问,最终所有的勘探井都勘探失败的概率?
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed()//不指定数字,随机种子
n = 9
p = 0.1
size = 100000
x = np.random.binomial(n, p, size)
#print(x)
sumFail = np.sum(x==0)
print(sumFail)
print(sumFail/size)
plt.hist(x)
plt.xlabel('随机变量')
plt.ylabel('样本中出现的次数')
plt.show()
s = stats.binom.pmf(range(5),n,p)//s接收0-4五个值
print(s)
print(np.around(s, 3))//输出结果保留三位小数
- 模拟投硬币,投2次,请问两次都为正面的概率?
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed()
n = 2
p = 0.5
size = 1000000
x = np.random.binomial(n,p,size)
#print('P(x=2)=',np.sum(x==2)/size)
print(np.around(np.sum(x==2)/size,4))
plt.hist(x)
plt.show()
s = stats.binom.pmf(range(n+1), n, p)
print(np.around(s, 3))
泊松分布
泊松概率函数的数学表示:
numpy.random.poisson(lam=1.0, size=None)
表示对一个泊松分布进行采样, size 表示采样的次数, lam 表示一个单位内发生事件的平均值,函
数的返回值表示一个单位内事件发生的次数。
- 假定某航空公司预定票处平均每小时接到42次订票电话,那么10分钟内恰好接到6次电话的概率是多少?
np.random.seed()
lam = 42 / 6
size = 100000
x = np.random.poisson(lam, size)
print(x)
y = np.sum(x==6)
print(y/size)
plt.hist(x)
plt.show()
x = stats.poisson.pmf(6,lam)
print(x)
超几何分布
在超几何分布中,各次实验不是独立的,各次实验成功的概率也不等。 超几何分布概率函数的数学表示:
numpy.random.hypergeometric(ngood, nbad, nsample, size=None)
表示对一个超几何分布进行采样, size 表示采样的次数, ngood 表示总体中具有成功标志的元素个数, nbad 表示总体中不具有成功标志的元素个数, ngood+nbad 表示总体样本容量, nsample 表示抽取元素的次数(小于或等于总体样本容量),函数的返回值表示抽取nsample 个元素中具有成功标识的元素个数。
- 一共20只动物里有7只是狗,抽取12只有3只狗的概率(无放回抽样)。
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed(20200605)
size = 500000
x = np.random.hypergeometric(ngood=7, nbad=13, nsample=12, size=size)
'''或者
#用rvs(M, n, N, loc=0, size=1, random_state=None)模拟
x = stats.hypergeom.rvs(M=20,n=7,N=12,size=size)
'''
print(np.sum(x == 3) / size) # 0.198664
plt.hist(x, bins=8)
plt.xlabel('狗的数量')
plt.ylabel('50000个样本中出现的次数')
plt.title('超几何分布',fontsize=20)
plt.show()
"""
M 为总体容量
n 为总体中具有成功标志的元素的个数
N,k 表示抽取N个元素有k个是成功元素
"""
x = range(8)
#用hypergeom.pmf(k, M, n, N, loc)来计算k次成功的概率
s = stats.hypergeom.pmf(k=x, M=20, n=7, N=12)
print(np.round(s, 3))
# [0. 0.004 0.048 0.199 0.358 0.286 0.095 0.01 ]
连续随机变量
均匀分布
numpy.random.uniform(low=0.0, high=1.0, size=None)
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed()
a = 0
b = 100
size = 100000
x = np.random.uniform(a,b,size=size)
print(np.all(x)>=0)
print(np.all(x)<100)
plt.hist(x)
plt.show()
numpy.random.rand(d0, d1, …, dn) 可以得到[0,1)之间的均匀分布随机数
import numpy as np
np.random.seed(1000)
print(np.random.rand())
print(np.random.rand(5))
np.random.seed(1000)
print(np.random.uniform())
print(np.random.uniform(size = 5))
0.6535895854646095
[0.11500694 0.95028286 0.4821914 0.87247454 0.21233268]
0.6535895854646095
[0.11500694 0.95028286 0.4821914 0.87247454 0.21233268]
numpy.random.randint(low, high=None, size=None, dtype=‘l’) 可以得到[low,high) 之间均匀分布的随机整数。若high 不为None 时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。
正态分布
numpy.random.randn(d0, d1, …, dn)
根据指定大小产生满足标准正态分布的数组(均值为0,标准差为1)。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(20200614)
size = 50000
x = np.random.randn(size)
y1 = (np.sum(x < 1) ‐ np.sum(x < ‐1)) / size
y2 = (np.sum(x < 2) ‐ np.sum(x < ‐2)) / size
y3 = (np.sum(x < 3) ‐ np.sum(x < ‐3)) / size
print(y1) # 0.68596
print(y2) # 0.95456
print(y3) # 0.99744
plt.hist(x, bins=20)
plt.show()
y1 = stats.norm.cdf(1) ‐ stats.norm.cdf(‐1)
y2 = stats.norm.cdf(2) ‐ stats.norm.cdf(‐2)
y3 = stats.norm.cdf(3) ‐ stats.norm.cdf(‐3)
print(y1) # 0.6826894921370859
print(y2) # 0.9544997361036416
print(y3) # 0.9973002039367398
指定分布以及所需参数:
numpy.random.normal(loc=0.0, scale=1.0, size=None)
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(20200614)
x = 0.5 * np.random.randn(2, 4) + 5
'''或者
#模拟10000个随机变量
x = 0.5*stats.norm.rvs(size=(2,4))+5
'''
print(x)
# [[5.39654234 5.4088702 5.49104652 4.95817289]
# [4.31977933 4.76502391 4.70720327 4.36239023]]
np.random.seed(20200614)
mu = 5#平均值
sigma = 0.5#标准差
x = np.random.normal(mu, sigma, (2, 4))
print(x)
# [[5.39654234 5.4088702 5.49104652 4.95817289]
# [4.31977933 4.76502391 4.70720327 4.36239023]]
size = 50000
x = np.random.normal(mu, sigma, size)
print(np.mean(x)) # 4.996403463175092
print(np.std(x, ddof=1)) # 0.4986846716715106(#样本标准差)
plt.hist(x, bins=20)
plt.show()
指数分布
numpy.random.exponential(scale=1.0, size=None)
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(20200614)
lam = 7
size = 50000
x = np.random.exponential(1 / lam, size)
y1 = (np.sum(x < 1 / 7)) / size
y2 = (np.sum(x < 2 / 7)) / size
y3 = (np.sum(x < 3 / 7)) / size
print(y1) # 0.63218
print(y2) # 0.86518
print(y3) # 0.95056
plt.hist(x, bins=20)
plt.show()
y1 = stats.expon.cdf(1 / 7, scale=1 / lam)
y2 = stats.expon.cdf(2 / 7, scale=1 / lam)
y3 = stats.expon.cdf(3 / 7, scale=1 / lam)
print(y1) # 0.6321205588285577
print(y2) # 0.8646647167633873
print(y3) # 0.950212931632136