经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » NumPy » 查看文章
python numpy之np.random的随机数函数使用介绍
来源:jb51  时间:2019/10/8 9:03:02  对本文有异议

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

  1. import numpy as np
  2.  
  3. a = np.random.rand(3, 4, 5)
  4.  
  5. a
  6. Out[3]:
  7. array([[[0.28576737, 0.96566496, 0.59411491, 0.47805199, 0.97454449],
  8. [0.15970049, 0.35184063, 0.66815684, 0.13571458, 0.41168113],
  9. [0.66737322, 0.91583297, 0.68033204, 0.49083857, 0.33549182],
  10. [0.52797439, 0.23526146, 0.39731129, 0.26576975, 0.26846021]],
  11.  
  12. [[0.46860445, 0.84988491, 0.92614786, 0.76410349, 0.00283208],
  13. [0.88036955, 0.01402271, 0.59294569, 0.14080713, 0.72076521],
  14. [0.0537956 , 0.08118672, 0.59281986, 0.60544876, 0.77931621],
  15. [0.41678215, 0.24321042, 0.25167563, 0.94738625, 0.86642919]],
  16.  
  17. [[0.36137271, 0.21672667, 0.85449629, 0.51065516, 0.16990425],
  18. [0.97507815, 0.78870518, 0.36101021, 0.56538782, 0.56392004],
  19. [0.93777677, 0.73199966, 0.97342172, 0.42147127, 0.73654324],
  20. [0.83139234, 0.00221262, 0.51822612, 0.60964223, 0.83029954]]])

np.random.randn

  1. b = np.random.randn(3, 4, 5)
  2.  
  3. b
  4. Out[5]:
  5. array([[[ 0.09170952, -0.36083675, -0.18189783, -0.52370155,
  6. -0.61183783],
  7. [ 1.05285606, -0.82944771, -0.93438396, 0.32229904,
  8. -0.85316565],
  9. [ 1.41103666, -0.32534111, -0.02202953, 1.02101228,
  10. 1.59756695],
  11. [-0.33896372, 0.42234042, 0.14297587, -0.70335248,
  12. 0.29436318]],
  13.  
  14. [[ 0.73454216, 0.35412624, -1.76199508, 1.79502353,
  15. 1.05694614],
  16. [-0.42403323, -0.36551581, 0.54033378, -0.04914723,
  17. 1.15092556],
  18. [ 0.48814148, 1.09265266, 0.65504441, -1.04280834,
  19. 0.70437122],
  20. [ 2.92946803, -1.73066859, -0.30184912, 1.04918753,
  21. -1.58460681]],
  22.  
  23. [[ 1.24923498, -0.65467868, -1.30427044, 1.49415265,
  24. 0.87520623],
  25. [-0.26425316, -0.89014489, 0.98409579, 1.13291179,
  26. -0.91343016],
  27. [-0.71570644, 0.81026219, -0.00906133, 0.90806035,
  28. -0.914998 ],
  29. [ 0.22115875, -0.81820313, 0.66359573, -0.1490853 ,
  30. 0.75663096]]])

np.random.randint

  1. c = np.random.randint(100, 200, (3, 4))
  2.  
  3. c
  4. Out[9]:
  5. array([[104, 140, 161, 193],
  6. [134, 147, 126, 120],
  7. [117, 141, 162, 137]])

numpy.random.randint的详细用法 - python

函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high)。如果没有写参数high的值,则返回[0,low)的值。
numpy.random.randint(low, high=None, size=None, dtype='l')

参数如下:

参数 描述
low: int 生成的数值最低要大于等于low。
(hign = None时,生成的数值要在[0, low)区间内)
high: int (可选) 如果使用这个值,则生成的数值在[low, high)区间。
size: int or tuple of ints(可选) 输出随机数的尺寸,比如size=(m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。
dtype: dtype(可选): 想要输出的格式。如int64、int等等

输出:

返回一个随机数或随机数数组

例子

>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
       [3, 2, 2, 0]])

>>>np.random.randint(2, high=10, size=(2,3))
array([[6, 8, 7],
       [2, 5, 2]])

