【问题标题】:finding the duration cycles in time series查找时间序列中的持续时间周期
【发布时间】:2021-07-23 09:58:26
【问题描述】:

我有一个很长的时间序列,想确定不同值发生了多少次跳跃和下降:

3 4 5 1 0 3 4 6 7 2 3 0 6 7 4 3 2 1 0 2 3 5 4 5 6 5 7 0 8 2 1 4 0

我想找到每个循环对的频率,例如值从多少次 5 到 0 到 5 3到0到3

等等。

我已经尝试过 cydets 一个库来查找循环,但无法得到我想要的。

from cydets.algorithm import detect_cycles

import pandas as pd
import dask.dataframe as dd

pct_change_series = pct_change_series.round()
cycles = detect_cycles(pct_change_series.compute())
print(cycles)

你能帮忙吗?

【问题讨论】:

  • 所提供示例的期望输出是什么?实际数据都是整数值还是其他数据格式(即浮点数、时间戳等)

标签: python-3.x time-series


【解决方案1】:

我写的这个小脚本应该可以解决问题。我确保不使用任何外部库:

if __name__ == '__main__':
    data = [3,4,5,1,0,3,4,6,7,2,3,0,6,7,4,3,2,1,0,2,3,5,4,5,6,5,7,0,8,2,1,4,0]
    G = {i:[0,0] for i in data} # {i:(status, n_cycles) ... }
    for i in range(len(data)):
        nid, S, C = data[i], *G[data[i]]
        if nid == 0: G.update({i:[2,c] for i,(s,c) in G.items() if s==1}); continue
        elif G[nid][0] == 0: G.update({nid:[1,C]})
        elif G[nid][0] == 2: G.update({nid:[0,C+1]})
    [print(f'[{i}] Number of Cycles: {c}') for i,(s,c) in sorted(list(G.items()),key=lambda x:x[0]) if i>0]

当我运行它时,我得到以下输出:

[1] Number of Cycles: 2
[2] Number of Cycles: 3
[3] Number of Cycles: 3
[4] Number of Cycles: 4
[5] Number of Cycles: 1
[6] Number of Cycles: 2
[7] Number of Cycles: 2
[8] Number of Cycles: 0

对于列表中的每个数字,循环数是数字 x 出现在位置 i 的次数,然后是一些 m em> 个数字之后出现了一个 0,然后另一个 x 在位置 (i+m+n) 的零之后出现了 n 个位置。计算完每个周期后,此过程会为 x 重新启动,这意味着现在下一个 x 周期可能会从位置 (i+m+n)时间>。如果你想让我更详细地解释一下,请 Lmk!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多