numpy.random

from numpy import random

numpy.random.uniform(low=0.0, high=1.0, size=None)


 生出size个符合均分布的浮点数,取值范围为[low, high),默认取值范围为[0, 1.0)

>>> random.uniform()
0.3999807403689315
>>> random.uniform(size=1)
array([0.55950578])
>>> random.uniform(5, 6)
5.293682668235986
>>> random.uniform(5, 6, size=(2,3))
array([[5.82416021, 5.68916836, 5.89708586],
       [5.63843125, 5.22963754, 5.4319899 ]])

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


 生成一个(d0, d1, ..., dn)维的数组,数组的元素取自[0, 1)上的均分布,若没有参数输入,则生成一个数

>>> random.rand()
0.4378166124207712
>>> random.rand(1)
array([0.69845956])
>>> random.rand(3,2)
array([[0.15725424, 0.45786148],
       [0.63133098, 0.81789056],
       [0.40032941, 0.19108526]])
>>> random.rand(3,2,1)
array([[[0.00404447],
        [0.3837963 ]],

       [[0.32518355],
        [0.82482599]],

       [[0.79603205],
        [0.19087375]]])

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


生成size个整数,取值区间为[low, high),若没有输入参数high则取值区间为[0, low)

>>> random.randint(8)
5
>>> random.randint(8, size=1)
array([1])
>>> random.randint(8, size=(2,2,3))
array([[[4, 7, 0],
        [1, 4, 1]],

       [[2, 2, 5],
        [7, 6, 4]]])
>>> random.randint(8, size=(2,2,3), dtype='int64')
array([[[5, 5, 6],
        [2, 7, 2]],

       [[2, 7, 6],
        [4, 7, 7]]], dtype=int64)

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


 生成size个整数,取值区间为[low, high], 若没有输入参数high则取值区间为[1, low],注意这里左右都是闭区间

>>> random.random_integers(5)
1
>>> random.random_integers(5, size=1)
array([2])
>>> random.random_integers(4, 5, size=(2,2))
array([[5, 4],
       [4, 4]])

numpy.random.random(size=None)


 产生[0.0, 1.0)之间的浮点数

>>> random.random(5)
array([0.94128141, 0.98725499, 0.48435957, 0.90948135, 0.40570882])
>>> random.random()
0.49761416226728084

 相同用法:

  • numpy.random.random_sample
  • numpy.random.ranf
  • numpy.random.sample (抽取不重复)

 

numpy.random.bytes(length)


 生成随机字节

>>> random.bytes(1)
b'%'
>>> random.bytes(2)
b'\xd0\xc3'

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


 从a(数组)中选取size(维度)大小的随机数,replace=True表示可重复抽取,p是a中每个数出现的概率

 若a是整数,则a代表的数组是arange(a)

>>> random.choice(5)
3
>>> random.choice([0.2, 0.4])
0.2
>>> random.choice([0.2, 0.4], p=[1, 0])
0.2
>>> random.choice([0.2, 0.4], p=[0, 1])
0.4
>>> random.choice(5, 5)
array([1, 2, 4, 2, 4])
>>> random.choice(5, 5, False)
array([2, 0, 1, 4, 3])
>>> random.choice(100, (2, 3, 5), False)
array([[[43, 81, 48,  2,  8],
        [33, 79, 30, 24, 83],
        [ 3, 82, 97, 49, 98]],

       [[32, 12, 15,  0, 96],
        [19, 61,  6, 42, 60],
        [ 7, 93, 20, 18, 58]]])

numpy.random.permutation(x)


 随机打乱x中的元素。若x是整数,则打乱arange(x),若x是一个数组,则将copy(x)的第一位索引打乱,意思是先复制x,对副本进行打乱处理,打乱只针对数组的第一维

>>> random.permutation(5)
array([1, 2, 3, 0, 4])
>>> random.permutation(5)
array([1, 4, 3, 2, 0])
>>> random.permutation([[1,2,3],[4,5,6]])
array([[1, 2, 3],
       [4, 5, 6]])
>>> random.permutation([[1,2,3],[4,5,6]])
array([[4, 5, 6],
       [1, 2, 3]])

numpy.random.shuffle(x)


 与permutation类似,随机打乱x中的元素。若x是整数,则打乱arange(x). 但是shuffle会对x进行修改

>>> a = arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> random.permutation(a)
array([1, 4, 3, 2, 0])
>>> a
array([0, 1, 2, 3, 4])
>>> random.shuffle(a)
>>> a
array([4, 1, 3, 2, 0])

numpy.random.seed(seed=None)


 设置随机生成算法的初始值

 

其它符合函数分布的随机数函数


  • numpy.random.beta
  • numpy.random.binomial
  • numpy.random.chisquare
  • numpy.random.dirichlet
  • numpy.random.exponential
  • numpy.random.f
  • numpy.random.gamma
  • numpy.random.geometric
  • numpy.random.gumbel
  • numpy.random.hypergeometric
  • numpy.random.laplace
  • numpy.random.logistic
  • numpy.random.lognormal
  • numpy.random.logseries
  • numpy.random.multinomial
  • numpy.random.multivariate_normal
  • numpy.random.negative_binomial
  • numpy.random.noncentral_chisquare
  • numpy.random.noncentral_f
  • numpy.random.normal
  • numpy.random.pareto
  • numpy.random.poisson
  • numpy.random.power
  • numpy.random.randn
  • numpy.random.rayleigh
  • numpy.random.standard_cauchy
  • numpy.random.standard_exponential
  • numpy.random.standard_gamma
  • numpy.random.standard_normal
  • numpy.random.standard_t
  • numpy.random.triangular
  • numpy.random.vonmises
  • numpy.random.wald
  • numpy.random.weibull
  • numpy.random.zipf

 

 

承接Matlab、Python和C++的编程,机器学习、计算机视觉的理论实现及辅导,本科和硕士的均可,咸鱼交易,专业回答请走知乎,详谈请联系QQ号757160542,非诚勿扰

你可能感兴趣的:(科学计算)