numpy 加速滑动窗口和列表之间的匹配

vlurs2pr  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我有一个奇数长度的窗口和一个数组,我需要找到窗口居中的数组的索引,并且匹配更多。到目前为止,我用下面的代码来做。有可能加快这些计算吗?

import numpy as np
import time

def find_best_match_position(lst, window):
    min_corr = np.inf
    best_position = -1

    for i in range(len(lst) - len(window) + 1):
        window_subset = lst[i:i + len(window)]
        corr = np.linalg.norm(window - window_subset)
        if corr < min_corr:
            min_corr = corr
            best_position = i

    return best_position

input_list_len = int(8E+6)
np.random.seed(2)
input_list = np.random.rand(input_list_len)

win_length = 31
np.random.seed(4)
window = np.random.rand(win_length)

gnd_index = 15

half_width = win_length // 2
start = gnd_index - half_width  # Shift start by 1 to the right
end = gnd_index + half_width + 1
input_list[start:end] = window + 0.01 * np.random.rand(win_length)

t = time.time()
print(f'Computed index {find_best_match_position(input_list, window) + half_width}')
t1 = time.time()
print(f'Computation time {(t1 - t) / 60} min')
# Computed index 15
# Computation time 0.6488747239112854 min

字符串

n6lpvg4x

n6lpvg4x1#

在我的测试中,这比以前快了10倍。

def find_best_match_position(lst, window):
    slides = np.lib.stride_tricks.sliding_window_view(lst, len(window))
    norms = np.linalg.norm(window - slides, axis=1)
    return norms.argmin()

字符串
更快,使用更少的内存:

def find_best_match_position(lst, window):
    return sum(
        (lst[i : i-len(window)+1 or None] - w) ** 2
        for i, w in enumerate(window)
    ).argmin()

相关问题