滚动MAD的Numpy版本(平均绝对偏差)

时间:2017-03-17 18:48:57

标签: performance pandas numpy vectorization

如何制作以下MAD功能的滚动版本

$mday,$mon,$year,$wday,$yday,$isdst

此代码是this question

的答案

目前我将numpy转换为pandas然后应用此函数,然后将结果转换回numpy

from numpy import mean, absolute

def mad(data, axis=None):
    return mean(absolute(data - mean(data, axis)), axis)

但是在较大的数据帧上这是低效的。如何在没有循环的情况下获得numpy中相同函数的滚动窗口并给出相同的结果?

1 个答案:

答案 0 :(得分:7)

这是一个矢量化的NumPy方法 -

# From this post : http://stackoverflow.com/a/40085052/3293881
def strided_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
    nrows = ((a.size-L)//S)+1
    n = a.strides[0]
    return np.lib.stride_tricks.as_strided(a, shape=(nrows,L), strides=(S*n,n))

# From this post : http://stackoverflow.com/a/14314054/3293881 by @Jaime
def moving_average(a, n=3) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:] / n

def mad_numpy(a, W):
    a2D = strided_app(a,W,1)
    return np.absolute(a2D - moving_average(a,W)[:,None]).mean(1)

运行时测试 -

In [617]: data = np.random.randint(0,9,(10000))
     ...: df = pd.DataFrame(data)
     ...: 

In [618]: pandas_out = pd.rolling_apply(df,90,mad).values.ravel()
In [619]: numpy_out = mad_numpy(data,90)

In [620]: np.allclose(pandas_out[89:], numpy_out) # Nans part clipped
Out[620]: True

In [621]: %timeit pd.rolling_apply(df,90,mad)
10 loops, best of 3: 111 ms per loop

In [622]: %timeit mad_numpy(data,90)
100 loops, best of 3: 3.4 ms per loop

In [623]: 111/3.4
Out[623]: 32.64705882352941

巨大的 32x+ 加速了循环的熊猫解决方案!