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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/10/11 综合教程 78 老码农
生活随笔 收集整理的這篇文章主要介紹了 (数据科学学习手札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

總結

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

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

主站蜘蛛池模板: 亚洲激情在线视频 | 人妻人人澡人人添人人爽 | 成a人片亚洲日本久久 | wwwxxx欧美| 日韩欧美一区二区一幕 | 黄色同人网站 | 深夜小视频在线观看 | 日韩av免费在线观看 | 伊人网av| 男女视频在线观看 | 91麻豆国产视频 | 打美女屁股网站 | 日韩国产在线一区 | 天堂在线中文网 | 日日舔夜夜摸 | 红桃一区二区三区 | 一区二区在线精品 | 成人在线免费小视频 | 最新成人 | 午夜电影天堂 | 一区二区三区观看 | 久久久国产高清 | 成人颜色网站 | 国产乱淫a∨片免费视频 | 97超碰在线资源 | 亚洲第8页| 久久久久极品 | 免费人成视频在线 | 韩日精品在线观看 | 强开小受嫩苞第一次免费视频 | 台湾男男gay做爽爽的视频 | 九一爱爱 | 欧美字幕 | 天堂中文在线观看 | 天天操天天干天天插 | 亚洲图片视频在线 | 国产精品厕所 | 动漫涩涩免费网站在线看 | 欧美大片免费在线观看 | 校园春色综合 | 久久综合九九 | 色综合色综合 | 激情综合网五月婷婷 | 香蕉视频久久久 | 黄色av电影在线观看 | 国产高清视频在线免费观看 | 天天爽天天摸 | 成人高潮视频 | 国产欧美另类 | 免费在线看黄视频 | 欧美极品少妇xxxxⅹ免费视频 | 国产97自拍 | 日韩av影音先锋 | 成人tiktok黄短视频 | 黄色精品一区 | 操小妞 | xxxx性视频| 国产人伦精品一区二区三区 | 青少年xxxxx性开放hg | 欧美福利视频一区 | 精品日本一区二区 | 国产一区二区自拍 | 亚洲中文字幕无码一区二区三区 | 四虎黄色片| 777午夜 | 激情av中文字幕 | 在线国产三级 | 麻豆一区二区三区在线观看 | 久久精品第一页 | 今天高清视频在线观看视频 | 天堂av2018| 成人v| 亚洲综合在线播放 | 精品视频99 | 老司机av福利| 国产ts三人妖大战直男 | 香蕉久久夜色精品国产使用方法 | 福利视频不卡 | 午夜h视频 | 成人小视频免费 | 欧美国产日韩一区二区三区 | 久久天天东北熟女毛茸茸 | 8x8ⅹ成人永久免费视频 | 性毛片| 啪啪免费网站 | 乱色专区 | 国产午夜免费福利 | 又白又嫩毛又多15p 超碰在线一区 | 99久久久久 | 福利国产在线 | 村上里沙番号 | 特级做a爰片毛片免费69 | 男同毛片| 看片在线观看 | www日本www | av在线地址 | 亚洲一区免费观看 | 69午夜| 亚洲欧洲日韩在线 |