日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

(数据科学学习手札03)Python与R在随机数生成上的异同

發布時間:2023/12/18 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (数据科学学习手札03)Python与R在随机数生成上的异同 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

隨機數的使用是很多算法的關鍵步驟,例如蒙特卡洛法、遺傳算法中的輪盤賭法的過程,因此對于任意一種語言,掌握其各類型隨機數生成的方法至關重要,Python與R在隨機數底層生成上都依靠梅森旋轉(twister)來生成高質量的隨機數,但在語法上存在著很多異同點。

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的模塊說明文檔詳細說明了random中內置的各種隨機數生成方法,下面針對其中一些常見的舉例說明:

1.random.random_sample()與random.random()

生成[0,1]之間的服從均勻分布的浮點隨機數

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

2.random.random_integers()

生成指定范圍內的可重復整數

random.random_integers(1,10,10) Out[44]: array([ 9, 10, 6, 4, 10, 10, 5, 3, 1, 6])

3.random.permutation()

生成指定范圍內所有整數的一次隨機排列

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

將指定的列表隨機打亂順序

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

以括號中的整數為起點設置偽隨機數種子,同樣的隨機數種子設置后生成的隨機數相同

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

從制定的序列中隨機抽取多個元素(有放回或無放回,通過replace參數控制)

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)#無放回 Out[9]: array([1, 3, 9, 4, 0, 8])

7.random.rand()

生成0-1中服從均勻分布的多個隨機數

random.rand(5) Out[19]: array([0.86317047, 0.43070734, 0.85228662, 0.74797087, 0.76224563])

8.random.randn()

生成多個服從標準正態分布的隨機數

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

等可能的生成指定范圍內的多個隨機整數

random.randint(1,10,5) Out[29]: array([2, 9, 8, 8, 9])

?

R

作為專為統計而生的一種語言,R在隨機數生成上自然是異常的豐富,這里僅舉常用的一些隨機數生成函數

1.rnorm()

生成服從正態分布的隨機數,其中參數mean控制均值,sd控制標準差

> rnorm(5,mean=0,sd=1) [1] -0.36167951 -0.50435239 -0.20245800 0.07877604 0.23662553

2.runif()

生成指定范圍內的均勻分布隨機數

> runif(5, min=0,max=10) [1] 3.2774081 1.7341489 8.4128022 3.1511841 0.3385417

3.sample()

以不放回的方式生成指定范圍內的隨機整數序列

> sample(1:10,5,replace=T)#有放回
[1] 4 9 3 4 4
> sample(1:10,5,replace=F)#無放回
[1] 3 2 6 8 1

4.set.seed()

以括號內的整數值作為隨機數發生算法的起點,因此通過控制偽隨機數種子的參數,可以實現隨機抽樣的重現

而真正的隨機算法里是默認以系統時間等我們認為充分隨機的數字作為起點

> 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

?

轉載于:https://www.cnblogs.com/feffery/p/8536955.html

總結

以上是生活随笔為你收集整理的(数据科学学习手札03)Python与R在随机数生成上的异同的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产性一乱一性一伧一色 | 一级黄色免费网站 | 成人免费视频国产免费 | 一区二区三区视频免费视 | 狼人精品一区二区三区在线 | 成年人免费大片 | 亚洲五月天综合 | 超碰人人爱 | 永久免费看片在线播放 | 日韩少妇 | 亚洲嫩草影院 | 亚洲免费一区二区 | 国产麻豆剧果冻传媒白晶晶 | 午夜福利电影 | 天堂av网在线| 超碰在线免费看 | 亚洲先锋影音 | 成人aⅴ视频 | 午夜黄色网址 | www.伊人网 | 日本一区二区精品 | 欧美成人精品欧美一级乱 | 亚洲一区免费在线观看 | 色婷婷综合激情 | 美女扒开腿免费视频 | 久久久久女 | 久久伊人成人网 | 波多野结衣视频在线看 | 国产主播福利 | 女女爱爱视频 | 91色视频在线观看 | 欧美爽爽| 亚洲精品一区二区三区蜜桃久 | 亚洲国产精品系列 | 亚洲美免无码中文字幕在线 | 在线日韩亚洲 | 91精品国产精品 | 在线免费看av片 | 极品另类 | 欧美精品videos另类日本 | 人妻丝袜一区 | 日韩福利在线观看 | 欧美日韩一区二区在线视频 | 国产真实乱 | 91久久久久久久久久久久久 | 91视频黄色 | 免费看91的网站 | 日产mv免费观看 | 中国爆后菊女人的视频 | 欧美日韩免费高清一区色橹橹 | 欧美欧美欧美 | 色男人网| 人人看人人插 | 91极品身材尤物theporn | 四虎影库永久在线 | 日韩精品一区二区电影 | 国产精品久久久久久久久久免费看 | 黄色一级大片免费看 | 国产精品外围 | 91麻豆精品国产91久久久更新时间 | 国产免费一区视频观看免费 | 亚洲国产精品成人 | 久久在线中文字幕 | 日本三级黄在线观看 | 欧美精品不卡 | 国产91一区 | 美女在线观看www | 天天亚洲 | 欧美精品成人一区二区在线观看 | 超碰人人爱 | 国产理论精品 | 亚洲AV无码国产成人久久 | 亚洲熟妇丰满大屁股熟妇 | 日本欧美国产一区二区三区 | 97人妻精品一区二区三区视频 | 成年人免费视频网站 | 欧美在线观看成人 | 91一区视频 | 爱乃なみ加勒比在线播放 | 在线成人av | 日韩伦理中文字幕 | 色视频综合 | 婷婷.com| 午夜试看120秒 | 欧美性在线观看 | 涩涩网站免费 | 亚洲欧美日韩国产成人精品影院 | 国产精品毛片一区二区在线看舒淇 | 91手机视频在线观看 | 国产三级精品三级在线观看 | 国产精品久久一区 | 男女性生活视频网站 | 夜色资源网 | 性欧美8khd高清极品 | 成年人国产 | 天堂资源在线观看 | 亚洲AV蜜桃永久无码精品性色 | 91丨porny丨在线中文 | a在线看 |