随机抽样 (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.

转载自:http://www.cnblogs.com/hhh5460/p/4324967.html

numpy : numpy.random相关推荐

  1. numpy的random模块

    numpy的random模块 开区间是区间两边都不取等号 闭区间是两边都取等号 半开区间就是只取一边等号 翻译自官网的文档.转自http://www.mamicode.com/info-detail- ...

  2. numpy的random

    在Python的random中,可以非常方便的生成随机数,但如果需要生成多种维度的随机数组或矩阵,那么就需要更好更强大的numpy的random. 一.基本函数 (1)随机生成包含N个元素的数组 形式 ...

  3. Numpy之random.randint产生随机整数

    前言 本文主要讲述了如何使用Numpy的random.randint来产生随机整数,我们演示了如何生成不同上限或下限的指定大小的数组 方法 numpy.random.randint(low, high ...

  4. python设置随机数种子(numpy,pytorch,random)

    为了保证代码能够复现,需要固定所有可能的随机数 import torch import numpy as np import randomdef seed_everywhere(seed):torch ...

  5. numpy numpy.concatenate()函数

    @[TOC](numpy numpy.concatenate()函数)

  6. NumPy - np.random.multivariate_normal()

    np.random.multivariate_normal(mean, cov, size=None, check_valid=None, tol=None) 生成一个多元正态分布矩阵. o. mea ...

  7. Numpy np.random.RandomState()的简单用法

    和np.random.seed()一样,也是做一个随机数种子,不过前者是全局的,也就没有返回值的说法,而这个是局部的: 比如说: import numpy as nprnd = np.random.R ...

  8. numpy: np.random.get_state()

    state = np.random.get_state() 功能:获取随机生成器 np.random的状态 作用:常与np.random.set_state() 搭配使用.使随机生成器random保持 ...

  9. Numpy的random函数

    1 numpy.random.rand() rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1 np.random.rand(4,2) array([[ 0.02173903, 0.4 ...

最新文章

  1. js 各种循环的区别与用法(for in,forEach,for of)
  2. Android 高德地图在清除所有Marker,清除某一个MarKer
  3. 依赖注入底层反射原理_PHP反射机制实现自动依赖注入
  4. flv 自动播放 html autostart=true,《网页制作之FLV视频播放代码的编写.doc
  5. 剖析Caffe源码之Net类变量
  6. python飞机大战跟随鼠标移动_用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动...
  7. 程序员学习视频教程汇总
  8. 2019-5-5学习心得
  9. html元素上下移动,一个可以上下摆动的html元素
  10. [多图]Maclean的巴厘岛游记
  11. ubuntu查看显卡驱动以及其他驱动
  12. cass简码大全_Cass简码成图
  13. —— GPS测量原理及应用复习-7 ——
  14. n1怎么进入线刷模式_中国移动N1 M821线刷刷机教程_移动M821线刷包_救砖包
  15. 【用Python对全职高手小说分析分词词频词性,小说人物出场次数排序,小说中食物排序,小说人物关系等等】
  16. Android websocket闪退,退出手机浏览器,websocket会自动关闭,不是长持续吗
  17. 翁恺《零基础学习Java语言》作业答案 第1周到第7周
  18. 电脑USB插拔记录删除方法分享
  19. jquery 立体走马灯_jquery实现页面百叶窗走马灯式翻滚显示效果的方法
  20. 【搬运】射手播放器下载字幕存储位置及修改方法

热门文章

  1. mongo 查询显示字段_MongoDB查询操作限制返回字段的方法
  2. linux 取文件字节数,如何在Linux上的C中获取文件中的字符数(而不是字节数)
  3. 浏览器 刷新页面后回到顶部_当你在浏览器中,忘记了曾经的登录密码怎么办......
  4. list 分页_mybatis一对多分页查询
  5. OpenCV-Python实战(17)——人脸识别详解
  6. kotlin枚举_Kotlin枚举班
  7. java方法重载和重载方法_Java中的重载与重载
  8. okhttp离线缓存_Android改造OkHttp离线缓存
  9. primefaces教程_Primefaces面板,PanelGrid和PanelMenu示例教程
  10. shell学习之定时运行作业