根据指数分布生成数据

时间:2019-10-30 08:47:55

标签: python pandas numpy

我想生成一个包含(50-5000)范围内的30个条目的数据集,使其遵循递增的曲线(对数曲线),即开始时增加,然后结束时停滞。

我遇到了from scipy.stats import expon,但是我不确定如何在我的方案中使用该程序包。

任何人都可以帮忙。

可能的输出看起来像[300, 1000, 1500, 1800, 1900, ...]

1 个答案:

答案 0 :(得分:0)

首先,您需要(均匀)生成30个随机x值。然后,您得到log(x)。理想情况下,log(x)应该在[50, 5000)范围内。但是,在这种情况下,您将需要e^50 <= x <= e^5000(溢出!)。一种可能的解决方案是在[min_x, max_x)中生成随机x值,获取对数值,然后将其缩放到所需范围[50, 5000)

import numpy as np

min_y = 50
max_y = 5000
min_x = 1
# any number max_x can be chosen
# this number controls the shape of the logarithm, therefore the final distribution
max_x = 10

# generate (uniformly) and sort 30 random float x in [min_x, max_x)
x = np.sort(np.random.uniform(min_x, max_x, 30))
# get log(x), i.e. values in [log(min_x), log(max_x))
log_x = np.log(x)
# scale log(x) to the new range [min_y, max_y)
y = (max_y - min_y) * ((log_x - np.log(min_x)) / (np.log(max_x) - np.log(min_x))) + min_y
相关问题