运行或滑动中值,均值和标准差 [英] Running or sliding median, mean and standard deviation

查看:194
本文介绍了运行或滑动中值,均值和标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算大型数组的运行中位数,均值和标准差.我知道如何计算运行平均值,如下所示:

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技巧专门用于查找sumaverage值,并且不要认为可以简单地扩展它来获取medianstd值.在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)

要运行medianstd,只需替换 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