首页 > 技术文章 > numpy.random模块常用函数解析

iwangzhengchao 2018-10-24 17:45 原文

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

1 import numpy as np
2 print('random_sample:\n', np.random.random_sample((2, 3)))
3 print('random:\n', np.random.random((2, 3)))
4 print('ranf:\n', np.random.ranf((2, 3)))
5 print('sample:\n', np.random.sample((2, 3)))
 1 random_sample:
 2  [[ 0.87996593  0.2706701   0.42158973]
 3  [ 0.91952234  0.99470239  0.07363656]]
 4 random:
 5  [[ 0.44572326  0.23595379  0.1061901 ]
 6  [ 0.48362249  0.4270327   0.12281262]]
 7 ranf:
 8  [[ 0.07180002  0.25542854  0.55630057]
 9  [ 0.38181471  0.91512916  0.04020929]]
10 sample:
11  [[ 0.80390231  0.0024602   0.95974309]
12  [ 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中

1 print('1:\n', np.random.choice(5))
2 print('2:\n', np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))
3 print('3:\n', np.random.choice(5, (2, 3)))
4 print('4:\n', np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))
 1 1:
 2  4
 3 2:
 4  [1 4]
 5 3:
 6  [[2 1 4]
 7  [0 2 3]]
 8 4:
 9  [[3 6 1 6 1]
10  [3 3 3 3 1]]

10. numpy.random.seed(None)

设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数

1 np.random.seed(2)
2 np.random.rand(2, 3)
1 array([[ 0.4359949 ,  0.02592623,  0.54966248],
2        [ 0.43532239,  0.4203678 ,  0.33033482]])
1 np.random.seed(2)
2 np.random.rand(2, 3)
1 array([[ 0.4359949 ,  0.02592623,  0.54966248],
2        [ 0.43532239,  0.4203678 ,  0.33033482]])
1 np.random.rand(2, 3)
1 array([[ 0.20464863,  0.61927097,  0.29965467],
2        [ 0.26682728,  0.62113383,  0.52914209]])

 

推荐阅读