问题描述
我有给定的价格范围,我使用了随机制服从中获取随机生成的随机结果.我如何引入np.random.zipf
做同样的事情?
I have a given price range and i had used random uniform to get random generated random results from it. How can i introduce np.random.zipf
to do the same ?
我尝试了以下方法:
a = np.random.zipf((randint(1, 6000000)), size=None)
print(a)
但是它似乎没有提供任何返回值,并且它在不终止的情况下继续运行代码
But it seems to be providing no return values, and it keeps running the code without any termination
order_total_price_range1 = round(random.uniform(850, 560000), 5)
order_total_price_range2 = round(random.uniform(850, 560000), 5)
我希望从zipf
分布中获取最大值和最小值,但目前未返回任何结果.
I expected to get max and min values from the zipf
distribution, but currently not getting any results returned.
推荐答案
虽然@RobinNicole在Zipf分布中是正确的,但是您可以使用离散采样来模拟截断的Zipf.沿线
While @RobinNicole is right wrt Zipf distribution, you could simulate truncated Zipf using discrete sampling. Along the lines
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()
将制作这样的图形
这篇关于如何使用np.random.zipf为给定的值范围生成随机变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!