numpy.random.randn()和rand()用法

在python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下。

import numpy as np

1 numpy.random.rand()

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

  • rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
  • dn表格每个维度
  • 返回值为指定维度的array
np.random.randn() # 当没有参数时,返回单个数据
-1.1241580894939212
np.random.randn(2,4)

 

array([[ 0.27795239, -2.57882503,  0.3817649 ,  1.42367345],
       [-1.16724625, -0.22408299,  0.63006614, -0.41714538]])
np.random.randn(4,3,2)
array([[[ 1.27820764,  0.92479163],
        [-0.15151257,  1.3428253 ],
        [-1.30948998,  0.15493686]],
 
       [[-1.49645411, -0.27724089],
        [ 0.71590275,  0.81377671],
        [-0.71833341,  1.61637676]],
 
       [[ 0.52486563, -1.7345101 ],
        [ 1.24456943, -0.10902915],
        [ 1.27292735, -0.00926068]],
 
       [[ 0.88303   ,  0.46116413],
        [ 0.13305507,  2.44968809],
        [-0.73132153, -0.88586716]]])

标准正态分布介绍

  • 标准正态分布—-standard normal distribution
  • 标准正态分布又称为u分布,是以0为均值、以1为标准差的正态分布,记为N(0,1)。

3 numpy.random.randint()

3.1 numpy.random.randint()

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

  • 返回随机整数,范围区间为[low,high),包含low,不包含high
  • 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
  • high没有填写时,默认生成随机数的范围是[0,low)
np.random.randint(1,size=5) # 返回[0,1)之间的整数,所以只有0
array([0, 0, 0, 0, 0])
np.random.randint(1,5) # 返回1个[1,5)时间的随机整数
np.random.randint(-5,5,size=(2,2))
array([[ 2, -1],
       [ 2,  0]])

3.2 numpy.random.random_integers

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

  • 返回随机整数,范围区间为[low,high],包含low和high
  • 参数:low为最小值,high为最大值,size为数组维度大小
  • high没有填写时,默认生成随机数的范围是[1,low]

该函数在最新的numpy版本中已被替代,建议使用randint函数

np.random.random_integers(1,size=5)

 

array([1, 1, 1, 1, 1])

4 生成[0,1)之间的浮点数

  • numpy.random.random_sample(size=None)
  • numpy.random.random(size=None)
  • numpy.random.ranf(size=None)
  • numpy.random.sample(size=None)
未完参考 https://blog.csdn.net/u012149181/article/details/78913167
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值