[英]How can i generate random variables using np.random.zipf for a given range of values?
我有一个给定的价格范围,我使用随机制服从中获得随机生成的随机结果。 我如何引入np.random.zipf
来做同样的事情?
我尝试了以下方法:
a = np.random.zipf((randint(1, 6000000)), size=None)
print(a)
但它似乎没有提供返回值,并且它继续运行代码而没有任何终止
order_total_price_range1 = round(random.uniform(850, 560000), 5)
order_total_price_range2 = round(random.uniform(850, 560000), 5)
我希望从zipf
分布中获得最大值和最小值,但目前没有返回任何结果。
虽然@RobinNicole 是正确的 Zipf 分布,但您可以使用离散采样模拟截断的 Zipf。 顺势而为
import numpy as np
from matplotlib import pyplot as plt
def Zipf(a: np.float64, min: np.uint64, max: np.uint64, size=None):
"""
Generate Zipf-like random variables,
but in inclusive [min...max] interval
"""
if min == 0:
raise ZeroDivisionError("")
v = np.arange(min, max+1) # values to sample
p = 1.0 / np.power(v, a) # probabilities
p /= np.sum(p) # normalized
return np.random.choice(v, size=size, replace=True, p=p)
min = np.uint64(3)
max = np.uint64(8)
q = Zipf(1.2, min, max, 10000)
print(q)
h, bins = np.histogram(q, bins = int(max-min+1),range=(min-0.5,max+0.5))
print(h)
print(bins)
plt.hist(q, bins = bins)
plt.title("Zipf")
plt.show()
会做这样的图表
您不能像使用均匀分布那样调整 Zipf 定律的参数以将其限制在给定的区间内。 原因是 Zipf 分布总是定义在所有正整数的集合上,与其参数无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.