numpy的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]]) #random
randn(d0, d1, ..., dn)
返回一个样本,具有标准正态分布。
Notes
For random samples from N(\mu, \sigma^2), 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)
>>> 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 {0, 5/8, 10/8, 15/8, 20/8}):
>>> 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 Unif[a, b), b > a 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
p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }}
e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },
where \mu is the mean and \sigma the standard deviation. The square of the standard deviation, \sigma^2, 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 x + \sigma and x - \sigma [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模块的更多相关文章
- numpy的random模块详细解析
随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14 ...
- python中numpy的random模块
1. rand(d0,d1,.....,dn)产生[0,1]的浮点随机数,括号里面的参数可以指定产生数组的形状 例如:np.random.rand(3,2)则产生 3×2的数组,里面的数是0-1 ...
- 【numpy】新版本中numpy(numpy>1.17.0)中的random模块
numpy是Python中经常要使用的一个库,而其中的random模块经常用来生成一些数组,本文接下来将介绍numpy中random模块的一些使用方法. 首先查看numpy的版本: import nu ...
- numpy.random模块常用函数解析
numpy.random模块中常用函数解析 numpy.random模块官方文档 1. numpy.random.rand(d0, d1, ..., dn)Create an array of the ...
- numpy.random模块用法总结
from numpy import random numpy.random.uniform(low=0.0, high=1.0, size=None) 生出size个符合均分布的浮点数,取值范围为[l ...
- Python基础系列讲解——random模块随机数的生成
随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的random模块提供了生成随机数的方法,使用这些方法时需要导入ran ...
- ZH奶酪:【Python】random模块
Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random ra ...
- np.random模块的使用介绍
np.random模块常用的一些方法介绍 名称 作用 numpy.random.rand(d0, d1, …, dn) 生成一个[d0, d1, …, dn]维的numpy数组,数组的元素取自[0, ...
- getpass模块和random模块
getpass模块 用于对密码的隐藏输入案例: import getpass passwd = getpass.getpass("please input your password&quo ...
随机推荐
- JS 从斐波那契数列浅谈递归
一.前言 昨晚下班后,经理出于兴趣给我们技术组讲了讲算法相关的东西,全程一脸懵逼的听,中途还给我们出了一道比较有趣的爬楼问题,问题如下: 假设一个人从地面开始爬楼梯,规定一步只能爬一坎或者两坎,人只能 ...
- spring-boot-2.0.3源码篇 - filter的注册,值得一看
前言 开心一刻 过年女婿来岳父家走亲戚,当时小舅子主就问:姐夫,你什么时候能给我姐幸福,让我姐好好享受生活的美好.你们这辈子不准备买一套大点的房子吗?姐夫说:现在没钱啊!不过我有一个美丽可爱的女儿,等 ...
- 微信小程序https配置
先简单说下什么是https,https与http区别 ,以及https的原理 什么是https 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网页时候使用的一种协议.HTTP协议传输 ...
- [转]git提交代码时遇到代码库有更新以及本地有更新的解决方法
本文转自:https://blog.csdn.net/myphp2012/article/details/80519156 在多人协作开发时,经常碰到同事把最新修改推送到远程库,你在本地也做了修改,这 ...
- C# 常用的加密代码参考
1.MD5加密 public static string EncryptString(string source) { string result; if (source == string.Empt ...
- 【Spring】18、springMVC对异常处理的支持
无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了.跟其他MVC框架一样, ...
- Java并发编程:线程池的使用(转载)
转载自:https://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- websocket学习和群聊实现
WebSocket协议可以实现前后端全双工通信,从而取代浪费资源的长轮询.在此协议的基础上,可以实现前后端数据.多端数据,真正的实时响应.在学习WebSocket的过程中,实现了一个简化版群聊,过程和 ...
- HDU2196(SummerTrainingDay13-D tree dp)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【代码笔记】Web-HTML-图像
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...