侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python numpy之np.random的随机数函数使用介绍

Python  /  管理员 发布于 7年前   258

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)aOut[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)bOut[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))cOut[9]: array([[104, 140, 161, 193],    [134, 147, 126, 120],    [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中参数为空,则生成的随机数“完全”随机。参考和文档。

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))aOut[15]: array([[116, 111, 154, 188],    [162, 133, 172, 178],    [149, 151, 154, 177]])np.random.shuffle(a)aOut[17]: array([[116, 111, 154, 188],    [149, 151, 154, 177],    [162, 133, 172, 178]])np.random.shuffle(a)aOut[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))bOut[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]])bOut[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,))cOut[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))uOut[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))nOut[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))pOut[35]: array([[0, 2, 2, 1],    [2, 0, 1, 3],    [4, 2, 0, 3]])

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


  • 上一条:
    Python模块汇总(常用第三方库)
    下一条:
    python系列 文件操作的代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客