import numpy as np
(0, 1)间的随机数(组)
print(np.random.random()) # 0.9712291171155641print(np.random.random(3)) # [0.54426685 0.10859501 0.66425772]
随机选择choice
print(np.random.choice([2, 3, 5, 7], 5)) # [7 7 3 5 3]
指定范围的随机整数(组矩阵)
print(np.random.randint(2, 10)) # 7print(np.random.randint(2, 10, 3)) # [5 3 5]print(np.random.randint(2, 10, (2, 2))) # [[2 7]# [3 2]]
服从正态分布的随机整数
np.random.normal(loc=μ,scale=δ,size=shape)正态分布mdt = np.random.normal(0, 1, 100) # 产生服从标准正态分布的100个随机数# Numpy Functions: min() max() mean() median() std()(标准差)print(f'min: {mdt.min(): .2f}, max: {mdt.max(): .2f}, mean: {mdt.mean(): .2f}, std: {mdt.std(): .2f}')# min: -2.67, max: 2.88, mean: -0.01, std: 1.02
复制(?)
# (repeating sequences) np.tile np.repeatprint(np.tile([1, 3, 2], 2)) # [1 3 2 1 3 2]print(np.repeat([1, 3, 2], 2)) # [1 1 3 3 2 2]
结合matplotlib
import matplotlib.pyplot as pltplt.hist(mdt, color="grey", bins=21)plt.show()