python numpy 常用随机数的产生方法的实现


Posted in Python onAugust 21, 2019

numpy 中 的random模块有多个函数用于生成不同类型的随机数,常见的有 uniform、rand、random、randint、random_interges

下面介绍一下各自的用法

1、np.random.uniform的用法

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

作用:可以生成[low,high)中的随机数,可以是单个值,也可以是一维数组,也可以是多维数组

参数介绍:

  • low :float型,或者是数组类型的,默认为0
  • high:float型,或者是数组类型的,默认为1
  • size:int型,或元组,默认为空
In[1]: import numpy as np

In[2]: np.random.uniform() # 默认为0到1
Out[2]: 0.827455693512018

In[3]: np.random.uniform(1,5)
Out[3]: 2.93533586182789

In[4]: np.random.uniform(1,5,4) #生成一维数组
Out[4]: array([ 3.18487512, 1.40233721, 3.17543152, 4.06933042])

In[5]: np.random.uniform(1,5,(4,3)) #生成4x3的数组
Out[5]: 
array([[ 2.33083328, 1.592934 , 2.38072  ],
    [ 1.07485686, 4.93224857, 1.42584919],
    [ 3.2667912 , 4.57868281, 1.53218578],
    [ 4.17965117, 3.63912616, 2.83516143]])

In[6]: np.random.uniform([1,5],[5,10]) 
Out[6]: array([ 2.74315143, 9.4701426 ])

2、np.random.random_sample的用法

和np.random.random作用一样

random_sample(size=None)

- 作用:返回[0,1)之间的浮点型随机数,通过size控制返回的形状

np.random.random_sample()
  0.47108547995356098
type(np.random.random_sample())
  <type 'float'>
np.random.random_sample((5,))
  array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])

  Three-by-two array of random numbers from [-5, 0):

5 * np.random.random_sample((3, 2)) - 5
  array([[-3.99149989, -0.52338984],
      [-2.99091858, -0.79479508],
      [-1.23204345, -1.75224494]])

3、np.random.rand的用法

rand(d0, d1, …, dn)

作用:返回[0,1)内的浮点数,输入的d0,d1…dn代表维度信息,没有输入时,则返回[0,1)内的一个随机值

In[15]: np.random.rand()
Out[15]: 0.9027797355532956

In[16]:np.random.rand(3,3)
Out[16]: 
array([[ 0.47507608, 0.64225621, 0.9926529 ],
    [ 0.95028412, 0.18413813, 0.91879723],
    [ 0.89995217, 0.42356103, 0.81312942]])

In[17]: np.random.rand(3,3,3)
Out[17]: 
array([[[ 0.30295904, 0.76346848, 0.33125168],
    [ 0.77845927, 0.75020602, 0.84670385],
    [ 0.2329741 , 0.65962263, 0.93239286]],

    [[ 0.24575304, 0.9019242 , 0.62390674],
    [ 0.43663215, 0.93187574, 0.75302239],
    [ 0.62658734, 0.01582182, 0.66478944]],

    [[ 0.22152418, 0.51664503, 0.41196781],
    [ 0.47723318, 0.19248885, 0.29699868],
    [ 0.11664651, 0.66718804, 0.39836448]]])

4、np.random.randint的用法

randint(low, high=None, size=None, dtype='l')

作用:生成整型随机数,可以是单个随机数,也可以是多维的随机数构成的数组

参数介绍

  • low:int 型,随机数的下限
  • high:int 型,默认为空,随机数的上限,当此值为空时,函数生成[0,low)区间内的随机数
  • size:int、或ints、或元组,指明生成的随机数的类型
  • dtype:可选'int' ,'int32',默认为'l'
In[7]: np.random.randint(4)
Out[7]: 1

In[8]: np.random.randint(4,size=4)
Out[8]: array([2, 2, 2, 0])

In[9]: np.random.randint(4,10,size=6)
Out[9]: array([7, 9, 7, 8, 6, 9])

np.random.randint(4,10,size=(2,2),dtype='int32')
Out[10]: 
array([[7, 4],
    [6, 9]])

5、np.random.random_integers的用法

random_integers(low, high=None, size=None)

和randint的用法较为相似,区别在于[low,high]

的右边界能够取到,且改函数即将被抛弃,可以使用

