python - 如何检测时间序列中的翻转?

标签 python time-series

对于在不同操作模式下运行的系统,我想减少模式之间的翻转。为此,需要检测翻转和校正。
假设我们有一个模式频繁切换的序列:

before = [0,0,0,(1,0),1,1,1,2,2,(1,2),1,1,1,(0,1),0]   # parenthesis indicate flipping
我想只在(改变点)进行交换以获得:
after  = [0,0,0,(0,1),1,1,1,2,2,(2,1),1,1,1,(1,0),0]   # parenthesis indicate corrections
我怎样才能做到这一点?

最佳答案

有一种“更简单”的方法可以找到范围 1 的翻转,但不适用于连续重复翻转

before = np.array([0,0,1,0,0,1,0,1,1,1,2,2,1,2,1,1,1,0,1,0,0])
goal = np.array(  [0,0,0,0,0,0,1,1,1,1,2,2,2,1,1,1,1,1,0,0,0])

diff = np.diff(before)          # find 'jumps' with range 1
diff[:-1][(diff == 0)[1:]] = 0  # correct 'right' side of 'jumps'
after = before - np.r_[0,diff]  # correct flipping

plt.plot(before, linestyle='--',label='before')
plt.plot(goal,   linestyle='-.',label='goal')
plt.plot(after,  linestyle=':', label='after')
plt.legend();
输出
smothed signal
适用于更大的步数和负“跳跃”
before = np.array([0,0,-1,0,0,1,0,1,1,1,4,4,1,4,1,1,1,0,1,0,0])
goal = np.array(  [0,0, 0,0,0,0,1,1,1,1,4,4,4,1,1,1,1,1,0,0,0])

diff = np.diff(before)          # find flipping with range 1
diff[:-1][(diff == 0)[1:]] = 0  # correct 'right' side of signal
after = before - np.r_[0,diff]  # correct flipping

plt.plot(before, linestyle='--',label='before')
plt.plot(goal,   linestyle='-.',label='goal')
plt.plot(after,  linestyle=':', label='after')
plt.legend();
输出
with bigger steps

解决方案的局限性
  • 连续重复翻转
  •     before = [0,1,0,1,0,1,1,1,1]
        goal   = [0,0,0,0,1,1,1,1,1]
    
    wrong result

    关于python - 如何检测时间序列中的翻转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63990101/

    相关文章:

    python - 使用 pandas 时间序列的过去 n 小时的变化率

    database - 在数据库中存储二维时间序列的最有效方法是什么(sqlite3)

    artificial-intelligence - 如何计算噪声时间序列数据的斜率

    mysql - 使用 Between 进行连接查询的限制

    python - 为什么这个字符串比较返回 False?

    javascript - 读取 Flask 未设置的 cookie

    python - 通过 Python 使用 BULK INSERT

    python - 当我打开 IDLE 时出现 "Cannot update File menu Recent Files list [Errno 13] Permission denied: recent-files.lst"(Python 3.4 GUI)

    python - 如何在 SQLAlchemy 中进行跨数据库查询连接?

    python - 与日期分开导入时间数据并且不添加日期