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 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 (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 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
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]) | 齐普夫分布 |
随机数生成器
Container for the Mersenne Twister pseudo-random number generator.
seed([seed])
Seed the generator.
Return a tuple representing the internal state of the generator.
set_state(state)
Set the internal state of the generator from a tuple.
标签:
踩
( 3)
赞
( 30)
举报
评论 一句话评论(0)
<div id="commentlistend" name="commentlistend" class="divtextaligncenter margintop20">
<span id="lblpage">共<span class="pagecolorc">0</span>条 </span>
</div>
</div>
<div class="margintop20">
<form method="post" action="/ajaxjs/info_detail_commentadd.aspx">
<div class="divtextalignleft paddingtop20">
<div id="commenthf" class="divbackgroundcolor1">
</div>
<div>
<textarea name="tbcommentcontent" id="tbcommentcontent" class="tb" style="width: 680px;
height: 120px;"></textarea>
</div>
</div>
<div class="divtextalignright paddingtop10 ">
<span id="addCommentTishi" class="colorboldHong">登录后才能评论!</span>
<span id="loginno"><input type="button" class="mbtn1" value="登录" onclick="location.href='http://member.mamicode.com/login.aspx?returnUrl='+document.URL.replace(new RegExp('&', 'g'), '(_)')"></span>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript"> mamicode_adload('content_bottom');</script><div class="margintop20"> <script type="text/javascript"> /*mamicode-bottom-680x250*/ var cpro_id = "u2811621"; </script> <script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script><div id="BAIDU_SSP__wrapper_u2811621_0"><iframe id="iframeu2811621_0" src="http://pos.baidu.com/ycam?rdid=2811621&dc=3&di=u2811621&dri=0&dis=0&dai=3&ps=11270x126&dcb=___adblockplus&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1500859826702&ti=numpy%E7%9A%84random%E6%A8%A1%E5%9D%97&ari=2&dbv=2&drs=1&pcs=1268x630&pss=1269x11271&cfv=0&cpl=25&chi=1&cce=true&cec=UTF-8&tlm=1500859826&rw=630&ltu=http%3A%2F%2Fwww.mamicode.com%2Finfo-detail-507676.html&ecd=1&uc=1920x1040&pis=-1x-1&sr=1920x1080&tcn=1500859827&qn=22368b720b4c12ee&tt=1500859826643.314.314.314" width="680" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" style="border:0;vertical-align:bottom;margin:0;width:680px;height:250px" allowtransparency="true"></iframe></div></div><div class="divtextaligncenter margintop20"> <script type="text/javascript"> /*mamicode-infodetail-title-top-680x250*/ var cpro_id = "u2860054"; </script> <script src="http://cpro.baidustatic.com/cpro/ui/c.js" type="text/javascript"></script><div id="BAIDU_SSP__wrapper_u2860054_0"><iframe id="iframeu2860054_0" src="http://pos.baidu.com/ycam?rdid=2860054&dc=3&di=u2860054&dri=0&dis=0&dai=4&ps=11565x126&dcb=___adblockplus&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1500859826702&ti=numpy%E7%9A%84random%E6%A8%A1%E5%9D%97&ari=2&dbv=2&drs=1&pcs=1268x630&pss=1269x11565&cfv=0&cpl=25&chi=1&cce=true&cec=UTF-8&tlm=1500859826&rw=630&ltu=http%3A%2F%2Fwww.mamicode.com%2Finfo-detail-507676.html&ecd=1&uc=1920x1040&pis=-1x-1&sr=1920x1080&tcn=1500859827&qn=13f461ea45b9a4ea&tt=1500859826643.330.330.331" width="680" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" style="border:0;vertical-align:bottom;margin:0;width:680px;height:250px" allowtransparency="true"></iframe></div></div>
</div>
</div>
<div class="width30bi divfloatright">
<div class="paddingbottom20">
<script type="text/javascript"> (function () { document.write(unescape('%3Cdiv id="bdcs"%3E%3C/div%3E')); var bdcs = document.createElement('script'); bdcs.type = 'text/javascript'; bdcs.async = true; bdcs.src = 'http://znsv.baidu.com/customer_search/api/js?sid=7490613033886828738' + '&plate_url=' + encodeURIComponent(window.location.href) + '&t=' + Math.ceil(new Date() / 3600000); var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bdcs, s); })();</script><div id="bdcs"><div class="bdcs-container"><meta http-equiv="x-ua-compatible" content="IE=9"> <!-- 嵌入式 --> <div class="bdcs-main bdcs-clearfix" id="default-searchbox"> <div class="bdcs-search bdcs-clearfix" id="bdcs-search-inline"> <form action="http://zhannei.baidu.com/cse/search" method="get" target="_blank" class="bdcs-search-form" autocomplete="off" id="bdcs-search-form"> <input type="hidden" name="s" value="7490613033886828738"> <input type="hidden" name="entry" value="1"> <input type="text" name="q" class="bdcs-search-form-input" id="bdcs-search-form-input" placeholder="请输入关键词" style="height: 34px; line-height: 34px;"> <input type="submit" class="bdcs-search-form-submit " id="bdcs-search-form-submit" value="搜索"> </form> </div> </div> </div></div>
<script type="text/javascript"> mamicode_adload('right_top');</script><div class="paddingbottom10"> <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 1-mamicode-right-300x600 --> <ins class="adsbygoogle" style="display: inline-block; width: 300px; height: 600px" data-ad-client="ca-pub-8616102841876629" data-ad-slot="8947049888" data-adsbygoogle-status="done"><ins id="aswift_3_expand" style="display:inline-table;border:none;height:600px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><ins id="aswift_3_anchor" style="display:block;border:none;height:600px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><iframe width="300" height="600" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_3" name="aswift_3" style="left:0;position:absolute;top:0;width:300px;height:600px;"></iframe></ins></ins></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div>
- 如何培养《未来架构师》 (2) 2017-07-20
- 机器学习—逻辑回归理论简介 2015-03-06
- 在深圳有娃的家长必须要懂的社保少儿医保,不然亏大了!(收藏) 2016-11-16
- 下了个蓝屏代码查看工具,就中病毒了。。。什么鬼病毒,竟然还是用的VBS 2015-06-03
- “全栈”工程师 请不要随意去做 2017-03-28
- ASCLL表 2016-03-26
- 爱奇艺、优酷、腾讯视频竞品分析报告2016(一) 2016-04-04
- 十六进制转八进制 2015-03-31
- numpy的random模块 2015-03-10
- Visual Studio 2015 的安装与使用 2015-10-24
<script type="text/javascript"> mamicode_adload('right_bottom');</script><div id="xuanting" class="width300px margintop5" style="left: 834px; top: 1px; z-index: 1000; position: fixed;"> <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- mamicode-right-xuanting-300x600 --> <ins class="adsbygoogle" style="display: inline-block; width: 300px; height: 0px;" data-ad-client="ca-pub-8616102841876629" data-ad-slot="4406515082" data-adsbygoogle-status="done"><ins id="aswift_4_expand" style="display: inline-table; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 300px; background-color: transparent;"><ins id="aswift_4_anchor" style="display: block; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 300px; overflow: hidden; transition: opacity 1s cubic-bezier(0.4, 0, 1, 1), width 0.2s cubic-bezier(0.4, 0, 1, 1) 0.3s, height 0.5s cubic-bezier(0.4, 0, 1, 1); opacity: 0; background-color: transparent;"><iframe width="300" height="600" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_4" name="aswift_4" style="left:0;position:absolute;top:0;width:300px;height:600px;"></iframe></ins></ins></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div>
</div>
<div class="divfloatclear">
</div>
</div>