[英]generating a log normal distribution using R
我想生成一個對數正態分布,以在我的Python代碼中使用以更改擊中服務器的速率。 誰能指導我生成相同的東西?
除非您決心使用R,否則不需要外部庫。 Python的內置隨機模塊非常適合一般用途。 它可以從各種常見分布中生成隨機數。
import math
import random
#generate 10k lognormal samples with mean=0 and stddev=1
samples = [random.lognormvariate(0,1) for r in xrange(10000)]
#demonstrate the mean and stddev are close to the target
#compute the mean of the samples
log_samples = [math.log(sample) for sample in samples]
mu = sum(log_samples)/len(samples)
#compute the variance and standard deviation
variance = sum([(val-mu)**2 for val in log_samples])/(len(log_samples)-1)
stddev = var**0.5
print('Mean: %.4f' % mu)
print('StdDev: %.4f' % stddev)
#Plot a histogram if matplotlib is installed
try:
import pylab
hist = pylab.hist(samples,bins=100)
pylab.show()
except:
print('pylab is not available')
如果您使用的是Rpy2,則可以開始使用:
import rpy2.robjects as robjects
#reference the rlnorm R function
rlnorm = robjects.r.rlnorm
#generate the samples in R
samples = rlnorm(n=10000, meanlog=1, sdlog=1)
在R中,您可以使用rlnorm
但是為什么不使用numpy並直接在Python中進行呢?
查看此文檔: http : //docs.scipy.org/doc/numpy/reference/generated/numpy.random.lognormal.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.