我想生成一个包含 30 个条目的数据集,范围在 (50-5000) 之间,使其遵循递增曲线(对数曲线),即在开始时增加,然后在最后停滞。
我遇到了 from scipy.stats import expon
但我不确定如何在我的场景中使用该包。
谁能帮忙。
可能的输出如下所示:[300, 1000, 1500, 1800, 1900, ...]
。
最佳答案
首先你需要生成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
关于python - 根据指数分布生成数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58621505/