pandas 的窗户重叠 [英] Window overlap in Pandas

查看:78
本文介绍了 pandas 的窗户重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在熊猫中,有几种方法可以处理给定窗口中的数据(例如pd.rolling_meanpd.rolling_std.)但是,我想设置一个窗口重叠,我认为这是一个非常标准的要求.例如,在下图中,您可以看到一个窗口,该窗口跨越256个样本,重叠了128个样本.

In pandas, there are several methods to manipulate data in a given window (e.g. pd.rolling_mean or pd.rolling_std.) However, I would like to set a window overlap, which I think, is a pretty standard requirement. For example, in the following image, you can see a window spanning 256 samples and overlapping 128 samples.

如何使用Pandas或Numpy中包含的优化方法来做到这一点?

How can I do that using the optimized methods included in Pandas or Numpy?

推荐答案

使用as_strided您将执行以下操作:

Using as_strided you would do something like this:

import numpy as np
from numpy.lib.stride_tricks import as_strided

def windowed_view(arr, window, overlap):
    arr = np.asarray(arr)
    window_step = window - overlap
    new_shape = arr.shape[:-1] + ((arr.shape[-1] - overlap) // window_step,
                                  window)
    new_strides = (arr.strides[:-1] + (window_step * arr.strides[-1],) +
                   arr.strides[-1:])
    return as_strided(arr, shape=new_shape, strides=new_strides)

如果将一维数组传递给上述函数,它将返回二维数组,其形状为(number_of_windows, window_size),因此您可以进行计算,例如窗口的意思是:

If you pass a 1D array to the above function, it will return a 2D view into that array, with shape (number_of_windows, window_size), so you could calculate, e.g. the windowed mean as:

win_avg = np.mean(windowed_view(arr, win_size, win_overlap), axis=-1)

例如:

>>> a = np.arange(16)
>>> windowed_view(a, 4, 2)
array([[ 0,  1,  2,  3],
       [ 2,  3,  4,  5],
       [ 4,  5,  6,  7],
       [ 6,  7,  8,  9],
       [ 8,  9, 10, 11],
       [10, 11, 12, 13],
       [12, 13, 14, 15]])
>>> windowed_view(a, 4, 1)
array([[ 0,  1,  2,  3],
       [ 3,  4,  5,  6],
       [ 6,  7,  8,  9],
       [ 9, 10, 11, 12],
       [12, 13, 14, 15]])

这篇关于 pandas 的窗户重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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