numpy.random用法
最近发现numpy的random用法有很多,不注意很容易混淆,今天参考几个博客内容整理了一下。
numpy.random.randint
low、high、size三个参数。默认high是None,如果只有low,那范围就是[0,low)。如果有high,范围就是[low,high)。
>>> 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]])
numpy.random.randn()与rand()的区别
numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中。
numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)中。
代码:
import numpy as np arr1 = np.random.randn(2,4)
print(arr1)
print('******************************************************************')
arr2 = np.random.rand(2,4)
print(arr2)
结果
[[-1.03021018 0.5197033 0.52117459 -0.70102661]
[ 0.98268569 1.21940697 -1.095241 -0.38161758]]
******************************************************************
[[ 0.19947349 0.05282713 0.56704222 0.45479972]
[ 0.28827103 0.1643551 0.30486786 0.56386943]]
随机抽样 (numpy.random)
参考:https://blog.csdn.net/vicdd/article/details/52667709
简单的随机数据
rand(d0, d1, ..., dn) |
随机值 >>> np.random.rand(3,2) |
randn(d0, d1, ..., dn) |
返回一个样本,具有标准正态分布。 Notes For random samples from , use: sigma * np.random.randn(...) + mu >>> np.random.randn() >>> 2.5 * np.random.randn(2, 4) + 3 |
randint(low[, high, size]) |
返回随机的整数,位于半开区间 [low, high)。 >>> np.random.randint(2, size=10) >>> np.random.randint(5, size=(2, 4)) |
random_integers(low[, high, size]) |
返回随机的整数,位于闭区间 [low, high]。 Notes To sample from N evenly spaced floating-point numbers between a and b, use: a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.) >>> np.random.random_integers(5) >>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4. >>> d1 = np.random.random_integers(1, 6, 1000) >>> import matplotlib.pyplot as plt |
random_sample([size]) |
返回随机的浮点数,在半开区间 [0.0, 1.0)。 To sample multiply the output of random_sample by (b-a) and add a: (b - a) * random_sample() + a >>> np.random.random_sample() >>> 5 * np.random.random_sample((3, 2)) - 5 |
random([size]) |
返回随机的浮点数,在半开区间 [0.0, 1.0)。 (官网例子与random_sample完全一样) |
ranf([size]) |
返回随机的浮点数,在半开区间 [0.0, 1.0)。 (官网例子与random_sample完全一样) |
sample([size]) |
返回随机的浮点数,在半开区间 [0.0, 1.0)。 (官网例子与random_sample完全一样) |
choice(a[, size, replace, p]) |
生成一个随机样本,从一个给定的一维数组 Examples Generate a uniform random sample from np.arange(5) of size 3: >>> np.random.choice(5, 3) >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) >>> np.random.choice(5, 3, replace=False) >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) >>> aa_milne_arr = [‘pooh‘, ‘rabbit‘, ‘piglet‘, ‘Christopher‘] |
bytes(length) |
返回随机字节。
|
排列
shuffle(x) |
现场修改序列,改变自身内容。(类似洗牌,打乱顺序) >>> arr = np.arange(10) This function only shuffles the array along the first index of a multi-dimensional array: >>> arr = np.arange(9).reshape((3, 3)) |
permutation(x) |
返回一个随机排列 >>> np.random.permutation(10) |
分布
beta(a, b[, size]) | 贝塔分布样本,在 [0, 1]内。 |
binomial(n, p[, size]) | 二项分布的样本。 |
chisquare(df[, size]) | 卡方分布样本。 |
dirichlet(alpha[, size]) | 狄利克雷分布样本。 |
exponential([scale, size]) | 指数分布 |
f(dfnum, dfden[, size]) | F分布样本 |
gamma(shape[, scale, size]) | 伽马分布 |
geometric(p[, size]) | 几何分布 |
gumbel([loc, scale, size]) | 耿贝尔分布。 |
hypergeometric(ngood, nbad, nsample[, size]) | 超几何分布样本。 |
laplace([loc, scale, size]) | 拉普拉斯或双指数分布样本 |
logistic([loc, scale, size]) | Logistic分布样本 |
lognormal([mean, sigma, size]) | 对数正态分布 |
logseries(p[, size]) | 对数级数分布 |
multinomial(n, pvals[, size]) | 多项分布 |
multivariate_normal(mean, cov[, size]) |
多元正态分布。 >>> mean = [0,0] |
negative_binomial(n, p[, size]) | 负二项分布 |
noncentral_chisquare(df, nonc[, size]) | 非中心卡方分布 |
noncentral_f(dfnum, dfden, nonc[, size]) | 非中心F分布 |
normal([loc, scale, size]) |
正态(高斯)分布 Notes The probability density for the Gaussian distribution is where is the mean and the standard deviation. The square of the standard deviation, , is called the variance. The function has its peak at the mean, and its “spread” increases with the standard deviation (the function reaches 0.607 times its maximum at and [R217]). Examples Draw samples from the distribution: >>> mu, sigma = 0, 0.1 # mean and standard deviation >>> abs(mu - np.mean(s)) < 0.01 >>> import matplotlib.pyplot as plt |
pareto(a[, size]) | 帕累托(Lomax)分布 |
poisson([lam, size]) | 泊松分布 |
power(a[, size]) | Draws samples in [0, 1] from a power distribution with positive exponent a - 1. |
rayleigh([scale, size]) | Rayleigh 分布 |
standard_cauchy([size]) | 标准柯西分布 |
standard_exponential([size]) | 标准的指数分布 |
standard_gamma(shape[, size]) | 标准伽马分布 |
standard_normal([size]) | 标准正态分布 (mean=0, stdev=1). |
standard_t(df[, size]) | Standard Student’s t distribution with df degrees of freedom. |
triangular(left, mode, right[, size]) | 三角形分布 |
uniform([low, high, size]) | 均匀分布 |
vonmises(mu, kappa[, size]) | von Mises分布 |
wald(mean, scale[, size]) | 瓦尔德(逆高斯)分布 |
weibull(a[, size]) | Weibull 分布 |
zipf(a[, size]) | 齐普夫分布 |
随机数生成器
RandomState | Container for the Mersenne Twister pseudo-random number generator. |
seed([seed]) | Seed the generator. |
get_state() | Return a tuple representing the internal state of the generator. |
set_state(state) | Set the internal state of the generator from a tuple. |
random VS numpy.random
参考链接:https://www.cnblogs.com/ymjyqsx/p/7395685.html
https://blog.csdn.net/u010758410/article/details/71799142
https://blog.csdn.net/kancy110/article/details/69665164
numpy.random用法相关推荐
- 【python】numpy.random用法
numpy中的random主要是用来产生随机数的一个模块. 模块使用中的常见问题: 1.randn()和rand()的区别: numpy.random.randn(d0, d1, -, dn)是从标 ...
- numpy.random.choice用法
python,numpy中np.random.choice()的用法详解及其参考代码 处理数据时经常需要从数组中随机抽取元素,这时候就需要用到np.random.choice().然而choice用法 ...
- numpy.random.choice()用法详解(附官方文档)
numpy.random.choice numpy官方文档:https://numpy.org/devdocs/reference/random/generated/numpy.random.choi ...
- [转载] 【Python-Numpy】numpy.random.randint用法
参考链接: Python中的numpy.random.rand numpy.random.randint(low, high=None, size=None, dtype='l') 函数的作用是,返回 ...
- numpy.random.seed()用法详解
1.总体说明: numpy.random.seed()中每一个数字代表一种随机数生成规则,当种子数确定后,每次调用numpy.random下的随机函数时,都会根据该种子数对应的规则,依次生成随机数或随 ...
- numpy.random.rand用法
使用 numpy.random.rand 方法生成任意随机数 创建一个给定类型的数组,将其填充在一个均匀分布的随机样本[0, 1)中 (1)生成单个随机数. r1=np.random.rand() p ...
- numpy.random.randn()用法
在python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下. import numpy as np ...
- random.shuffle和numpy.random.shuffle用法一样吗?
random.shuffle和numpy.random.shuffle的使用 1.对于一维数据 2.对于多维数据 2.1 random.shuffle操作np.array格式数组 2.2 random ...
- numpy.random.randint用法
numpy.random.randint(low, high=None, size=None, dtype='l') 函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, h ...
最新文章
- R语言ggplot2可视化自动换行适配长文本图例(legend)实战:Multiple Lines for Text per Legend Label
- 微信小程序使用高德地图API获取准确定位地址描述
- 【AC Saber】双指针
- pycharm+itk+vtk安装及测试程序运行
- 用Spring更好地处理Struts动作三种整合
- SPI、I2C、UART 三种串行总线对比介绍
- zigbee上位机通过vs2019的mfc实现
- DOM相关(主要是var和let的区别用法)
- html中写meta会乱码,BeautifulSoup+Unicode,该死的html乱码,尽管在meta标记中指定了字符集...
- mysql 数据库被覆盖_理解MySQL数据库覆盖索引
- excel游戏_Excel集中游戏
- 学习-Python列表之列表应用之降序排列
- linux 空白进程,Linux空白行:行号显示、空白行数统计、删除空白行
- 河北省地税数据上收集中及异地容灾应用(RealSync数据库同步复制)
- 编译akm小车底盘控制节点
- 并发问题中的乐观锁和悲观锁
- 多传感器融合定位十五-多传感器时空标定(综述)
- 为什么单页面的seo不友好?如何解决这一问题?
- Python 兵器谱
- 【不同的子序列II】
热门文章
- 启动android SDK下模拟器的方法
- 写给想去银行国企Java岗位的学习路线规划
- 【阿里校招最后一周】错过悔终生!
- java编程10以内随机数字乘除_随机生成10个长度为10以内的字符串并排序(Java入门第三季集合下作...
- 卡特兰数catalan证明及应用举例
- 十万个为什么儿童版_(完整版)十万个为什么少儿版
- mysql安装教程2005,SQL 2005怎么安装 SQL Server 2005安装图解教程
- 二阶段提交,三阶段提交,Paxos
- C++教程——继承、多态
- linux内核oops错误码说明,Linux Kernel Oops异常分析