1.numpy.random.rand()
用法是:numpy.random.rand(d0,d1,…dn)
以给定的形状创建一个数组,并在数组中加入在[0,1]之间均匀分布的随机样本。
用法及实现

>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random[ 0.37601032,  0.25528411],  #random[ 0.49313049,  0.94909878]]) #random

>>>np.random.rand(5)
array([ 0.26677034,  0.01680242,  0.5164905 ,  0.70920141,  0.30438513])

2.numpy.random.randn()
用法是:numpy.random.rand(d0,d1,…dn)
以给定的形状创建一个数组,数组元素来符合标准正态分布N(0,1)
若要获得一般正态分布则可用sigma * np.random.randn(…) + mu进行表示
用法及实现

>>> a = np.random.randn(2, 4)
>>> a
array([[-0.29188711,  0.76417681,  1.00922644,  0.34169581],[-0.3652463 , -0.9158214 ,  0.34467129, -0.31121017]])
>>> b = np.random.randn(2)
>>> b
array([ 0.37849173,  1.14298464])

3.numpy.random.randint()
用法是:numpy.random.randint(low,high=None,size=None,dtype)
生成在半开半闭区间[low,high)上离散均匀分布的整数值;若high=None,则取值区间变为[0,low)
用法及实现
high=None的情形

>>> a = np.random.randint(2, size=10)
>>> a
array([0, 1, 0, 1, 1, 0, 1, 0, 0, 1])
>>> b = np.random.randint(1, size=10)
>>> b
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> c =  np.random.randint(5, size=(2, 4))
>>> c
array([[3, 4, 3, 3],[3, 0, 0, 1]])

high≠None

d = np.random.randint(2,high=6,size=(2,4))
>>> d
array([[5, 2, 4, 2],[4, 3, 5, 4]])

4.numpy.random.random_integers()
用法是: numpy.random.random_integers(low,high=None,size=None)
生成闭区间[low,high]上离散均匀分布的整数值;若high=None,则取值区间变为[1,low]
用法及实现
high=None的情形

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

high≠None的情形

>>> c =  np.random.random_integers(6,high=8,size=(3,2))
>>> c
array([[7, 8],[7, 8],[8, 8]])

此外,若要将【a,b】区间分成N等分,也可以用此函数实现
a+(b-a)*(numpy.random.random_integers(N)-1)/(N-1)

5.numpy.random_sanmple()
用法是: numpy.random.random_sample(size=None)
以给定形状返回[0,1)之间的随机浮点数
用法及实现

>>> np.random.random_sample()
0.2982524530687424
>>> np.random.random_sample((5,))
array([ 0.47989216,  0.12580015,  0.99624494,  0.14867684,  0.56981553])
>>> np.random.random_sample((2,5))
array([[ 0.00659559,  0.45824325,  0.13738623,  0.60766919,  0.39234638],[ 0.6914948 ,  0.92461145,  0.43289058,  0.63093292,  0.06921928]])

其他函数,numpy.random.random() ;numpy.random.ranf()
numpy.random.sample()用法及实现都与它相同

6.numpy.random.choice()
用法是: numpy.random.choice(a,size=None,replace=True,p=None)
若a为数组,则从a中选取元素;若a为单个int类型数,则选取range(a)中的数
replace是bool类型,为True,则选取的元素会出现重复;反之不会出现重复
p为数组,里面存放选到每个数的可能性,即概率
用法及实现

>>>a =  np.random.choice(5, 3)
>>> a
array([4, 3, 1])
>>>b =  np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
>>> b
array([2, 3, 3], dtype=int64)
>>> c =  np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
>>> c
array([3, 2, 0])

转载于:https://www.cnblogs.com/mlan/p/8179028.html

Python的numpy库中rand(),randn(),randint(),random_integers()的使用相关推荐

  1. Python的numpy库中rand(),randn(),randint(),random_integers()等random系函数的使用

    在使用Python进行数据处理时,往往需要用到大量的随机数据,那如何构造这么多数据呢?Python的第三方库numpy库中提供了random函数来实现这个功能. 本文将根据官方文档以及其他博友的博客一 ...

  2. Python:numpy库中的一些函数简介、使用方法之详细攻略

    Python:numpy库中的一些函数简介.使用方法之详细攻略 目录 numpy库中的一些函数简介.使用方法 1.np.concatenate() 1.1.函数案例 1.2.函数用法 numpy库中的 ...

  3. [转载] Python里面numpy库中zeros()的一些问题

    参考链接: Python中的numpy.zeros Python里面numpy库中zeros函数的一些问题 定义 本文记录了在使用numpy库中的zeros函数时遇到的一些问题 定义 用法:zeros ...

  4. python 的numpy库中的mean()函数用法介绍

    这篇文章主要介绍了python 的numpy库中的mean()函数用法介绍,具有很好对参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 mean() 函数定义: 2 mean()函数功能: 求取均 ...

  5. Python:Numpy库中的invert()函数的用法

    Numpy库中的invert()函数的用法 官方解释: Compute bit-wise inversion, or bit-wise NOT, element-wise. Computes the ...

  6. matlab中randint函数用法,matlab中rand randn randint函数的区别

    matlab中rand函数是产生0到1的随机分布 matlab中randn函数是产生标准正态分布 randint是产生整数随机数,默认为0和1 %%%%%%%%%%%rand%%%%%%%%%%%%% ...

  7. python 的numpy库中的mean()函数用法

    1. mean() 函数定义: numpy. mean ( a,  axis=None,  dtype=None,  out=None,  keepdims=<class numpy._glob ...

  8. python中mean的用法_python 的numpy库中的mean()函数用法介绍

    1. mean() 函数定义: numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)[source] Compute the arithm ...

  9. python average函数怎么用_python 的numpy库中的mean()函数用法介绍

    1. mean() 函数定义: numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)[source] Compute the arithm ...

最新文章

  1. tensorflow cuda 对应版本
  2. 问题解决:无法获得锁 /var/lib/dpkg/lock
  3. Linux(CentOS)同步时间
  4. 《四世同堂》金句摘抄(三)
  5. 29. ExtJs - Struts2 整合(1) - 登录页面
  6. 把握不好数组边界的危害(记洛谷P1789题RE+WA的经历,Java语言描述)
  7. 360网络自动化运维
  8. Javascript实现简单的选项卡
  9. javascript图片库威力增强版
  10. SpringBoot2整合Shiro实现权限管理
  11. mongodb oplog java_MongoDB oplog 深入剖析
  12. 学生管理系统Element UI版
  13. Convex Clustering(凸聚类)
  14. IOS13图标尺寸_7大原则,带你设计出更优秀的图标
  15. Pandas的panel结构
  16. 服务器项目迁移本地,云服务器迁移本地
  17. 得中原者得天下!2018中国软件生态大会郑州站火爆进行
  18. 【区块链108将】微数链林道坤:区块链有助于更好的发挥大数据价值
  19. 支付宝当面付打shang系统源码分享
  20. 硅谷一万清华人,为何打不过印度人

热门文章

  1. python画-如何用Python画各种著名数学图案 | 附图+代码
  2. python在线课程价格-杭州python课程价格
  3. python详细安装教程3.8-python3.8下载及安装步骤详解
  4. python科学计算基础教程pdf下载-用Python做科学计算 高清晰PDF
  5. python编程视频-Python开发视频百度就得看这个!
  6. python表白源代码-python七夕浪漫表白源码
  7. python主要用途-学习Python的三大主要用途
  8. python学精通要多久-精通python需要多久
  9. python代码块-Python 代码块
  10. python三维图-python 三维坐标图