两个Numpy数组的向量化Lookback

watbbzwu  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(94)

是否可以使用两个Numpy数组对lookback进行向量化?在下面的示例中,我需要“循环”first_parameter中的每个值,并检查前面的4个值中是否包含来自second_parameter的3个或更多值。如果条件为true,则first_parameter中的值应添加到result_parameter中。
如果first_bytes中的值少于4个,它应该检查存在多少个值(尽管如果少于3个,条件显然永远不会为真)。
实际的阵列将更大,所以我正在寻找一个矢量化的解决方案。
示例一:

first_arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
second_arr = [2, 4, 5, 11, 13]

result_arr = [6]

字符串
例二:

first_arr = [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
second_arr = [14, 15, 22, 23, 24, 26, 30, 31, 32]

result_arr = [25, 26, 27, 33]

eyh26e7m

eyh26e7m1#

你可以这样做:

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

first_arr = [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
second_arr = [14, 15, 22, 23, 24, 26, 30, 31, 32]

lookback = 4  # lookback length
contains = 3  # number of intersecting values

# pad the first array at the front (pad with a number that's
definitely not in second_arr)
farr = np.pad(
    first_arr,
    (lookback - contains, 0),
    "constant",
    constant_values=np.sum(np.abs(second_arr)) + 1
)

result_arr = [
    row[-1]
    for row in sliding_window_view(farr, lookback + 1)
    if len(np.intersect1d(row[:lookback], second_arr)) >= contains
]

字符串
这将创建初始数组的sliding window view并检查intersecting values的数量。它仍然包含for循环,因此可能存在其他NumPy向量化。

wyyhbhjk

wyyhbhjk2#

下面是一个向量化的函数--只使用numpy:

import numpy as np
def sliding_window_contains(first_arr, second_arr, lookback, contains):
    s = np.r_[False, np.in1d(first_arr, second_arr)]
    size = s.size - lookback, lookback + 1
    idx = np.lib.stride_tricks.as_strided(s, size, s.strides*2)[:,:-1].sum(1)>=contains
    return np.array(first_arr[lookback - 1:])[idx]
first_arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
second_arr1 = [2, 4, 5, 11, 13]
sliding_window_contains(first_arr1, second_arr1, 4, 3)
array([6])
first_arr2 = [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
second_arr2 = [14, 15, 22, 23, 24, 26, 30, 31, 32]
sliding_window_contains(first_arr2, second_arr2, 4, 3)
array([25, 26, 27, 33])

相关问题