离散型随机变量
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(20200605)
n = 9
p = 0.1
size = 50000
x = np.random.binomial(n, p, size)
'''或者使用binom.rvs
#使用binom.rvs(n, p, size=1)函数模拟一个二项随机变量,可视化地表现概率
y = stats.binom.rvs(n, p, size=size)#返回一个numpy.ndarray
'''
print(np.sum(x == 0) / size)
plt.hist(x)
plt.xlabel('random variants: the times of success')
plt.ylabel('the sample of the set')
plt.show()
s = stats.binom.pmf(range(10), n, p)
print(np.around(s, 3))
0.3897

[0.387 0.387 0.172 0.045 0.007 0.001 0. 0. 0. 0. ]
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed(20200605)
n = 2
p = 0.5
size = 50000
x = np.random.binomial(n, p, size)
'''或者使用binom.rvs
#使用binom.rvs(n, p, size=1)函数模拟一个二项随机变量,可视化地表现概率
y = stats.binom.rvs(n, p, size=size)#返回一个numpy.ndarray
'''
print(np.sum(x == 0) / size)
print(np.sum(x == 1) / size)
print(np.sum(x == 2) / size)
plt.hist(x, density=True)
plt.xlabel('随机变量:硬币为正面次数', fontproperties='SimHei')
plt.ylabel('50000个样本中出现的次数', fontproperties='SimHei')
plt.show()
s = stats.binom.pmf(range(n + 1), n, p)
print(np.around(s, 3))
0.25154
0.49874
0.24972

[0.25 0.5 0.25]
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed(20200605)
lam = 42 / 6
size = 50000
x = np.random.poisson(lam, size)
'''或者
#模拟服从泊松分布的50000个随机变量
x = stats.poisson.rvs(lam,size=size)
'''
print(np.sum(x == 6) / size)
plt.hist(x)
plt.xlabel('随机变量:每十分钟接到订票电话的次数', fontproperties='SimHei')
plt.ylabel('50000个样本中出现的次数', fontproperties='SimHei')
plt.show()
x = stats.poisson.pmf(6, lam)
print(x)
0.14988

0.14900277967433773
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)
plt.hist(x, bins=8)
plt.xlabel('狗的数量', fontproperties=r'/usr/share/fonts/simhei.ttf')
plt.ylabel('50000个样本中出现的次数', fontproperties=r'/usr/share/fonts/simhei.ttf')
plt.title('超几何分布',fontsize=20, fontproperties=r'/usr/share/fonts/simhei.ttf')
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()
"""
M 为总体容量
n 为总体中具有成功标志的元素的个数
N,k 表示抽取N个元素有k个是成功元素
"""
x = range(8)
s = stats.hypergeom.pmf(k=x, M=20, n=7, N=12)
print(np.round(s, 3))
0.198664

[0. 0.004 0.048 0.199 0.358 0.286 0.095 0.01 ]
连续型随机变量
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(20200614)
a = 0
b = 100
size = 50000
x = np.random.uniform(a, b, size=size)
print(np.all(x >= 0))
print(np.all(x < 100))
y = (np.sum(x < 50) - np.sum(x < 10)) / size
print(y)
plt.hist(x, bins=20)
plt.show()
a = stats.uniform.cdf(10, 0, 100)
b = stats.uniform.cdf(50, 0, 100)
print(b - a)
True
True
0.40144

0.4
import numpy as np
np.random.seed(20200614)
print(np.random.rand())
print(np.random.rand(5))
print(np.random.rand(4, 3))
np.random.seed(20200614)
print(np.random.uniform())
print(np.random.uniform(size=5))
print(np.random.uniform(size=(4, 3)))
0.7594819171852776
[0.75165827 0.16552651 0.0538581 0.46671446 0.89076925]
[[0.10073292 0.14624784 0.40273923]
[0.21844459 0.22226682 0.37246217]
[0.50334257 0.01714939 0.47780388]
[0.08755349 0.86500477 0.70566398]]
0.7594819171852776
[0.75165827 0.16552651 0.0538581 0.46671446 0.89076925]
[[0.10073292 0.14624784 0.40273923]
[0.21844459 0.22226682 0.37246217]
[0.50334257 0.01714939 0.47780388]
[0.08755349 0.86500477 0.70566398]]
import numpy as np
np.random.seed(20200614)
x = np.random.randint(2, size=10)
print(x)
x = np.random.randint(1, size=10)
print(x)
x = np.random.randint(5, size=(2, 4))
print(x)
x = np.random.randint(1, 10, [3, 4])
print(x)
[0 0 0 1 0 1 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[[3 3 0 1]
[1 1 0 1]]
[[2 1 7 7]
[7 2 4 6]
[8 7 2 8]]
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)
print(y2)
print(y3)
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)
print(y2)
print(y3)
0.68596
0.95456
0.99744

