python numpy.random详细解析

Numpy常用random随机函数汇总 numpy官方文档(scipy) https://docs.scipy.org/doc/numpy-1.17.0/reference/ 函数名 说明 seed([seed]) 设定随机种子,这样每次生成的随机数会相同 rand(d0,d1,d2.....) 返回数据在[0,1)之间,具有均匀分布 randn(d0,d1,d2....) 阅读详情

随机抽样 (numpy.random)

简单的随机数据

rand(d0, d1, ..., dn)

随机值

>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random

randn(d0, d1, ..., dn)

返回一个样本,具有标准正态分布

Notes

For random samples from 技术分享, use:

sigma * np.random.randn(...) + mu

Examples

>>> np.random.randn()
2.1923875335537315 #random

Two-by-four array of samples from N(3, 6.25):

>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random

randint(low[, high, size])

返回随机的整数,位于半开区间 [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])

Generate a 2 x 4 array of ints between 0 and 4, inclusive:

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

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.)

Examples

>>> np.random.random_integers(5)
4
>>> type(np.random.random_integers(5))
<type int>
>>> np.random.random_integers(5, size=(3.,2.))
array([[5, 4],
       [3, 3],
       [4, 5]])

Choose five random numbers from the set of five evenly-spaced numbers between 0 and 2.5, inclusive (i.e., from the set 技术分享):

>>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4.
array([ 0.625,  1.25 ,  0.625,  0.625,  2.5  ])

Roll two six sided dice 1000 times and sum the results:

>>> d1 = np.random.random_integers(1, 6, 1000)
>>> d2 = np.random.random_integers(1, 6, 1000)
>>> dsums = d1 + d2

Display results as a histogram:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(dsums, 11, normed=True)
>>> plt.show()

 

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

Examples

>>> np.random.random_sample()
0.47108547995356098
>>> type(np.random.random_sample())
<type float>
>>> np.random.random_sample((5,))
array([ 0.30220482,  0.86820401,  0.1654503 ,  0.11659149,  0.54323428])

Three-by-two array of random numbers from [-5, 0):

>>> 5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
       [-2.99091858, -0.79479508],
       [-1.23204345, -1.75224494]])

 

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)
array([0, 3, 4])
>>> #This is equivalent to np.random.randint(0,5,3)

Generate a non-uniform random sample from np.arange(5) of size 3:

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])

Generate a uniform random sample from np.arange(5) of size 3 without replacement:

>>> np.random.choice(5, 3, replace=False)
array([3,1,0])
>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]

Generate a non-uniform random sample from np.arange(5) of size 3 without replacement:

>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])

Any of the above can be repeated with an arbitrary array-like instead of just integers. For instance:

>>> aa_milne_arr = [pooh, rabbit, piglet, Christopher]
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array([pooh, pooh, pooh, Christopher, piglet],
      dtype=|S11)

 

bytes(length)

返回随机字节。

>>> np.random.bytes(10)
 eh\x85\x022SZ\xbf\xa4 #random

 

排列

shuffle(x)

现场修改序列,改变自身内容。(类似洗牌,打乱顺序)

>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]

 

This function only shuffles the array along the first index of a multi-dimensional array:

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
       [6, 7, 8],
       [0, 1, 2]])

 

permutation(x)

返回一个随机排列

>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12])
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8],
       [0, 1, 2],
       [3, 4, 5]])

 

分布

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]
>>> cov = [[1,0],[0,100]] # diagonal covariance, points lie on x or y-axis
>>> import matplotlib.pyplot as plt
>>> x, y = np.random.multivariate_normal(mean, cov, 5000).T
>>> plt.plot(x, y, x); plt.axis(equal); plt.show()

 

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
>>> s = np.random.normal(mu, sigma, 1000)

Verify the mean and the variance:

>>> abs(mu - np.mean(s)) < 0.01
True
>>> abs(sigma - np.std(s, ddof=1)) < 0.01
True

