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 np
a = 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]])
np.random的随机数函数的更多相关文章
- np.random.rand均匀分布随机数和np.random.randn正态分布随机数函数使用方法
np.random.rand用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me 生成特定形状下[0,1)下的均匀分布随机数 np.random.rand(a1,a2,a3...)生成形状为( ...
- np.random.random()系列函数
1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...
- np.random.random()函数 参数用法以及numpy.random系列函数大全
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...
- 【转】np.random.random()函数 参数用法以及numpy.random系列函数大全
转自:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.random((1000, 20) ...
- np.random 系列函数
1 random() # 产生区间 [0, 1) 均匀分布的浮点数样本值 np.random.seed(42) 2 rand(d0, d1, ..., dn) # 产生区间 [0, 1) 均 ...
- 统计学习方法 | 第1章 统计学习方法概论 | np.random.rand()函数
np.random.rand()函数 语法: np.random.rand(d0,d1,d2……dn) 注:使用方法与np.random.randn()函数相同 作用: 通过本函数可以返回一个或一组服 ...
- Python之np.random.permutation()函数的使用
官网的解释是:Randomly permute a sequence, or return a permuted range. 即随机排列序列,或返回随机范围.我的理解就是返回一个乱序的序列.下面通过 ...
- 怎么理解np.random.seed()?
在使用numpy时,难免会用到随机数生成器.我一直对np.random.seed(),随机数种子搞不懂.很多博客也就粗略的说,利用随机数种子,每次生成的随机数相同. 我有两个疑惑:1, 利用随机数种子 ...
- Python基础系列讲解——random模块随机数的生成
随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的random模块提供了生成随机数的方法,使用这些方法时需要导入ran ...
随机推荐
- nginx和php-fpm的启停和配置
一.nginx的启停 (1) 启动nginx /etc/init.d/nginx start (2) 停止nginx /etc/init.d/nginx stop (3) 重启nginx /etc/i ...
- centos7 rabbitmq 安装
http://www.rabbitmq.com/install-rpm.html Overview rabbitmq-server is included in Fedora. However, th ...
- day5-os、sys模块
一.概述 开发运维相关支撑系统现今已成为Devops下的一大热门领域,Python在这方面也有着自己独到的优势.这类场景以及其他一些场景下,需要调用一些操作系统的接口,这就涉及到今天要讲述的OS模块和 ...
- h1标签
h1标签一.每个网页只能拥有一个<H1>标签 H2,H3,H4可以有多个...但多个H1造成的后果是搜索引擎不知道你这个页面哪个标题内容最重要,会淡化这个页面的标题和关键词.H1用得好的话 ...
- 添加resx文件过程笔记
为了测试资源文件,今天上午耗掉了.终于实现了,原来是要按照它自动建文件夹,不可以手动建.下图 见其他语言的时候一定要:Mytest.zh-CN.resx 这样,加上后缀名字 代码中获取资源 http: ...
- C++复习2.软件开发知识小节
高质量的软件开发 1.满足正确性,健壮性,可靠性,性能,易用性,清晰性,安全性,兼容性,扩展性,可移植性等等来评价软件的质量. 2.没有错误的程序世间难求,任何一个程序,无论他多么的小,总是存在着错误 ...
- PHP Socket(套接字连接)扩展简介和使用方法
PHP socket扩展是基于流行的BSD sockets,实现了和socket通讯功能的底层接口,它可以和客户端一样当做一个socket服务器. 使用这些函数时请注意,虽然他们中有很多和C函数同名的 ...
- LINUX文件的权限
一.权限设定的意义:系统最底层安全设定方法之,保证文件可以被可用的用户做相应操作. 二.文件权限的查看(alias) 命令:ls ls -l file ## 查看文件属性 ls -ld mkdir ...
- ASP.NET MVC3关于生成纯静态后如何不再走路由直接访问静态页面--收藏没测
要解决这个问题,我们需要先了解ASP.NET应用程序的生命周期,先看下面作者整理的一张图片: 从图中我们可以清楚的看到:通用IIS访问应用程序时,每次的单个页面URL访问时,都会先经过HttpAppl ...
- PostgreSQL资料汇总
慢慢积累一些有用的资料: https://postgrespro.ru