gpt4 book ai didi

python - 如何在 pandas 数据框中应用递归数字过滤器?

转载 作者:行者123 更新时间:2023-12-02 01:59:16 24 4
gpt4 key购买 nike

我有一个像这样的数据框:

days1 = pd.date_range('2020-01-01 01:00:00','2020-01-01 01:19:00',freq='60s')

DF = pd.DataFrame({'Time': days1,
'TimeSeries1': [10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
'TimeSeries2': [11, 12, 13, 12, 11, 14, 15, 16, 21, 20, 20, 23, 15, 15, 15, 15, 15, 15, 15, 15]})

我想得到以下内容:

  1. 对于每个 TimeSeries 列(TimeSeries1 和 TimeSeries2),我想创建一个对应的“_Filtered”列,即:TimeSeries1_Filtered[i] = (1-A)* TimeSeries1_Filtered[i-1] + A*TimeSeries1[i]

“A”是介于 0 和 1 之间的过滤因子。

  • 对于每一列,我需要使用不同的“A”因子。例如:TimeSeries1 的 A1=0.5,TimeSeries1 的 A2=0.8。

  • 我有超过 100 个“TimeSeriesN”列,因此最好以元组或列表的形式传递“A#”参数。

  • A1=0.5 的示例

                          Time  TimeSeries1  TimeSeries1_Filtered
    0 2020-01-01 01:00:00 10 10
    1 2020-01-01 01:01:00 10 10
    2 2020-01-01 01:02:00 10 10
    3 2020-01-01 01:03:00 20 15
    4 2020-01-01 01:04:00 20 17.5
    5 2020-01-01 01:05:00 20 18.75
    6 2020-01-01 01:06:00 20 19.375
    7 2020-01-01 01:07:00 20 19.6875
    8 2020-01-01 01:08:00 20 19.84375
    9 2020-01-01 01:09:00 20 19.92188
    10 2020-01-01 01:10:00 20 19.96094
    11 ... ... ...

    谢谢!

    编辑:修正滤波器符号和方程。感谢@not_speshal 的提醒。

    最佳答案

    对于第 n 个数据点,递归公式的计算结果为:

    filtered[n] = A*(x[n] + (1-A)*x[n-1] + (1-A)**2 * x[n-2] +...) + (1-A)**n * x[0]

    您现在可以创建一个返回上述内容的自定义函数并将其应用到您的数据帧:

    def ts_filter(srs, A):
    return srs.expanding().apply(lambda x: A*(x*((1-A)**np.arange(len(x))[::-1])).sum() + (1-A)**x.size*x.iat[0])

    factors = {"TimeSeries1": 0.5, "TimeSeries2": 0.2}
    filtered = df.filter(like="TimeSeries").apply(lambda x: ts_filter(x, A=factors[x.name]))

    output = df.join(filtered, rsuffix="_filtered")
    输出:
    >>> output
    Time TimeSeries1 ... TimeSeries1_filtered TimeSeries2_filtered
    0 2020-01-01 01:00:00 10 ... 10.000000 11.000000
    1 2020-01-01 01:01:00 10 ... 10.000000 11.200000
    2 2020-01-01 01:02:00 10 ... 10.000000 11.560000
    3 2020-01-01 01:03:00 20 ... 15.000000 11.648000
    4 2020-01-01 01:04:00 20 ... 17.500000 11.518400
    5 2020-01-01 01:05:00 20 ... 18.750000 12.014720
    6 2020-01-01 01:06:00 20 ... 19.375000 12.611776
    7 2020-01-01 01:07:00 20 ... 19.687500 13.289421
    8 2020-01-01 01:08:00 20 ... 19.843750 14.831537
    9 2020-01-01 01:09:00 20 ... 19.921875 15.865229
    10 2020-01-01 01:10:00 20 ... 19.960938 16.692183
    11 2020-01-01 01:11:00 20 ... 19.980469 17.953747
    12 2020-01-01 01:12:00 20 ... 19.990234 17.362997
    13 2020-01-01 01:13:00 20 ... 19.995117 16.890398
    14 2020-01-01 01:14:00 20 ... 19.997559 16.512318
    15 2020-01-01 01:15:00 20 ... 19.998779 16.209855
    16 2020-01-01 01:16:00 20 ... 19.999390 15.967884
    17 2020-01-01 01:17:00 20 ... 19.999695 15.774307
    18 2020-01-01 01:18:00 20 ... 19.999847 15.619446
    19 2020-01-01 01:19:00 20 ... 19.999924 15.495556

    关于python - 如何在 pandas 数据框中应用递归数字过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69197849/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com