Xarray rolling mean with weights

前端 未结 3 597
执念已碎
执念已碎 2021-01-01 07:46

When I do running / rolling mean with weights in numpy, I e.g. do something like this:

data = np.random.random(100)  # Example data...
weights = np.array([1,         


        
相关标签:
3条回答
  • 2021-01-01 08:32

    The weighted-rolling-mean is not yet implemented in xarray.

    The following does almost the same thing but it would be quite slow. I think the use of np.convolve is the current best choice.

    def weighted_sum(x, axis):
        weight = [1, 2, 1]
        if x.shape[axis] == 3:
            return np.sum(x * weight, axis=axis)
        else:
            return np.nan
    
    da.rolling(dim_0=3, center=True).reduce(weighted_sum)
    

    Currently, we are working to support more flexible (and faster) rolling operations. See https://github.com/pydata/xarray/pull/1837

    EDIT:

    With xarray=0.10.2, weighted rolling mean can be computed as follows,

    weight = xr.DataArray([0.25, 0.5, 0.25], dims=['window'])
    da.rolling(dim_0=3, center=True).construct('window').dot(weight)
    

    where construct method constructs a view of the rolling object, where the window dimension (named window in the above example) is attatched to the last position. inner product with the weight array gives the weighted sum along the window dimension.

    0 讨论(0)
  • 2021-01-01 08:46

    If you want a Gaussian-like filter, another hack is to apply the rolling mean recursively.

    Infinite recursions of the boxcar filter (i.e., our rolling mean) becomes a Gaussian filter. See B-spline in wikipedia for the detail.

    Example:

    x = xr.DataArray([0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dims=['x'])
    
    # With window=2
    tmp = x
    plt.plot(tmp, '-ok', label='original')
    for i in range(3):
        tmp = tmp.rolling(x=2, min_periods=1).mean()
        plt.plot(tmp, '-o', label='{}-times'.format(i+1))
    plt.legend()
    

    # with window=3, center=True
    tmp = x
    plt.plot(tmp, '--ok', label='original')
    for i in range(3):
        tmp = tmp.rolling(x=3, center=True, min_periods=1).mean()
        plt.plot(tmp, '-o', label='{}-times'.format(i+1))
    plt.legend()
    

    Note: if you want to centralize the result, use the odd window size.

    0 讨论(0)
  • 2021-01-01 08:48

    This is specific for the [1,2,1] weights, and it requires two steps, so it is not the best solution, but it is quite quick:

    dim_name = "dim_0"
    da_mean = da.rolling(**{dim_name: 3, "center": True}).mean(dim=dim_name)
    da_mean = (3 * da_mean + da) / 4.  # Expand it, and add the middle value.
    
    0 讨论(0)
提交回复
热议问题