使用NaN计算numpy数组中的移动平均值 [英] Calculate moving average in numpy array with NaNs

查看:279
本文介绍了使用NaN计算numpy数组中的移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在包含NaN的大型numpy数组中计算移动平均值.目前我正在使用:

I am trying to calculate the moving average in a large numpy array that contains NaNs. Currently I am using:

import numpy as np

def moving_average(a,n=5):
      ret = np.cumsum(a,dtype=float)
      ret[n:] = ret[n:]-ret[:-n]
      return ret[-1:]/n

使用遮罩数组进行计算时:

When calculating with a masked array:

x = np.array([1.,3,np.nan,7,8,1,2,4,np.nan,np.nan,4,4,np.nan,1,3,6,3])
mx = np.ma.masked_array(x,np.isnan(x))
y = moving_average(mx).filled(np.nan)

print y

>>> array([3.8,3.8,3.6,nan,nan,nan,2,2.4,nan,nan,nan,2.8,2.6])

我要寻找的结果(下)理想情况下应该仅在原始数组x具有NaN的位置具有NaN,并且应该对分组中非NaN元素的数量进行平均(我需要一些在函数中更改n大小的方法.)

The result I am looking for (below) should ideally have NaNs only in the place where the original array, x, had NaNs and the averaging should be done over the number of non-NaN elements in the grouping (I need some way to change the size of n in the function.)

y = array([4.75,4.75,nan,4.4,3.75,2.33,3.33,4,nan,nan,3,3.5,nan,3.25,4,4.5,3])

我可以遍历整个数组并按索引检查索引,但是我正在使用的数组很大,这将花费很长时间.有numpythonic的方法来做到这一点吗?

I could loop over the entire array and check index by index but the array I am using is very large and that would take a long time. Is there a numpythonic way to do this?

推荐答案

在您仍然可以使用cumsum来实现此目的之前,我将添加一些很好的答案:

I'll just add to the great answers before that you could still use cumsum to achieve this:

import numpy as np

def moving_average(a, n=5):
    ret = np.cumsum(a.filled(0))
    ret[n:] = ret[n:] - ret[:-n]
    counts = np.cumsum(~a.mask)
    counts[n:] = counts[n:] - counts[:-n]
    ret[~a.mask] /= counts[~a.mask]
    ret[a.mask] = np.nan

    return ret

x = np.array([1.,3,np.nan,7,8,1,2,4,np.nan,np.nan,4,4,np.nan,1,3,6,3])
mx = np.ma.masked_array(x,np.isnan(x))
y = moving_average(mx)

这篇关于使用NaN计算numpy数组中的移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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