gpt4 book ai didi

python - 逆加权分布

转载 作者:行者123 更新时间:2023-12-01 05:45:42 24 4
gpt4 key购买 nike

我正在寻找一种进行逆加权分布的方法。这是我的简单线性加权分布的代码:

total = 0
cumulative_distribution = []

for value in distribution:
total += value
cumulative_distribution.append(total)

selected = total * random.random()

index = 0
while cumulative_distribution[index] < selected:
index += 1

return index

我如何反转它,以便列表中权重最小的项目被选择的概率最高?有什么方法可以使事物正常化并进行切换吗?

最佳答案

正如评论中提到的,这实际上取决于您想要如何衡量它们。使用您的声明:

the smallest weights have the highest probability of being chosen

@Blckknght 和我都有同样的想法,只需用其倒数对 PDF 中的每个点进行加权即可。我建议通过像这样的参数对它们进行加权

inverse_PDF = 1/(PDF + delta)

其中 delta 是您可以根据自己的喜好进行控制的参数。如果delta=0,那么PDF中原始权重为零的任何点都会抛出ZeroDivisionError,这通常是不受欢迎的。下面是一些使用 numpy 实现上述内容的示例代码:

import numpy as np

# Generate a random points
pts = np.random.normal(size=(10**6,))

# Compute a PDF
PDF,bins = np.histogram(pts, bins=50)

# Normalize (could have used normed=True in hist)
PDF = PDF / np.trapz(PDF, bins[1:])

# Create the inverse distribution
delta = .1
inverse_PDF = 1/(PDF + delta)

# Normalize
inverse_PDF = inverse_PDF / np.trapz(inverse_PDF, bins[1:])

# Plot the results
import pylab as plt
plt.subplot(211)
plt.plot(bins[1:],PDF,lw=4,alpha=.7)
plt.title("Original Distribution")
plt.subplot(212)
plt.plot(bins[1:],inverse_PDF,lw=4,alpha=.7)
plt.title(r"'Inverse' Distribution with $\delta=%.3f$" % delta)
plt.tight_layout()
plt.show()

enter image description here

关于python - 逆加权分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com