【发布时间】:2019-04-05 07:34:48
【问题描述】:
我想计算熊猫数据框每一行的加权中位数。
我找到了这个不错的函数 (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() 来实现?
非常感谢您的宝贵时间!
【问题讨论】:
标签: python pandas numpy percentile weighted