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模块常用函数解析的更多相关文章

  1. python重要的第三方库pandas模块常用函数解析之DataFrame

    pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...

  2. pandas模块常用函数解析之Series(详解)

    pandas模块常用函数解析之Series 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网 ...

  3. random模块常用函数

    random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...

  4. numpy.random之常用函数

    在实际开发中,我们经常会使用随机函数,比如交叉验证,构造测试数据等.下面,是我常用的几个生成随机样本的函数: 1,rand(n1,n2,…,nn) 每一维度都是[0.0,1.0)半闭半开区间上的随机分 ...

  5. numpy模块常用函数解析

    https://blog.csdn.net/lm_is_dc/article/details/81098805 numpy模块以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter note ...

  6. 操作 numpy 数组的常用函数

    操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...

  7. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  8. $python正则表达式系列(2)——re模块常用函数

    本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...

  9. numpy.random模块用法小结

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...

随机推荐

  1. json返序列化

    ASP.NET中JSON的序列化和反序列化 http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html 迟来的Json反序列化 ht ...

  2. 符号修饰与函数签名、extern “C”(转载)

    转自:http://www.cnblogs.com/monotone/archive/2012/11/16/2773772.html 参考资料: <程序员的自我修养>3.5.3以及3.5. ...

  3. bzoj 4326: NOIP2015 运输计划【树链剖分+二分+树上差分】

    常数巨大,lg上开o2才能A 首先预处理出运输计划的长度len和lca,然后二分一个长度w,对于长度大于w的运输计划,在树上差分(d[u]+1,d[v]+1,d[lca]-2),然后dfs,找出所有覆 ...

  4. less新手入门(五)—— CssGuards、循环、合并

    九. CssGuards 警卫也可以应用于css选择器,这是一种语法糖,用于声明mixin,然后立即调用它. 例如,在1.5.0之前,您必须这样做 .my-optional-style() when ...

  5. less新手入门(三) 作为函数使用的Mixin、@import 导入指令 、@import 导入选项

    五.作为函数使用的Mixin 从mixin返回变量 在mixin中定义的所有变量都是可见的,并且可以在调用者的作用范围中使用(除非调用者用相同的名称定义它自己的变量). .mixin(){ @widt ...

  6. [SHOI2013]超级跳马

    题目描述 现有一个n 行m 列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘.试求跳法种数mod 30011. 输入输出格式 输入格式: ...

  7. 树形DP UVA 1292 Strategic game

    题目传送门 /* 题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择 树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值 */ /***** ...

  8. Android 性能优化(22)*性能工具之「Hierarchy Viewer」 Hierarchy Viewer Walkthrough

    Hierarchy Viewer Walkthrough 1.In this document Prerequisites Setting the ANDROID_HVPROTO variable W ...

  9. 网上商城 Incorrect datetime value: '' for column 'ordertime' at row 1

    今天在做商城项目的[提交订单]功能的时候,向数据库插入数据报错:Incorrect datetime value: '' for column 'ordertime' at row 1 public ...

  10. [ POI 2005 ] Bank Notes

    \(\\\) Description 给出 \(N\) 种货币的面值 \(b_i\) 和个数 \(c_i\) ,求最少需要用多少个硬币凑出 \(Q\) 元钱,并输出任意一种方案. \(n\le 200 ...