在时间序列中查找相似的子序列?

问题描述 投票:1回答:1

我有数千个时间序列(24维数据-一天中的每个小时为1维)。在这些时间序列中,我对看起来像这样的特定子序列或模式感兴趣:

我对与突出显示部分的整体形状相似的子序列感兴趣-即,子序列具有陡峭的负斜率,然后是几个小时的时间段,其中斜率相对平坦,直到最终结束带有明显的正斜率。我知道我感兴趣的子序列不会完全匹配,并且很可能会随时间推移,缩放比例不同,斜率相对平坦的时间段较长/较短等,但是我想找到一种将它们全部检测出来的方法。

为此,我开发了一个简单的启发式算法(基于我对突出显示部分的定义),可以快速找到一些感兴趣的子序列。但是,我想知道是否有一种更优雅的方法(在Python中)搜索我感兴趣的子序列的数千个时间序列(同时考虑到上述问题-时间,比例等方面的差异)。 )?

python time-series sequence heuristics
1个回答
0
投票

下面的功能应该做;用直观的变量和方法名称编写的代码,并且在某些阅读中应该是不言自明的。最后,该代码高效且可扩展。


功能

  • 指定最小和最大平线长度
  • 指定左右尾巴的最小和最大斜率
  • 为多个尾部的左右尾巴指定最小和最大平均斜率

示例

import numpy as np
import matplotlib.pyplot as plt

# Toy data
t = np.array([[ 5,  3,  3,  5,  3,  3,  3,  3,  3,  5,  5,  3,  3,  0,  4,  
                1,  1, -1, -1,  1,  1,  1,  1, -1,  1,  1, -1,  0,  3,  3,  
                5,  5,  3,  3,  3,  3,   3,  5,  7, 3,  3,  5]]).T
plt.plot(t)
plt.show()

# Get flatline indices
indices = get_flatline_indices(t, min_len=4, max_len=5)
plt.plot(t)
for idx in indices:
    plt.plot(idx, t[idx], marker='o', color='r')
plt.show()

# Filter by edge slopes
lims_left  = (-10, -2)
lims_right = (2,  10)
averaging_intervals = [1, 2, 3]
indices_filtered = filter_by_edge_slopes(indices, t, lims_left, lims_right,
                                         averaging_intervals)
plt.plot(t)
for idx in indices_filtered:
    plt.plot(idx, t[idx], marker='o', color='r')
plt.show()


def get_flatline_indices(sequence, min_len=2, max_len=6):
    indices=[]
    elem_idx = 0
    max_elem_idx = len(sequence) - min_len

    while elem_idx < max_elem_idx:
        current_elem = sequence[elem_idx]
        next_elem    = sequence[elem_idx+1]
        flatline_len = 0 # min possible len

        if current_elem == next_elem:
            while current_elem == next_elem:
                flatline_len += 1
                next_elem = sequence[elem_idx + flatline_len]

            if flatline_len >= min_len:
                if flatline_len > max_len:
                    flatline_len = max_len

                trim_start = elem_idx
                trim_end   = trim_start + flatline_len
                indices_to_append = [index for index in range(trim_start, trim_end)]
                indices += indices_to_append

            elem_idx += flatline_len
            flatline_len = 0
        else:
            elem_idx += 1

    return indices if not all([(entry == []) for entry in indices]) else []

def filter_by_edge_slopes(indices, data, lims_left, lims_right,
                          averaging_intervals=1):         
    indices_filtered = []
    indices_temp = []
    tails_temp = []
    got_left, got_right = False, False

    for idx in indices:
        slopes_left, slopes_right = _get_slopes(data, idx, averaging_intervals)

        for tail_left, slope_left in enumerate(slopes_left):
            if _valid_slope(slope_left, lims_left):
                if got_left:
                    indices_temp = []  # discard prev if twice in a row
                    tails_temp.pop(-1)
                indices_temp.append(idx)
                tails_temp.append(tail_left + 1)
                got_left = True

        if got_left:
            for edge_right, slope_right in enumerate(slopes_right):
                if _valid_slope(slope_right, lims_right):
                    if got_right:
                        indices_temp.pop(-1)
                        tails_temp.pop(-1)
                    indices_temp.append(idx)
                    tails_temp.append(edge_right + 1)
                    got_right = True

        if got_left and got_right:
            left_append  = indices_temp[0] - tails_temp[0]
            right_append = indices_temp[1] + tails_temp[1]
            indices_filtered.append(_fill_range(left_append, right_append))
            indices_temp = []
            tails_temp = []
            got_left, got_right = False, False

    return indices_filtered

def _get_slopes(data, idx, averaging_intervals):
    if type(averaging_intervals) == int:
        averaging_intervals = [averaging_intervals]

    slopes_left, slopes_right = [], []
    for interval in averaging_intervals:
        slopes_left  += [(data[idx] - data[idx-interval]) / interval]
        slopes_right += [(data[idx+interval] - data[idx]) / interval]
    return slopes_left, slopes_right

def _valid_slope(slope, lims):
    min_slope, max_slope = lims
    return (slope  >= min_slope) and (slope <= max_slope)

def _iterative_average(_list):
    averages = []
    for i in range(1, len(_list) + 1):
        averages.append(np.mean(_list[:i]))
    return averages

def _fill_range(_min, _max):
    return [i for i in range(_min, _max + 1)]
© www.soinside.com 2019 - 2024. All rights reserved.