Display the histogram of the samples, along with the probability density function:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
...                np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
...          linewidth=2, color=r)
>>> plt.show()

 

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.
Numpy常用random随机函数 只要random.seed( * ) seed里面的值一样,那随机出来的结果就一样。所以说,seed的作用是让随机结果可重现。也就是说当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数。使用同一个种子,每次生成的随机数序列都是相同的。 阅读详情

相关推荐

random.sample 函数详解

是一个非常实用的函数,适用于多种数据类型的无放回随机采样。无论是处理小规模数据,还是需要在大规模数据集上抽样分析,都可以利用来实现。在数据科学、游戏开发以及抽奖系统等多种领域,都是一个不可或缺的工具。希望这篇文章能帮助大家更好地理解和使用函数!

猫敷雪 2152

numpy中的np.random用法

一、np.random.rand():生成指定维度的[0,1)间的随机数 np.random.rand(4,3);///生成4行3列的数组,数组中内一个元素都是[0,1)间的随机数 二、np.random.random():生成指定维度的[0,1)间的随机数 np.random.random([4,3]);///生成4行3列的数组,数组中内一个元素都是[0,1)间的随机数,等同于np.random.rand(); 三、np.random.randn():生成的随机数服从正态分布 np.random.ra

Candyerer的博客 2万+

python-Numpy学习之(三) random详细解析

python-Numpy学习之(三) random详细解析 参考网址:https://blog.csdn.net/vicdd/article/details/52667709 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 &gt;&gt;&gt; np.random.rand(3,2) a...

a1809032425的博客 1836

Numpyrandom函数

1 numpy.random.rand() rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1 np.random.rand(4,2) array([[ 0.02173903, 0.44376568], [ 0.25309942, 0.85259262], [ 0.56465709, 0.95135013], [ 0.14145746, 0.55389458]]) np.random.rand(5,2,1) # shape: 5*2*1 [[

aqiangdeba的博客 9217

python np random_pythonnumpy.random详细解析

