python 均匀分布_如何在Python中生成对数均匀分布?

I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, exp(min), exp(max), base] that returns n log uniformly distributed in the range exp(min) and exp(max).

The closest I found though was numpy.random.uniform.

解决方案In a loguniform distribution, the logtransformed random variable is assumed to be uniformly distributed.

Thus

logU(a, b) ~ exp(U(log(a), log(b))

Thus, we could create a log-uniform distribution using numpy:

def loguniform(low=0, high=1, size=None):

return np.exp(np.random.uniform(low, high, size))

If you want to choose a different base, we could define a new function as follows:

def lognuniform(low=0, high=1, size=None, base=np.e):

return np.power(base, np.random.uniform(low, high, size))

EDIT: @joaoFaria's answer is also correct.

def loguniform(low=0, high=1, size=None):

return scipy.stats.reciprocal(np.exp(low), np.exp(high)).rvs(size)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值