0.6826894921370859
0.9544997361036416
0.9973002039367398
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)
np.random.seed(20200614)
mu = 5
sigma = 0.5
x = np.random.normal(mu, sigma, (2, 4))
print(x)
size = 50000
x = np.random.normal(mu, sigma, size)
print(np.mean(x))
print(np.std(x, ddof=1))
'''
ddof:int, optional
Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
where N represents the number of elements. By default ddof is zero.
'''
plt.hist(x, bins=20)
plt.show()
[[5.39654234 5.4088702 5.49104652 4.95817289]
[4.31977933 4.76502391 4.70720327 4.36239023]]
[[5.39654234 5.4088702 5.49104652 4.95817289]
[4.31977933 4.76502391 4.70720327 4.36239023]]
4.996403463175092
0.4986846716715106

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)
'''或者
#rvs(loc=0, scale=1/lam, size=size, random_state=None)模拟
'''
y1 = (np.sum(x < 1 / 7)) / size
y2 = (np.sum(x < 2 / 7)) / size
y3 = (np.sum(x < 3 / 7)) / size
print(y1)
print(y2)
print(y3)
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)
print(y2)
print(y3)
0.63218
0.86518
0.95056

0.6321205588285577
0.8646647167633873
0.950212931632136
import numpy as np
np.random.seed(20200614)
x = np.random.choice(10, 3)
print(x)
x = np.random.choice(10, 3, p=[0.05, 0, 0.05, 0.9, 0, 0, 0, 0, 0, 0])
print(x)
x = np.random.choice(10, 3, replace=False, p=[0.05, 0, 0.05, 0.9, 0, 0, 0, 0, 0, 0])
print(x)
aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
x = np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
print(x)
np.random.seed(20200614)
x = np.random.randint(0, 10, 3)
print(x)
[2 0 1]
[3 2 3]
[3 0 2]
['pooh' 'rabbit' 'pooh' 'pooh' 'pooh']
[2 0 1]
import numpy as np
np.random.seed(20200614)
x = np.arange(10)
np.random.shuffle(x)
print(x)
print(np.random.shuffle([1, 4, 9, 12, 15]))
x = np.arange(20).reshape((5, 4))
print(x)
np.random.shuffle(x)
print(x)
[6 8 7 5 3 9 1 4 0 2]
None
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 8 9 10 11]
[ 0 1 2 3]
[12 13 14 15]
[16 17 18 19]
[ 4 5 6 7]]
import numpy as np
np.random.seed(20200614)
x = np.arange(10)
y = np.random.permutation(x)
print(y)
print(np.random.permutation([1, 4, 9, 12, 15]))
x = np.arange(20).reshape((5, 4))
print(x)
y = np.random.permutation(x)
print(y)
[6 8 7 5 3 9 1 4 0 2]
[ 4 1 9 15 12]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 8 9 10 11]
[ 0 1 2 3]
[12 13 14 15]
[16 17 18 19]
[ 4 5 6 7]]
练习
import numpy as np
x = np.random.randint(5, 10, [5, 3])
print(x)
x = np.random.uniform(5, 10, [5, 3])
print(x)
[[6 6 5]
[6 8 8]
[7 9 9]
[6 6 7]
[8 6 8]]
[[9.13477517 7.90190832 5.01204465]
[7.25841789 9.54911823 5.05862277]
[9.6214281 9.0346432 5.35105615]
[9.88164525 7.65130301 6.04607113]
[5.805795 9.65992556 7.33807103]]