内容目录
Numpy的random模块¶
random.rand()¶
In [1]:
import numpy as np
np.random.seed(1)
np.random.rand()
Out[1]:
0.417022004702574
random.randn()¶
生成均值为0,标准差为1的标准正态分布的值
In [2]:
data = np.random.randn(2, 3) # 生成2行3列的数组,数组中数值满足标准正态分布
print(data)
[[-0.80217284 -0.44887781 -1.10593508] [-1.65451545 -2.3634686 1.13534535]]
random.randint()¶
random.choice()¶
从数组中随机挑选对应的数据
In [3]:
data = np.random.randint(0, 10, size=(10,))
print(data)
np.random.choice(data, 3)
np.random.choice(data, size=(3, 4))
[2 4 2 4 7 7 9 1 7 0]
Out[3]:
array([[1, 9, 0, 4], [2, 4, 7, 7], [4, 0, 7, 1]])
random.shuffle()¶
随机将数组中的数据打乱
In [4]:
data1 = np.arange(10)
print(data1)
np.random.shuffle(data1)
print(data1)
[0 1 2 3 4 5 6 7 8 9] [2 7 4 0 9 8 1 5 6 3]