随机抽样 (numpy.random)简单的随机数据rand(d0, d1, …, dn) 随机值>>> np.random.rand(3,2)array([[ 0.14022471, 0.96360618], #random[ 0.37601032, 0.25528411], #random[ 0.49313049, 0.94909878]]) #randomran...

weixin_32642109的博客 1236

python numpy.random详细解析

随机抽样 (numpy.random)简单的随机数据rand(d0, d1, ..., dn)随机值&gt;&gt;&gt; np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909...

小猪打呼噜 1万+

[转载] pythonnumpy.random详细解析

参考链接: Python中的numpy.float_power 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, …, dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360...

u013946150的博客 232

PythonNumPy库提供的函数——np.random.randn的基本用法

NumPy中用于生成服从标准正态分布(均值为0,标准差为1)的随机数的函数。它生成的随机数遵循标准正态分布,也称为高斯分布。以下是使用运行结果:这将生成一个或多个服从标准正态分布的随机数。如果要生成服从不同均值和标准差的正态分布随机数,可以使用函数,它为您指定均值和标准差的参数请注意,生成的随机数是伪随机数,它们是通过确定性算法生成的,但通常在实际应用中足够随机。要使随机数生成具有确定性,可以设置随机种子,使用函数,此用于实验的可重复性非常重要。

XDXDXDXDX111的博客 1万+

np.random一系列(np.random.normal()、np.random.randint、np.random.randn、np.random.rand)

         在使用numpy的时候,我们经常会使用到np.random一系列的有关函数,来创建ndarray 数组。random代表随机的意思,指ndarray中的数是随机数。后面的函数表示随机生成的ndarray需要符合什么样的条件。因为其太多,所以容易弄混淆下面将其常用的几个列出来(后续遇到新的不断...

嘤嘤怪赚钱养妈妈 4596

详述numpy中的np.random各个函数的用法

该函数括号内的参数指定的是返回结果的形状,如果不指定,那么生成的是一个浮点型的数;返回结果:返回值是一个大小为size的数组,如果指定了low和high这两个参数,那么生成的元素值的范围为[low,high),不包括high;我们前面已经说过了rand()这个函数,它返回的元素值是服从0-1的均匀分布,那如果不想要生成的是0-1范围内的均匀分布,想要其它范围内的均匀分布怎么办呢。其返回值的元素类型为浮点型。结果中的每一个元素是服从0~1均匀分布的随机样本值,也就是返回的结果中的每一个元素值在0-1之间。

猫敷雪 2355

(如何从一个列表中随机抽样)np.random.choice(),random.sample()

这个函数非常有用,可以从一个列表中抽样。 其一共有4个参数: choice(a, size=None, replace=True, p=None) a :列表或者整数 若为整数,则等价于一个列表,因为函数会自动先把整数a变成列表np.arange(a) 总之,其实就是一个列表。 size : 整数或元组 整数表示需要从列表a中抽样多少个元素。 元组(m, n, k)表示抽样m * n * k的元素。 replace : 布尔值 是否为有放回抽样 p : 列表 对列表a每一个元素赋予被抽取的概

qq_43391414的博客 7006

详述numpy中的np.random.rand()、np.random.randn()、np.random.randint()、np.random.uniform()函数的用法

np.random.rand()、np.random.randn()、np.random.randint()、np.random.uniform()函数的区别和用法,他们返回值都是怎么样的?本篇文章通过代码带你理解它们各自的作用。

BaoITcore的博客 5万+

numpy.random函数整合(部分)

在我们进行python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面由我进行一部分的总结 1.numpy.random.rand numpy.random.rand(d1 , d2,…dn) rand函数创建一个给定类型的数组,将其填充在一个均匀分布的随机样本[0, 1)中。 a=np.random.rand(2,2)#shape:2*2 print(a) [[0.22305618 0.64853825] [0.11869

weixin_52703681的博客 4811

numpy教程:随机数模块numpy.random

http://blog.csdn.net/pipisorry/article/details/39508417 随机数种子 RandomState RandomState exposes a number of methods for generating random numbersdrawn from a variety of probability distributions. 使用...

皮皮blog 11万+

Python随机数生成(二):numpy库中random函数

Python中主要有两种途径,一是利用生成,二是利用生成。在我们日常使用中,如果是为了得到,多考虑;如果是为了得到,就多考虑。除此之外,也可以生成随机数和特定分布的随机数。其他方式生成随机数见笔者的其他文章:Python随机数生成(一):random模块。

weixin_44842318的博客 3762

Numpy | np.random随机模块的使用介绍

如有错误,恳请指出。 文章目录1. 随机抽样2. 随机排序3. 随机分布4. 随机种子 平时都会使用到随机模块,一般是torch.random或者是numpy.random,有或者是直接使用ramdom这个python内置的工具包,那么下面就简单记录一下numpy.random常用的函数。 1. 随机抽样 import numpy as np np.random.randn(3,3) # 从标准正太分布中返回样本 np.random.rand(3,3) # 从0-1均匀分布分布中返回样本 np.ra.

Clichong 2852

Python基础之- Numpyrandom 函数简介

python 数据分析的学习和应用过程中,经常需要用到 numpy 的随机函数,由于随机函数 random 的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下。 1 numpy.random.rand() numpy.random.rand(d0,d1,…,dn) • rand 函数根据给定维度生成 [0,1) 之间的数据,包含 0,不包含 1 • dn 表格每个维度 • 返回值为指定维度的 array import numpy as np np.random.ran

weixin_45586124的博客 3582

NumPy教程-numpy.random()在Python中的使用

这个模块包含用于生成随机数的函数。这个 random 模块的函数用于生成从包括 (low) 到不包括 (high) 的随机整数。这个 random 模块的函数用于在半开区间 [0.0, 1.0) 中生成随机浮点数。这个 random 模块的函数用于在半开区间 [0.0, 1.0) 中生成随机浮点数。这个随机模块的函数用于生成半开区间 [0.0, 1.0) 内的随机浮点数。这个随机模块的函数用于生成半开区间 [0.0, 1.0) 内的随机浮点数。这个随机模块的函数用于从给定的一维数组中生成随机样本。

aobulaien001的博客 1502
上一篇: vim常用命令
下一篇: Lua中require,dofile,loadfile,dostring,loadstring,loadlib,load之间的区别
vicdd
博客等级 码龄12年 61粉丝 11原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值