1. 二项分布
numpy.random.binomial
(n, p, size=None)¶- n: 试验的次数
- p: 成功的概率
- size: 输出的格式
- 返回n次试验得到成功样本的次数
>>> np.random.binomial(10, 0.5)
4
>>> np.random.binomial(10, 0.5, 100)
array([6, 5, 7, 3, 3, 7, 7, 4, 6, 4, 3, 5, 5, 4, 4, 3, 5, 3, 6, 5, 7, 5,
8, 1, 3, 6, 6, 6, 8, 7, 3, 3, 5, 6, 9, 4, 5, 5, 5, 6, 5, 4, 2, 4,
3, 2, 4, 5, 3, 4, 6, 5, 5, 6, 6, 4, 5, 7, 2, 5, 5, 4, 4, 4, 2, 2,
4, 3, 4, 5, 5, 5, 5, 5, 4, 4, 4, 9, 4, 4, 5, 4, 6, 5, 5, 5, 6, 5,
7, 3, 3, 4, 4, 5, 4, 5, 6, 7, 4, 5])
>>> np.random.binomial(10, 0.5, (10,2))
array([[5, 4],
[6, 5],
[2, 7],
[4, 7],
[7, 5],
[6, 4],
[5, 5],
[7, 8],
[4, 5],
[5, 4]])
2. 均匀分布
numpy.random.
uniform
(low=0.0, high=1.0, size=None)¶- low: [low, high), x轴的左边界
- high: x轴的有边界
- size: 同上
- 返回均匀分布抽样得到的样本
>>> np.random.uniform(-1, 0)
-0.3215569004238422
>>> np.random.uniform(1, 2)
1.6176921978063046
>>> np.random.uniform(1, 2, 10)
array([1.12308939, 1.75385708, 1.10591517, 1.68946113, 1.76664439,
1.82339173, 1.32988529, 1.46474089, 1.26986578, 1.83440486])
3. 高斯分布(正态分布)
numpy.random.
normal
(loc=0.0, scale=1.0, size=None)¶- loc: 均值
- scale: 标准差
- size: 同上
- 返回正态分布的样本
>>> s = np.random.normal(0, 0.1, 1000)
>>> np.mean(s)
0.004807401973675762
>>> np.std(s)
0.09799453406363992
>>> import matplotlib.pyplot as plt
>>> plt.hist(s, 30, density=True)
(array([0.04649826, 0. , 0.04649826, 0. , 0.13949478,
0.18599304, 0.37198607, 0.41848433, 0.92996518, 1.34844951,
1.95292687, 1.62743906, 2.65040076, 3.67336245, 3.3943729 ,
3.90585375, 4.46383285, 3.48736942, 3.85935549, 3.06888509,
3.11538335, 1.95292687, 2.23191643, 1.39494777, 0.92996518,
0.41848433, 0.18599304, 0.32548781, 0.18599304, 0.18599304]), array([-0.35268248, -0.3311763 , -0.30967012, -0.28816394, -0.26665776,
-0.24515158, -0.22364539, -0.20213921, -0.18063303, -0.15912685,
-0.13762067, -0.11611449, -0.09460831, -0.07310212, -0.05159594,
-0.03008976, -0.00858358, 0.0129226 , 0.03442878, 0.05593497,
0.07744115, 0.09894733, 0.12045351, 0.14195969, 0.16346587,
0.18497206, 0.20647824, 0.22798442, 0.2494906 , 0.27099678,
0.29250296]), <a list of 30 Patch objects>)
>>> plt.show()
4. 卡方分布
numpy.random.
chisquare
(df, size=None)¶- df: 自由度
- 返回取样的样本
>>> np.random.chisquare(2, 10)
array([0.57828996, 1.44692155, 1.3593016 , 5.82680881, 0.66790546,
3.90958583, 2.58751119, 1.1587489 , 0.20344843, 8.62224483])
>>> np.random.chisquare(3, 10)
array([5.29193161, 1.49728924, 2.73340095, 2.44084495, 2.6488522 ,
5.45728677, 6.62040015, 0.2761154 , 2.2039041 , 0.52572555])
>>> np.random.chisquare(4, 10)
array([0.65494551, 3.76197252, 2.45412806, 1.40603174, 3.99235582,
3.47982822, 3.02089244, 3.86537378, 2.51286349, 2.03157254])