繁体   English   中英

Python:数组每一行的加权百分位数

[英]Python: weighted percentile for each row of array

我想计算熊猫数据框每一行的加权中位数。

我发现了这个不错的函数( https://stackoverflow.com/a/29677616/10588967 ),但我似乎无法传递二维数组。

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 = numpy.array(values)
quantiles = numpy.array(quantiles)
if sample_weight is None:
    sample_weight = numpy.ones(len(values))
sample_weight = numpy.array(sample_weight)
assert numpy.all(quantiles >= 0) and numpy.all(quantiles <= 1), 'quantiles should be in [0, 1]'

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

weighted_quantiles = numpy.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 /= numpy.sum(sample_weight)
return numpy.interp(quantiles, weighted_quantiles, values)

使用链接中的代码,以下工作:

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

但是,这不起作用:

values = numpy.random.randn(10,5)
quantiles = [0.0, 0.5, 1.]
sample_weight = numpy.random.randn(10,5)
weighted_quantile(values, quantiles, sample_weight)

我收到以下错误:

weighted_quantiles = np.cumsum(sample_weight) - 0.5 * sample_weight

ValueError:操作数无法与形状一起广播 (250,) (10,5,5)

问题是否可以在数据帧上以矢量化方式应用这个加权分位数函数,或者我只能使用 .apply() 来实现这一点?

非常感谢您的时间!

 np.cumsum(sample_weight)

返回一维列表。 所以你想使用

np.cumsum(sample_weight).reshape(10,5,5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM