运行或滑动中值,均值和标准差 [英] Running or sliding median, mean and standard deviation
问题描述
我正在尝试计算大型数组的运行中位数,均值和标准差.我知道如何计算运行平均值,如下所示:
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
这非常有效.但是我不太明白为什么(cumsum[N:] - cumsum[:-N]) / float(N)
可以给出平均值(我是从其他人那里借来的).
我试图添加另一个返回语句来计算中位数,但是它没有达到我想要的效果.
return (cumsum[N:] - cumsum[:-N]) / float(N), np.median(cumsum[N:] - cumsum[:-N])
有人能给我一些解决这个问题的提示吗?非常感谢你.
张焕年
该cumsum
技巧专门用于查找sum
或average
值,并且不要认为可以简单地扩展它来获取median
和std
值.在1D
数组的滑动/运行窗口中执行通用ufunc
操作的一种方法是创建一系列以2D数组堆叠的基于1D滑动窗口的索引,然后沿堆叠应用ufunc
轴.要获取这些索引,可以使用 broadcasting
. /p>
因此,为了执行均值,它看起来像这样-
idx = np.arange(N) + np.arange(len(x)-N+1)[:,None]
out = np.mean(x[idx],axis=1)
要运行median
和std
,只需替换 np.mean
和 np.median
和 np.std
.
I am trying to calculate the running median, mean and std of a large array. I know how to calculate the running mean as below:
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
This works very efficiently. But I do not quite understand why (cumsum[N:] - cumsum[:-N]) / float(N)
can give the mean value (I borrowed from someome else).
I tried to add another return sentence to calculate the median, but it does not do what I want.
return (cumsum[N:] - cumsum[:-N]) / float(N), np.median(cumsum[N:] - cumsum[:-N])
Does anyone offer me some hint to approach this problem? Thank you very much.
Huanian Zhang
That cumsum
trick is specific to finding sum
or average
values and don't think you can extend it simply to get median
and std
values. One approach to perform a generic ufunc
operation in a sliding/running window on a 1D
array would be to create a series of 1D sliding windows-based indices stacked as a 2D array and then apply the ufunc
along the stacking axis. For getting those indices, you can use broadcasting
.
Thus, for performing running mean, it would look like this -
idx = np.arange(N) + np.arange(len(x)-N+1)[:,None]
out = np.mean(x[idx],axis=1)
For running median
and std
, just replace np.mean
with np.median
and np.std
respectively.
这篇关于运行或滑动中值,均值和标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!