numpy.random模块常用函数解析
numpy.random模块中常用函数解析
1. numpy.random.rand(d0, d1, ..., dn)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)
按照给定形状产生一个多维数组,每个元素在0到1之间
注意: 这里定义数组形状时,不能采用tuple
import numpy as np
np.random.rand(2, 3)
array([[ 0.44590044, 0.36234046, 0.51609462],
[ 0.45733218, 0.80836224, 0.31628453]])
2. numpy.random.randn(d0, d1, ..., dn)
generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate “normal” distribution of mean 0 and variance 1
按照给定形状产生一个多维数组,数组中的元素服从标准正态分布
若要产生服从N(mu, sigma^2)分布的样本, 使用sigma * np.random.randn(...) + mu
例如产生 2 * 4 samples from N(3, 6.25):
2.5 * np.random.randn(2, 4) + 3
array([[ 2.90478558, 6.05670578, 6.21539068, 3.3955507 ],
[ 0.11594363, 3.17433693, 5.35625762, 1.4824643 ]])
3. numpy.random.randint(low, high=None, size=None, dtype='l')
Return random integers from low (inclusive) to high (exclusive).
按照给定的形状和范围产生随机整数
np.random.randint(0, 10, size=(2, 4))
array([[2, 7, 2, 1],
[3, 2, 4, 1]])
4. numpy.random.random_integers(low, high=None, size=None)
Random integers of type np.int between low and high, inclusive.
np.random.random_integers(1, 10, size=(2, 5))
array([[ 3, 3, 8, 4, 5],
[ 2, 7, 8, 10, 2]])
5. numpy.random.random_sample(size=None)
6. numpy.random.random(size=None)
7. numpy.random.ranf(size=None)
8. numpy.random.sample(size=None)
Return random floats in the half-open interval [0.0, 1.0).
以上四种方式都是生成[0,1)之间的浮点数
To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:
(b - a) * random_sample() + a
import numpy as np
print('random_sample:\n', np.random.random_sample((2, 3)))
print('random:\n', np.random.random((2, 3)))
print('ranf:\n', np.random.ranf((2, 3)))
print('sample:\n', np.random.sample((2, 3)))
random_sample:
[[ 0.87996593 0.2706701 0.42158973]
[ 0.91952234 0.99470239 0.07363656]]
random:
[[ 0.44572326 0.23595379 0.1061901 ]
[ 0.48362249 0.4270327 0.12281262]]
ranf:
[[ 0.07180002 0.25542854 0.55630057]
[ 0.38181471 0.91512916 0.04020929]]
sample:
[[ 0.80390231 0.0024602 0.95974309]
[ 0.32902852 0.62796713 0.42254831]]
9. numpy.random.choice(a, size = None, replace=True, p=None)
从给定的一维数组中生成随机数
如a是一个int数, 则产生的数组的元素都在np.arange(a)中
如a是一个1-D array-like, 则产生的数组的元素都在a中
print('1:\n', np.random.choice(5))
print('2:\n', np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))
print('3:\n', np.random.choice(5, (2, 3)))
print('4:\n', np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))
1:
4
2:
[1 4]
3:
[[2 1 4]
[0 2 3]]
4:
[[3 6 1 6 1]
[3 3 3 3 1]]
10. numpy.random.seed(None)
设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
np.random.seed(2)
np.random.rand(2, 3)
array([[ 0.4359949 , 0.02592623, 0.54966248],
[ 0.43532239, 0.4203678 , 0.33033482]])
np.random.seed(2)
np.random.rand(2, 3)
array([[ 0.4359949 , 0.02592623, 0.54966248],
[ 0.43532239, 0.4203678 , 0.33033482]])
np.random.rand(2, 3)
array([[ 0.20464863, 0.61927097, 0.29965467],
[ 0.26682728, 0.62113383, 0.52914209]])
numpy.random模块常用函数解析的更多相关文章
- python重要的第三方库pandas模块常用函数解析之DataFrame
pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...
- pandas模块常用函数解析之Series(详解)
pandas模块常用函数解析之Series 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网 ...
- random模块常用函数
random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...
- numpy.random之常用函数
在实际开发中,我们经常会使用随机函数,比如交叉验证,构造测试数据等.下面,是我常用的几个生成随机样本的函数: 1,rand(n1,n2,…,nn) 每一维度都是[0.0,1.0)半闭半开区间上的随机分 ...
- numpy模块常用函数解析
https://blog.csdn.net/lm_is_dc/article/details/81098805 numpy模块以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter note ...
- 操作 numpy 数组的常用函数
操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...
- AR模块常用函数
--AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...
- $python正则表达式系列(2)——re模块常用函数
本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...
- numpy.random模块用法小结
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...
随机推荐
- mac系统下的常用命令
这是我日常在mac下记录的一些常用终端命令: 1 java 2 javac 3 exit 4 /Users/lianxumac/Desktop/apktool1.5.2/反编译 ; exit; 5 / ...
- 实现SpringBoot登录
SpringBoot登录 https://www.cnblogs.com/jiekzou/p/9303871.html 通过前面10篇文章的学习,相信我们对SpringBoot已经有了一些了解,那么如 ...
- 在Android.mk文件中输出打印消息 (转载)
转自:http://blog.csdn.net/xiaibiancheng/article/details/8479694 在进行Android NDK的开发当中有时想看看Android.mk文件当中 ...
- bzoj 4297: [PA2015]Rozstaw szyn【瞎搞】
从叶子往上先拓扑一下,建立虚拟root,从root开始dfs.注意到每个点的最优取值一定是一个区间(中位数区间),从儿子区间推出父亲区间即可 #include<iostream> #inc ...
- P4576 [CQOI2013]棋盘游戏
传送门 很显然,除非白子和黑子相邻,否则必然是黑子获胜虽然我并没有看出来 那么现在对黑子来说它要尽可能快的赢,对白子它要多苟一会儿 然后就是这个叫做对抗搜索的东西了 //minamoto #inclu ...
- Linux day01(二)虚拟机快照和克隆的用法介绍
一:快照 优点:运行虚拟机后不用担心系统会被弄崩溃了,点击快照会立即恢复到初始状态 缺点:回滚会带来数据的丢失,所以要考虑数据恢复的成本和找回数据时进行操作的成本 1. 在导航栏中找虚拟机快照的小图标 ...
- Pycharm初始创建项目和环境搭建(解决aconda库文件引入不全等问题)
1.新建工程 1.选择新建一个Pure Python项目,新建项目路径可以在Location处选择. 2.Project Interpreter部分是选择新建项目所依赖的python库,第一个选项会在 ...
- ATX 学习 (三)-atxserver2-android-provider
服务端代码 代码clone到本地,搭好相应环境(怎么搭的这里就不介绍了,很好搭的哈)一般库首先查看main.py文件,debug模式开始运行 一开始就是没接触过的tornado.ioloop,有点偏底 ...
- 汇编程序52:实验15 安装新的int9中断例程
assume cs:code ;重写int9中断例程,当按住a后松开,便会产生满屏A stack segment dw dup() stack ends code segment start: mov ...
- JavaScript编程艺术-第8章-8.6.1-显示“缩略词语表”
8.6.1-显示“缩略词语表” ***代码亲测可用*** HTML: JS: ***end***