您的位置:首页 > 编程语言 > Python开发

numpy.random模块中常用函数解析

2018-01-20 10:05 399 查看


numpy.random模块中常用函数解析


numpy.random模块官方文档


1. numpy.random.rand(d0, d1, ..., dn)


Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)


按照给定形状产生一个多维数组,每个元素在0到1之间

注意: 这里定义数组形状时,不能采用tuple
import numpy as np
np.random.rand(2, 3)
array([[ 0.44590044,  0.36234046,  0.51609462],
[ 0.45733218,  0.80836224,  0.31628453]])



2. numpy.random.randn(d0, d1, ..., dn)


generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate “normal” distribution of mean 0 and variance 1

按照给定形状产生一个多维数组,数组中的元素服从标准正态分布

若要产生服从N(mu, sigma^2)分布的样本, 使用sigma * np.random.randn(...) + mu

例如产生 2 * 4 samples from N(3, 6.25):

2.5 * np.random.randn(2, 4) + 3
array([[ 2.90478558,  6.05670578,  6.21539068,  3.3955507 ],
[ 0.11594363,  3.17433693,  5.35625762,  1.4824643 ]])



3. numpy.random.randint(low, high=None, size=None, dtype='l')

Return random integers from low (inclusive) to high (exclusive).

按照给定的形状和范围产生随机整数

np.random.randint(0, 10, size=(2, 4))
array([[2, 7, 2, 1],
[3, 2, 4, 1]])


4. numpy.random.random_integers(low, high=None, size=None)

Random integers of type np.int between low and high, inclusive.
np.random.random_integers(1, 10, size=(2, 5))
array([[ 3,  3,  8,  4,  5],
[ 2,  7,  8, 10,  2]])


5. numpy.random.random_sample(size=None)

6. numpy.random.random(size=None)

7. numpy.random.ranf(size=None)

8. numpy.random.sample(size=None)

Return random floats in the half-open interval [0.0, 1.0).

以上四种方式都是生成[0,1)之间的浮点数

To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:

(b - a) * random_sample() + a
import numpy as np
print('random_sample:\n', np.random.random_sample((2, 3)))
print('random:\n', np.random.random((2, 3)))
print('ranf:\n', np.random.ranf((2, 3)))
print('sample:\n', np.random.sample((2, 3)))
random_sample:
[[ 0.87996593  0.2706701   0.42158973]
[ 0.91952234  0.99470239  0.07363656]]
random:
[[ 0.44572326  0.23595379  0.1061901 ]
[ 0.48362249  0.4270327   0.12281262]]
ranf:
[[ 0.07180002  0.25542854  0.55630057]
[ 0.38181471  0.91512916  0.04020929]]
sample:
[[ 0.80390231  0.0024602   0.95974309]
[ 0.32902852  0.62796713  0.42254831]]


9. numpy.random.choice(a, size = None, replace=True, p=None)

从给定的一维数组中生成随机数

如a是一个int数, 则产生的数组的元素都在np.arange(a)中

如a是一个1-D array-like, 则产生的数组的元素都在a中
print('1:\n', np.random.choice(5))
print('2:\n', np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))
print('3:\n', np.random.choice(5, (2, 3)))
print('4:\n', np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))
1:
4
2:
[1 4]
3:
[[2 1 4]
[0 2 3]]
4:
[[3 6 1 6 1]
[3 3 3 3 1]]


10. numpy.random.seed(None)

设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
np.random.seed(2)
np.random.rand(2, 3)
array([[ 0.4359949 ,  0.02592623,  0.54966248],
[ 0.43532239,  0.4203678 ,  0.33033482]])
np.random.seed(2)
np.random.rand(2, 3)
array([[ 0.4359949 ,  0.02592623,  0.54966248],
[ 0.43532239,  0.4203678 ,  0.33033482]])
np.random.rand(2, 3)
array([[ 0.20464863,  0.61927097,  0.29965467],
[ 0.26682728,  0.62113383,  0.52914209]])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: