np.random的随机数函数
np.random的随机数函数(1)
函数 | 说明 |
---|---|
rand(d0,d1,..,dn) | 根据d0‐dn创建随机数数组,浮点数, [0,1),均匀分布 |
randn(d0,d1,..,dn) | 根据d0‐dn创建随机数数组,标准正态分布 |
randint(low[,high,shape]) | 根据shape创建随机整数或整数数组,范围是[low, high) |
seed(s) | 随机数种子, s是给定的种子值 |
np.random.rand
import numpy as npa = np.random.rand(3, 4, 5)a
Out[3]:
array([[[0.28576737, 0.96566496, 0.59411491, 0.47805199, 0.97454449],[0.15970049, 0.35184063, 0.66815684, 0.13571458, 0.41168113],[0.66737322, 0.91583297, 0.68033204, 0.49083857, 0.33549182],[0.52797439, 0.23526146, 0.39731129, 0.26576975, 0.26846021]],[[0.46860445, 0.84988491, 0.92614786, 0.76410349, 0.00283208],[0.88036955, 0.01402271, 0.59294569, 0.14080713, 0.72076521],[0.0537956 , 0.08118672, 0.59281986, 0.60544876, 0.77931621],[0.41678215, 0.24321042, 0.25167563, 0.94738625, 0.86642919]],[[0.36137271, 0.21672667, 0.85449629, 0.51065516, 0.16990425],[0.97507815, 0.78870518, 0.36101021, 0.56538782, 0.56392004],[0.93777677, 0.73199966, 0.97342172, 0.42147127, 0.73654324],[0.83139234, 0.00221262, 0.51822612, 0.60964223, 0.83029954]]])
np.random.randn
b = np.random.randn(3, 4, 5)b
Out[5]:
array([[[ 0.09170952, -0.36083675, -0.18189783, -0.52370155,-0.61183783],[ 1.05285606, -0.82944771, -0.93438396, 0.32229904,-0.85316565],[ 1.41103666, -0.32534111, -0.02202953, 1.02101228,1.59756695],[-0.33896372, 0.42234042, 0.14297587, -0.70335248,0.29436318]],[[ 0.73454216, 0.35412624, -1.76199508, 1.79502353,1.05694614],[-0.42403323, -0.36551581, 0.54033378, -0.04914723,1.15092556],[ 0.48814148, 1.09265266, 0.65504441, -1.04280834,0.70437122],[ 2.92946803, -1.73066859, -0.30184912, 1.04918753,-1.58460681]],[[ 1.24923498, -0.65467868, -1.30427044, 1.49415265,0.87520623],[-0.26425316, -0.89014489, 0.98409579, 1.13291179,-0.91343016],[-0.71570644, 0.81026219, -0.00906133, 0.90806035,-0.914998 ],[ 0.22115875, -0.81820313, 0.66359573, -0.1490853 ,0.75663096]]])
np.random.randint
c = np.random.randint(100, 200, (3, 4))c
Out[9]:
array([[104, 140, 161, 193],[134, 147, 126, 120],[117, 141, 162, 137]])
np.random.seed
随机种子生成器,使下一次生成的随机数为由种子数决定的“特定”的随机数,如果seed中参数为空,则生成的随机数“完全”随机。参考和文档。
np.random.seed(10)np.random.randint(100, 200, (3 ,4))
Out[11]:
array([[109, 115, 164, 128],[189, 193, 129, 108],[173, 100, 140, 136]])np.random.seed(10)np.random.randint(100 ,200, (3, 4))
Out[13]:
array([[109, 115, 164, 128],[189, 193, 129, 108],[173, 100, 140, 136]])
np.random的随机数函数(2)
函数 | 说明 |
---|---|
shuffle(a) | 根据数组a的第1轴(也就是最外层的维度)进行随排列,改变数组x |
permutation(a) | 根据数组a的第1轴产生一个新的乱序数组,不改变数组x |
choice(a[,size,replace,p]) | 从一维数组a中以概率p抽取元素,形成size形状新数组replace表示是否可以重用元素,默认为False |
np.random.shuffle
a = np.random.randint(100, 200, (3, 4))a
Out[15]:
array([[116, 111, 154, 188],[162, 133, 172, 178],[149, 151, 154, 177]])np.random.shuffle(a)a
Out[17]:
array([[116, 111, 154, 188],[149, 151, 154, 177],[162, 133, 172, 178]])np.random.shuffle(a)a
Out[19]:
array([[162, 133, 172, 178],[116, 111, 154, 188],[149, 151, 154, 177]])
可以看到,a发生了变化,轴。
np.random.permutation
b = np.random.randint(100, 200, (3, 4))b
Out[21]:
array([[113, 192, 186, 130],[130, 189, 112, 165],[131, 157, 136, 127]])np.random.permutation(b)
Out[22]:
array([[113, 192, 186, 130],[130, 189, 112, 165],[131, 157, 136, 127]])b
Out[24]:
array([[113, 192, 186, 130],[130, 189, 112, 165],[131, 157, 136, 127]])
可以看到,b没有发生改变。
np.random.choice
c = np.random.randint(100, 200, (8,))c
Out[26]: array([123, 194, 111, 128, 174, 188, 109, 115])np.random.choice(c, (3, 2))
Out[27]:
array([[111, 123],[109, 115],[123, 128]])#默认可以出现重复值np.random.choice(c, (3, 2), replace=False)
Out[28]:
array([[188, 111],[123, 115],[174, 128]])#不允许出现重复值np.random.choice(c, (3, 2),p=c/np.sum(c))
Out[29]:
array([[194, 188],[109, 111],[174, 109]])#指定每个值出现的概率
np.random的随机数函数(3)
函数 | 说明 |
---|---|
uniform(low,high,size) | 产生具有均匀分布的数组,low起始值,high结束值,size形状 |
normal(loc,scale,size) | 产生具有正态分布的数组,loc均值,scale标准差,size形状 |
poisson(lam,size) | 产生具有泊松分布的数组,lam随机事件发生率,size形状 |
u = np.random.uniform(0, 10, (3, 4))u
Out[31]:
array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],[1.31291053, 8.42817933, 6.59036304, 5.95439605],[4.36353698, 3.56250327, 5.87130925, 1.49471337]])n = np.random.normal(10, 5, (3, 4))n
Out[33]:
array([[ 8.17771928, 4.17423265, 3.28465058, 17.2669643 ],[10.00584724, 9.94039808, 13.57941572, 4.07115727],[ 6.81836048, 6.94593078, 3.40304302, 7.19135792]])p = np.random.poisson(2.0, (3, 4))p
Out[35]:
array([[0, 2, 2, 1],[2, 0, 1, 3],[4, 2, 0, 3]])
转载于:https://www.cnblogs.com/sunshinewang/p/8905761.html
np.random的随机数函数相关推荐
- python随机抽样numpy_python numpy之np.random的随机数函数使用介绍
np.random的随机数函数(1) 函数 说明 rand(d0,d1,..,dn) 根据d0‐dn创建随机数数组,浮点数, [0,1),均匀分布 randn(d0,d1,..,dn) 根据d0‐dn ...
- random库 随机数函数
random库 随机数函数 随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性. random库是 使用随机数的python标准库 伪随机数:采用梅森旋 ...
- python random扩展随机数函数getrandbits(k)的使用
今天又是学习python快乐的一天,刚刚学到了扩展随机数函数getrandbits(k),生成一个k比特长的随机整数,但完全不懂是什么意思,那就来试验吧: 首先来试试random.getrandbit ...
- 详述numpy中的np.random.rand()、np.random.randn()、np.random.randint()、np.random.uniform()函数的用法
目录 (一)np.random.rand() (二)np.random.randn() (三)np.random.randint(low,high,size,dtype) (四)np.random.u ...
- python: np.random.rand(); random.randn(); random.randint()
目录 np.random.rand(): np.random.randn(): np.random.randint(): 总结: np.random.rand(): 该函数旨在输出0~1的正态分布随机 ...
- numpy中的随机数函数详解
作者学习中积累总结内容,转载请注明 np.random.* numpy随机数 一.生成器 二.简单随机数 三.分布随机数 四.排列 shuffle() permutation() choice() n ...
- python中rand函数是什么意思_科学网-Python中*和**作用及随机数产生函数 np.random.rand()/numpy.random.unifo...-张伟的博文...
[第一部分] Python中的 * 和 ** ,能够让函数支持任意数量的参数,它们在函数定义和调用中,有着不同的目的! 一. 打包参数 * 的作用:在函数定义中,收集所有位置参数到一个新的元组,并将整 ...
- np.randon模块,numpy.random模块各函数功能详解,分类描述各个函数功能,生成随机数
因为网上很多文章都是在依次列举各个函数的功能,并没有做分类,所以记忆上还有使用上都不方便. 本文旨在将功能相似的函数放在一起,以及对它们进行辨析和区分,便于精准地调用.未完待续 最权威的参考来源:Ra ...
- np.random.seed()函数
np.random.seed()函数用于生成指定随机数. seed()被设置了之后,np,random.random()可以按顺序产生一组固定的数组,如果使用相同的seed()值,则每次生成的随机数都 ...
最新文章
- php 图像函数,PHP图像操作常用函数
- 鲲鹏高校行太原站来袭,两大课程一站式掌握未来潮流
- kafka重置到最新offset偏移量
- 【自动驾驶】4.分布式实时通信——DDS技术
- Atom中安装atom-terminal插件来打开命令行运行vue项目
- x86已安装该产品 剑灵vcredist_MySQL Server v5.7正式版(附安装和配置数据库教程)
- sql server数据库实现保留指定位数小数的函数
- SQL Server 2008 阻止保存要求重新创建表的更改问题
- slack通知本地服务器_通过构建自己的Slack App学习无服务器
- python选定区域设置边框_Python教程:巧用openpyxl为指定区域设置边框为粗匣框线...
- 卡巴斯基正版下载及授权激活(4.8号更新)
- 关于使用DFS,BFS的一些思考总结
- VC对密码加密和解密函数
- SQL Server 2008 索引超出了数组界限
- win10绿联usb转串口_USB转串口DB9驱动安装与设置方法
- 《深入理解Windows操作系统》笔记5
- Rant ?? 我爱每一片绿叶
- win10开机慢怎么解决_win10开机速度变慢的解决方法教程
- 腾讯云「轻量应用服务器」开放公测!
- 同花顺服务器维护,同花顺云端服务器