Numpy常用random随机函数汇总
函数名 | 说明 |
---|---|
seed([seed]) | 设定随机种子,这样每次生成的随机数会相同 |
rand(d0, d1, …, dn) | 返回数据在[0,1)之间,具有均匀分布 |
randn(d0, d1, …, dn) | 返回数据具有标准正态分布(均值0, 方差1) |
randint(low[, high, size, dtype]) | 生成随机整数,包含low,不包含high |
random([size]) | 生成[0.0, 1.0)的随机数 |
choice(a [, size, replace, p]) | a是一维数组,从它里面生成随机结果 |
shuffle(x) | 把一个数组x进行随机排列 |
permutation(x) | 把一个数组x进行随机排列,或者数字的全排列 |
normal([loc, scale, size]) | 按照平均值loc和方差scale生成高斯分布的数字 |
uniform([low, high, size]) | 在[low,high)之间生成均匀分布的数字 |
In [1]:
import numpy as np
np.random.seed(666)
1.rand(d0, d1, …, dn)
返回数据在[0, 1)之间,具有均匀分布
In [2]:
np.random.rand(5)
Out[2]:
array([0.70043712, 0.84418664, 0.67651434, 0.72785806, 0.95145796])
In [3]:
np.random.rand(3, 4)
Out[3]:
array([[0.0127032 , 0.4135877 , 0.04881279, 0.09992856],
[0.50806631, 0.20024754, 0.74415417, 0.192892 ],
[0.70084475, 0.29322811, 0.77447945, 0.00510884]])
In [4]:
np.random.rand(2, 3, 4)
Out[4]:
array([[[0.11285765, 0.11095367, 0.24766823, 0.0232363 ],
[0.72732115, 0.34003494, 0.19750316, 0.90917959],
[0.97834699, 0.53280254, 0.25913185, 0.58381262]],
[[0.32569065, 0.88889931, 0.62640453, 0.81887369],
[0.54734542, 0.41671201, 0.74304719, 0.36959638],
[0.07516654, 0.77519298, 0.21940924, 0.07934213]]])
2.randn(d0, d1, …, dn)
返回数据具有标准正态分布(均值0,方差1)
In [5]:
np.random.randn(5)
Out[5]:
array([-1.20990266, -0.04618272, -0.44118244, 0.46953431, 0.44325817])
In [6]:
np.random.randn(3, 4)
Out[6]:
array([[-1.66738875, -1.81731749, -1.39753916, 0.78392691],
[-0.29129965, 0.67049043, 0.706931 , 1.42965241],
[-0.41407013, -1.32672274, -0.14880188, 0.34771289]])
In [7]:
np.random.randn(2, 3, 4)
Out[7]:
array([[[ 0.61030791, -1.17532603, 0.82985368, -0.30236752],
[-0.04327047, 0.06706965, -1.59102817, 0.01705112],
[-1.87296591, -0.96457904, -0.00420389, 0.47495047]],
[[-0.05421452, 0.89181355, 0.96866859, 0.6307865 ],
[-0.89051986, 0.08227022, -0.07594056, 0.42969347],
[ 0.11579967, -0.54443241, 0.02835341, 1.34408655]]])
3.randint(low[, high, size, dtype])
生成随机整数,包含low,不包含high。如果high不指定,则从[0, low)中生成数字
In [8]:
np.random.randint(3)
Out[8]:
2
In [12]:
np.random.randint(1, 10)
Out[12]:
9
In [13]:
np.random.randint(10, 30, size=(5,))
Out[13]:
array([18, 26, 19, 14, 21])
In [14]:
np.random.randint(10, 30, size=(2, 3, 4))
Out[14]:
array([[[29, 25, 19, 21],
[28, 12, 13, 19],
[27, 27, 18, 27]],
[[16, 24, 16, 19],
[21, 20, 19, 14],
[22, 26, 29, 20]]])
4.random([size])
生成[0.0, 1.0)的随机数
In [15]:
np.random.random(5)
Out[15]:
array([0.67940357, 0.65592832, 0.0039444 , 0.06654134, 0.00112109])
In [16]:
np.random.random(size=(3, 4))
Out[16]:
array([[0.66608382, 0.38565116, 0.09405827, 0.45856757],
[0.64434173, 0.59499774, 0.79060307, 0.79996907],
[0.67969792, 0.43875185, 0.26235889, 0.23652188]])
In [17]:
np.random.random(size=(2, 3, 4))
Out[17]:
array([[[0.83900208, 0.36874334, 0.61918838, 0.46656433],
[0.49250063, 0.71862211, 0.65415881, 0.9665017 ],
[0.38957233, 0.97017219, 0.36057961, 0.56184234]],
[[0.03133558, 0.30480028, 0.07269465, 0.46721993],
[0.41345069, 0.42228271, 0.79491031, 0.05651855],
[0.89718201, 0.31869638, 0.36398678, 0.70548804]]])
5.choice(a[, size, replace, p])
a是一维数组,从它里面生成的随机结果
In [18]:
# 这时候,a是数字,则从range(5)中生成,size为3
np.random.choice(5, 3)
Out[18]:
array([3, 2, 4])
In [19]:
np.random.choice(5, (2, 3))
Out[19]:
array([[0, 2, 1],
[4, 2, 3]])
In [20]:
# 这时候,a是数组,从里面随机取出数组
np.random.choice([2, 3, 6, 7, 9], 3)
Out[20]:
array([9, 2, 6])
In [21]:
np.random.choice([2, 3, 6, 7, 9], (2, 3))
Out[21]:
array([[9, 6, 2],
[9, 2, 3]])
6.shuffle(x)
把一个数组x进行随机排列
In [22]:
a = np.arange(10)
np.random.shuffle(a)
a
Out[22]:
array([1, 9, 3, 2, 4, 6, 0, 5, 7, 8])
In [25]:
a = np.arange(20).reshape(4, 5)
a
Out[25]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
In [26]:
# 如果数组是多维的,则只会在第一维度打散数据 只把行数打散
np.random.shuffle(a)
a
Out[26]:
array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[10, 11, 12, 13, 14],
[ 0, 1, 2, 3, 4]])
7.permutation(x)
把一个数组x进行随机排列,或者数字的全排列
In [27]:
# 这时候,生成range(10)的随机排列
np.random.permutation(10)
Out[27]:
array([2, 5, 8, 1, 9, 0, 6, 4, 3, 7])
In [29]:
# 这时候,在第一维度进行打散
arr = np.arange(9).reshape((3, 3))
arr
Out[29]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [30]:
# 注意,这里不会更改原来的arr,会返回一个新的copy
np.random.permutation(arr)
Out[30]:
array([[0, 1, 2],
[6, 7, 8],
[3, 4, 5]])
8.normal([loc, scale, size])
按照平均值loc和方差scale生成高斯分布的数字
In [31]:
np.random.normal(1, 10, 10)
Out[31]:
array([ 10.80291099, 9.2194 , -13.45464412, 5.92066144,
2.55578918, 4.70191278, 1.25552781, 19.16839105,
-11.2572319 , -13.4167598 ])
In [32]:
np.random.normal(1, 10, (3, 4))
Out[32]:
array([[ 13.53763269, 13.41439272, 7.04674961, 1.11736277],
[ -7.16656663, -9.45062631, 5.06354375, -7.57895331],
[ 8.05614584, 2.69688786, 23.53394139, -13.75199322]])
9.uniform([low, high, size])
在[low, high)之间生成均匀分布的数字
In [33]:
np.random.uniform(1, 10, 10)
Out[33]:
array([3.1972772 , 6.84872322, 4.33722237, 8.13376077, 8.64726595,
8.12580587, 2.93863321, 7.52063524, 7.98059166, 6.48754099])
In [34]:
np.random.uniform(1, 10, (3, 4))
Out[34]:
array([[3.5182339 , 7.30229967, 5.12969041, 9.02342506],
[6.99628706, 5.64623389, 7.32123531, 6.77599624],
[9.30139114, 2.38426176, 9.42378508, 4.75697765]])
实例:对数组加入随机噪声
In [35]:
import matplotlib.pyplot as plt
In [36]:
# 绘制sin曲线
x = np.linspace(-10, 10, 100) # 指定最小数、最大数,在它们中间生成100个点
y = np.sin(x)
plt.plot(x, y)
plt.show()
In [37]:
# 加入噪声
x = np.linspace(-10, 10, 100)
y = np.sin(x) + np.random.rand(len(x)) # len(x)生成的维度和x,y是一样的
plt.plot(x, y)
plt.show()