python:numpy.random中部分函数列表(rand,randint,randn,normal随机生成数据)

使用numpy.random里的函数随机生成数据

 

目录

1.numpy.random.rand(d0, d1, ..., dn)     生成服从均匀分布[0,1)的随机数、或指定size的随机数组

         2.numpy.random.randint(low, high=None, size=None, dtype=int)  生成服从离散均匀分布[low,high)的随机数、或指定size的随机数组

3. numpy.random.randn(d0, d1, ..., dn)   生成服从均值0方差1的正态分布的随机数、或指定size的随机数组

4. np.random.normal(loc=0.0scale=1.0size=None)      生成服从正态(高斯)分布的随机数(可自行选择均值、方差以及输出的size)


1.numpy.random.rand()    

1)用法: numpy.random.rand(d0, d1, ..., dn)   

2)作用从均匀分布[0,1)中抽取样本, 生成指定形状的数组,数组里的数是服从[0,1)均匀分布的随机数

3)参数

     d0, d1, …, dn  :数组的形状(The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.)
4)输出

    给定形状的数组 (ndarray, shape (d0, d1, ..., dn))

5)例子:随机生成一个3行2列的数组(服从[0,1)均匀分布)

#随机生成一个3行2列的数组(服从[0,1)均匀分布)
np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random

 

2.numpy.random.randint()

1)用法: numpy.random.randint(low, high=None, size=None, dtype=int)

2)作用从离散均匀分布[low,high)中抽取样本,生成一个随机整数、或者由随机数组成的数组

3)参数

    low, high  (范围区间为[low,high))      ( If high is None (the default), then results are from [0, low).)

    size:数组的形状(The dimensions of the returned array)

    dtype(默认为int)

4)输出给定形状的随机数组或一个随机整数

      int or ndarray of ints.   (size-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.)

      1) 如果指定size(数组的形状),输出指定size的随机数组
      2) 如果没有指定size(数组的形状),则输出一个随机数

5)例子:随机生成一个2行4列的数组(数组形状size=(2, 4),数组里的数在0~4之间)

#随机生成一个2行4列的数组(数组形状size=(2, 4),随机数在0~4之间)
Generate a 2 x 4 array of ints between 0 and 4, inclusive:
>>>
np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1], # random
       [3, 2, 2, 0]])

 

3. numpy.random.randn()

1)用法: numpy.random.randn(d0, d1, ..., dn)

2)作用从均值0方差1的正态分布中抽取样本生成一个随机数、或者指定形状的随机数组

3)参数

     d0, d1, …, dn  :数组的形状(The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.)
4)输出

       ndarray or float  【 A (d0, d1, ..., dn)-shaped array of floating-point samples from the standard normal distribution, or a single such float if no parameters were supplied.】

      给定形状的随机数组或一个随机数

5)例子

Examples

>>>
np.random.randn()
2.1923875335537315  # random

Two-by-four array of samples from N(3, 6.25):
>>>
3 + 2.5 * np.random.randn(2, 4)
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],   # random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]])  # random

   

4. np.random.normal()

1)用法numpy.random.normal(loc=0.0scale=1.0size=None)

2)作用:生成服从正态(高斯)分布的随机数(可自行选择均值、方差以及输出的size)

3)参数

     loc:  均值( Mean (“centre”) of the distribution.)

     scale: 标准差 (Standard deviation (spread or “width”) of the distribution. Must be non-negative.)

     size:  输出的形状 (int or tuple of ints, optional)
4)输出

      ndarray or scalar 数组或标量(Drawn samples from the parameterized normal distribution.)

5)例子: 生成均值为2,标准差为2.5,2行4列的随机数

Two-by-four array of samples from N(3, 6.25):

>>>
np.random.normal(3, 2.5, size=(2, 4))
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],   # random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]])  # random

 

参考:

numpy-random函数

np.random用法

Python Numpy随机数总结——numpy.random.rand/randn/randint/random/uniform/seed

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39450145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值