np.random.randint(low,high+1)进行代替

总结:随机数可以分为两大类,一类是浮点型的,常以np.random.uniform为代表,np.random.rand,np.random.radnom和np.random.random_simple可以看作是np.random.uniform的特例;另一类是整数型的,以np.random.randint为代表,也有np.random.random_integers 但是后者将被前者取代

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
PHP webshell检查工具 python实现代码
Sep 15 Python
Python中的包和模块实例
Nov 22 Python
python列出目录下指定文件与子目录的方法
Jul 03 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
Jun 13 Python
对比Python中__getattr__和 __getattribute__获取属性的用法
Jun 21 Python
python数据抓取分析的示例代码(python + mongodb)
Dec 25 Python
Python3爬虫学习入门教程
Dec 11 Python
程序员的七夕用30行代码让Python化身表白神器
Aug 07 Python
Python 词典(Dict) 加载与保存示例
Dec 06 Python
python设置 matplotlib 正确显示中文的四种方式
May 10 Python
pandas取dataframe特定行列的实现方法
May 24 Python
Python 实现定积分与二重定积分的操作
May 26 Python
在django模板中实现超链接配置
Aug 21 #Python
python爬虫 批量下载zabbix文档代码实例
Aug 21 #Python
Django 在iframe里跳转顶层url的例子
Aug 21 #Python
Python产生一个数值范围内的不重复的随机数的实现方法
Aug 21 #Python
django写用户登录判定并跳转制定页面的实例
Aug 21 #Python
Django自定义模板过滤器和标签的实现方法
Aug 21 #Python
扩展Django admin的list_filter()可使用范围方法
Aug 21 #Python
You might like
PHP SPL标准库之接口(Interface)详解
2015/05/11 PHP
PHP生成制作验证码的简单实例
2016/06/12 PHP
php识别翻转iphone拍摄的颠倒图片
2018/05/17 PHP
laravel添加前台跳转成功页面示例
2019/10/22 PHP
js中的异常处理try...catch使用介绍
2013/09/21 Javascript
原生js实现类似弹窗抖动效果
2015/04/02 Javascript
学习JavaScript设计模式(封装)
2015/11/26 Javascript
Javascript页面跳转常见实现方式汇总
2015/11/28 Javascript
Bootstrap入门书籍之(一)排版
2016/02/17 Javascript
浅析函数声明和函数表达式——函数声明的声明提前
2016/05/03 Javascript
高效Web开发的10个jQuery代码片段
2016/07/22 Javascript
详解Vue 事件修饰符capture 的使用
2017/12/29 Javascript
在vue中使用css modules替代scroped的方法
2018/03/10 Javascript
vue axios请求拦截实例代码
2018/03/29 Javascript
微信小程序日历/日期选择插件使用方法详解
2018/12/28 Javascript
Vue使用axios引起的后台session不同操作
2020/08/14 Javascript
[52:15]2014 DOTA2国际邀请赛中国区预选赛5.21 HGT VS LGD-GAMING
2014/05/23 DOTA
python的unittest测试类代码实例
2017/12/07 Python
Python语言生成水仙花数代码示例
2017/12/18 Python
使用python3实现操作串口详解
2019/01/01 Python
Python分析彩票记录并预测中奖号码过程详解
2019/07/09 Python
pytorch 实现查看网络中的参数
2020/01/06 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
2020/04/08 Python
美国著名的女性内衣零售商:Frederick’s of Hollywood
2018/02/24 全球购物
Europcar美国/加拿大:预订汽车或卡车租赁服务
2018/11/13 全球购物
编写函数,将一个3*3矩阵转置
2013/10/09 面试题
大型活动策划方案
2014/01/12 职场文书
前处理班长职位说明书
2014/03/01 职场文书
元旦促销方案
2014/03/15 职场文书
贯彻学习两会心得体会范文
2014/03/17 职场文书
大学毕业生个人总结
2015/02/28 职场文书
重阳节活动主持词
2015/07/04 职场文书
消防演习感想
2015/08/10 职场文书
《成长的天空》读后感3篇
2019/12/06 职场文书
python如何做代码性能分析
2021/04/26 Python
Python IO文件管理的具体使用
2022/03/20 Python