np.random.seed
随机种子生成器,使下一次生成的随机数为由种子数决定的“特定”的随机数,如果seed中参数为空,则生成的随机数“完全”随机。参考和文档。

  1. np.random.seed(10)
  2.  
  3. np.random.randint(100, 200, (3 ,4))
  4. Out[11]:
  5. array([[109, 115, 164, 128],
  6. [189, 193, 129, 108],
  7. [173, 100, 140, 136]])
  8.  
  9. np.random.seed(10)
  10.  
  11. np.random.randint(100 ,200, (3, 4))
  12. Out[13]:
  13. array([[109, 115, 164, 128],
  14. [189, 193, 129, 108],
  15. [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

  1. a = np.random.randint(100, 200, (3, 4))
  2.  
  3. a
  4. Out[15]:
  5. array([[116, 111, 154, 188],
  6. [162, 133, 172, 178],
  7. [149, 151, 154, 177]])
  8.  
  9. np.random.shuffle(a)
  10.  
  11. a
  12. Out[17]:
  13. array([[116, 111, 154, 188],
  14. [149, 151, 154, 177],
  15. [162, 133, 172, 178]])
  16.  
  17. np.random.shuffle(a)
  18.  
  19. a
  20. Out[19]:
  21. array([[162, 133, 172, 178],
  22. [116, 111, 154, 188],
  23. [149, 151, 154, 177]])

可以看到,a发生了变化,轴。

np.random.permutation

  1. b = np.random.randint(100, 200, (3, 4))
  2.  
  3. b
  4. Out[21]:
  5. array([[113, 192, 186, 130],
  6. [130, 189, 112, 165],
  7. [131, 157, 136, 127]])
  8.  
  9. np.random.permutation(b)
  10. Out[22]:
  11. array([[113, 192, 186, 130],
  12. [130, 189, 112, 165],
  13. [131, 157, 136, 127]])
  14.  
  15. b
  16. Out[24]:
  17. array([[113, 192, 186, 130],
  18. [130, 189, 112, 165],
  19. [131, 157, 136, 127]])

可以看到,b没有发生改变。

np.random.choice

  1. c = np.random.randint(100, 200, (8,))
  2.  
  3. c
  4. Out[26]: array([123, 194, 111, 128, 174, 188, 109, 115])
  5.  
  6. np.random.choice(c, (3, 2))
  7. Out[27]:
  8. array([[111, 123],
  9. [109, 115],
  10. [123, 128]])#默认可以出现重复值
  11.  
  12. np.random.choice(c, (3, 2), replace=False)
  13. Out[28]:
  14. array([[188, 111],
  15. [123, 115],
  16. [174, 128]])#不允许出现重复值
  17.  
  18. np.random.choice(c, (3, 2),p=c/np.sum(c))
  19. Out[29]:
  20. array([[194, 188],
  21. [109, 111],
  22. [174, 109]])#指定每个值出现的概率

np.random的随机数函数(3)

函数 说明
uniform(low,high,size) 产生具有均匀分布的数组,low起始值,high结束值,size形状
normal(loc,scale,size) 产生具有正态分布的数组,loc均值,scale标准差,size形状
poisson(lam,size) 产生具有泊松分布的数组,lam随机事件发生率,size形状

  1. u = np.random.uniform(0, 10, (3, 4))
  2.  
  3. u
  4. Out[31]:
  5. array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],
  6. [1.31291053, 8.42817933, 6.59036304, 5.95439605],
  7. [4.36353698, 3.56250327, 5.87130925, 1.49471337]])
  8.  
  9. n = np.random.normal(10, 5, (3, 4))
  10.  
  11. n
  12. Out[33]:
  13. array([[ 8.17771928, 4.17423265, 3.28465058, 17.2669643 ],
  14. [10.00584724, 9.94039808, 13.57941572, 4.07115727],
  15. [ 6.81836048, 6.94593078, 3.40304302, 7.19135792]])
  16.  
  17. p = np.random.poisson(2.0, (3, 4))
  18.  
  19. p
  20. Out[35]:
  21. array([[0, 2, 2, 1],
  22. [2, 0, 1, 3],
  23. [4, 2, 0, 3]])

数据分析师分析问题第一步,必须明确这是不是一个问题!!!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号