gpt4 book ai didi

python - 使用 numpy 的加权百分位数

转载 作者:IT老高 更新时间:2023-10-28 20:49:23 42 4
gpt4 key购买 nike

有没有办法使用 numpy.percentile 函数来计算加权百分位数?或者有人知道计算加权百分位数的替代 python 函数吗?

谢谢!

最佳答案

完全向量化的 numpy 解决方案

这是我使用的代码。这不是一个最佳方案(我无法用 numpy 编写),但仍然比公认的解决方案更快、更可靠

def weighted_quantile(values, quantiles, sample_weight=None, 
values_sorted=False, old_style=False):
""" Very close to numpy.percentile, but supports weights.
NOTE: quantiles should be in [0, 1]!
:param values: numpy.array with data
:param quantiles: array-like with many quantiles needed
:param sample_weight: array-like of the same length as `array`
:param values_sorted: bool, if True, then will avoid sorting of
initial array
:param old_style: if True, will correct output to be consistent
with numpy.percentile.
:return: numpy.array with computed quantiles.
"""
values = np.array(values)
quantiles = np.array(quantiles)
if sample_weight is None:
sample_weight = np.ones(len(values))
sample_weight = np.array(sample_weight)
assert np.all(quantiles >= 0) and np.all(quantiles <= 1), \
'quantiles should be in [0, 1]'

if not values_sorted:
sorter = np.argsort(values)
values = values[sorter]
sample_weight = sample_weight[sorter]

weighted_quantiles = np.cumsum(sample_weight) - 0.5 * sample_weight
if old_style:
# To be convenient with numpy.percentile
weighted_quantiles -= weighted_quantiles[0]
weighted_quantiles /= weighted_quantiles[-1]
else:
weighted_quantiles /= np.sum(sample_weight)
return np.interp(quantiles, weighted_quantiles, values)

例子:

weighted_quantile([1, 2, 9, 3.2, 4], [0.0, 0.5, 1.])

数组([ 1. , 3.2, 9. ])

weighted_quantile([1, 2, 9, 3.2, 4], [0.0, 0.5, 1.], sample_weight=[2, 1, 2, 4, 1])

数组([ 1. , 3.2, 9. ])

关于python - 使用 numpy 的加权百分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844024/

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