Issue
Is it possible to vectorize lookbacks using two Numpy arrays? In example one below I need to "loop" through each value in first_arr and check if any of the previous 4 values contain 3 or more values from second_arr. If the condition is true the value in first_arr should be added to result_arr.
If there are fewer than 4 values in first_arr it should check however many values exist (although obviously the condition will never be true if less than 3).
The actual arrays will be much larger so I am looking for a vecotorized solution.
Example one:
first_arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
second_arr = [2, 4, 5, 11, 13]
result_arr = [6]
Example two:
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]
Solution
Below is a vectorized function-- only using 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])
Answered By - Onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.