(数据科学学习手札03)Python与R在随机数生成上的异同
隨機(jī)數(shù)的使用是很多算法的關(guān)鍵步驟,例如蒙特卡洛法、遺傳算法中的輪盤(pán)賭法的過(guò)程,因此對(duì)于任意一種語(yǔ)言,掌握其各類型隨機(jī)數(shù)生成的方法至關(guān)重要,Python與R在隨機(jī)數(shù)底層生成上都依靠梅森旋轉(zhuǎn)(twister)來(lái)生成高質(zhì)量的隨機(jī)數(shù),但在語(yǔ)法上存在著很多異同點(diǎn)。
Python
numpy中的random模塊
from numpy import random ?random Type: module String form: <module 'numpy.random' from 'D:\\anaconda\\lib\\site-packages\\numpy\\random\\__init__.py'> File: d:\anaconda\lib\site-packages\numpy\random\__init__.py Docstring: ======================== Random Number Generation ======================== ==================== ========================================================= Utility functions ============================================================================== random_sample Uniformly distributed floats over ``[0, 1)``. random Alias for `random_sample`. bytes Uniformly distributed random bytes. random_integers Uniformly distributed integers in a given range. permutation Randomly permute a sequence / generate a random sequence. shuffle Randomly permute a sequence in place. seed Seed the random number generator. choice Random sample from 1-D array. ==================== ========================================================= ==================== ========================================================= Compatibility functions ============================================================================== rand Uniformly distributed values. randn Normally distributed values. ranf Uniformly distributed floating point numbers. randint Uniformly distributed integers in a given range. ==================== ========================================================= ==================== ========================================================= Univariate distributions ============================================================================== beta Beta distribution over ``[0, 1]``. binomial Binomial distribution. chisquare :math:`\chi^2` distribution. exponential Exponential distribution. f F (Fisher-Snedecor) distribution. gamma Gamma distribution. geometric Geometric distribution. gumbel Gumbel distribution. hypergeometric Hypergeometric distribution. laplace Laplace distribution. logistic Logistic distribution. lognormal Log-normal distribution. logseries Logarithmic series distribution. negative_binomial Negative binomial distribution. noncentral_chisquare Non-central chi-square distribution. noncentral_f Non-central F distribution. normal Normal / Gaussian distribution. pareto Pareto distribution. poisson Poisson distribution. power Power distribution. rayleigh Rayleigh distribution. triangular Triangular distribution. uniform Uniform distribution. vonmises Von Mises circular distribution. wald Wald (inverse Gaussian) distribution. weibull Weibull distribution. zipf Zipf's distribution over ranked data. ==================== ========================================================= ==================== ========================================================= Multivariate distributions ============================================================================== dirichlet Multivariate generalization of Beta distribution. multinomial Multivariate generalization of the binomial distribution. multivariate_normal Multivariate generalization of the normal distribution. ==================== ========================================================= ==================== ========================================================= Standard distributions ============================================================================== standard_cauchy Standard Cauchy-Lorentz distribution. standard_exponential Standard exponential distribution. standard_gamma Standard Gamma distribution. standard_normal Standard normal distribution. standard_t Standard Student's t-distribution. ==================== ========================================================= ==================== ========================================================= Internal functions ============================================================================== get_state Get tuple representing internal state of generator. set_state Set state of generator. ==================== =========================================================上述random的模塊說(shuō)明文檔詳細(xì)說(shuō)明了random中內(nèi)置的各種隨機(jī)數(shù)生成方法,下面針對(duì)其中一些常見(jiàn)的舉例說(shuō)明:
1.random.random_sample()與random.random()
生成[0,1]之間的服從均勻分布的浮點(diǎn)隨機(jī)數(shù)
from numpy import random for i in range(10):print(random.random_sample()) 0.5131167122678871 0.3182844248720986 0.5391999374256481 0.2212549424277599 0.80648135792427 0.34225462561468434 0.5388888490671446 0.00587378555105833 0.6731524781805254 0.210024262178738152.random.random_integers()
生成指定范圍內(nèi)的可重復(fù)整數(shù)
random.random_integers(1,10,10) Out[44]: array([ 9, 10, 6, 4, 10, 10, 5, 3, 1, 6])3.random.permutation()
生成指定范圍內(nèi)所有整數(shù)的一次隨機(jī)排列
for i in range(5):token = random.permutation(5)print(token)print(set(token)) [0 2 1 3 4] {0, 1, 2, 3, 4} [0 3 4 2 1] {0, 1, 2, 3, 4} [2 3 1 4 0] {0, 1, 2, 3, 4} [4 3 0 1 2] {0, 1, 2, 3, 4} [1 2 4 0 3] {0, 1, 2, 3, 4}4.random.shuffle()
將指定的列表隨機(jī)打亂順序
list = [i for i in range(10)] random.shuffle(list) print(list) [6, 8, 2, 4, 5, 3, 0, 7, 1, 9]5.random.seed()
以括號(hào)中的整數(shù)為起點(diǎn)設(shè)置偽隨機(jī)數(shù)種子,同樣的隨機(jī)數(shù)種子設(shè)置后生成的隨機(jī)數(shù)相同
random.seed(42) print(random.permutation(5)) random.seed(42) print(random.permutation(5)) [1 4 2 0 3] [1 4 2 0 3]?6.random.choice()
從制定的序列中隨機(jī)抽取多個(gè)元素(有放回或無(wú)放回,通過(guò)replace參數(shù)控制)
list = [i for i in range(10)] random.choice(list,6,replace=False)#有放回 Out[8]: array([9, 6, 4, 2, 7, 8]) random.choice(list,6,replace=False)#無(wú)放回 Out[9]: array([1, 3, 9, 4, 0, 8])7.random.rand()
生成0-1中服從均勻分布的多個(gè)隨機(jī)數(shù)
random.rand(5) Out[19]: array([0.86317047, 0.43070734, 0.85228662, 0.74797087, 0.76224563])8.random.randn()
生成多個(gè)服從標(biāo)準(zhǔn)正態(tài)分布的隨機(jī)數(shù)
random.randn(10) Out[21]: array([-0.25617082, -0.85531159, -0.18286371, 1.25656827, -0.72270841,0.13949334, 0.92318096, -1.12549131, -0.46908035, -0.28388281])9.random.randint()
等可能的生成指定范圍內(nèi)的多個(gè)隨機(jī)整數(shù)
random.randint(1,10,5) Out[29]: array([2, 9, 8, 8, 9])?
R
作為專為統(tǒng)計(jì)而生的一種語(yǔ)言,R在隨機(jī)數(shù)生成上自然是異常的豐富,這里僅舉常用的一些隨機(jī)數(shù)生成函數(shù)
1.rnorm()
生成服從正態(tài)分布的隨機(jī)數(shù),其中參數(shù)mean控制均值,sd控制標(biāo)準(zhǔn)差
> rnorm(5,mean=0,sd=1) [1] -0.36167951 -0.50435239 -0.20245800 0.07877604 0.236625532.runif()
生成指定范圍內(nèi)的均勻分布隨機(jī)數(shù)
> runif(5, min=0,max=10) [1] 3.2774081 1.7341489 8.4128022 3.1511841 0.33854173.sample()
以不放回的方式生成指定范圍內(nèi)的隨機(jī)整數(shù)序列
> sample(1:10,5,replace=T)#有放回
[1] 4 9 3 4 4
> sample(1:10,5,replace=F)#無(wú)放回
[1] 3 2 6 8 1
4.set.seed()
以括號(hào)內(nèi)的整數(shù)值作為隨機(jī)數(shù)發(fā)生算法的起點(diǎn),因此通過(guò)控制偽隨機(jī)數(shù)種子的參數(shù),可以實(shí)現(xiàn)隨機(jī)抽樣的重現(xiàn)
而真正的隨機(jī)算法里是默認(rèn)以系統(tǒng)時(shí)間等我們認(rèn)為充分隨機(jī)的數(shù)字作為起點(diǎn)
> set.seed(42) > sample(1:10,5,replace=F) [1] 10 9 3 6 4 > set.seed(42) > sample(1:10,5,replace=F) [1] 10 9 3 6 4?
轉(zhuǎn)載于:https://www.cnblogs.com/feffery/p/8536955.html
總結(jié)
以上是生活随笔為你收集整理的(数据科学学习手札03)Python与R在随机数生成上的异同的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Codeforces698B【并查集+拆
- 下一篇: Python全局变量和局部变量