Compute k adjacent (plus or minus) elements inside an array

I struggled with the task of having an inequality threshold superimposed on kadjacent elements in an array with a 1-dimensional matrix - I have to add both left and right. For instance:

if x[i+-100] < .05:

This is better than I can formulate it, obviously this will not work. But I hope that my point of view is clearer. In other words, I would like to distinguish if any elements within plus or minus 100 elements of element x have a value greater than 0.05. Here is my entire function for context: (here I have -.05)

def vector_recode(x):
    if (x[i+-100]) > .05 or (x[i+-100]) < -.05:
        return X
    else:
        return 0
recode_function = np.vectorize(vector_recode)

- For clarity, the above function tries to take values ​​that are close enough to zero and reset them to zero, or if there are any nearby [+ -100] elements that have a value greater than .05 in absolute terms, keep x as it is.

I have a drowning feeling that I might have to resort to a loop, but np always surprised me with its efficiency, and I hope someone has an understanding that could help me, others like me, in dealing with kadjacent items.

My other study of this procedure also includes scipy.ndimageconvolution matrices. However, I do not think that this approach is easily applicable to my case - it's just 1D left and right neighboring elements. I used to be wrong. Any help is appreciated.

If this helps, I will try to make a worked-out example:

    >>>A
[0.04,0.03,0.03,0.02,0.04]
    >>>recode_function(A)
[0,0,0,0,0]
    >>>B
[0.03,0.02,0.23,0.01,0.03]
    >>>recode_function(B)
[0.03,0.02,0.23,0.01,0.03]

"" " 100" "" " 5.". "" 0,05 -5,0, . "B" 0,05. 0,23, , x, 5 x. -, . "A" "B" 1 . , - , . , .

+4
3

min-max -0.05 0.05 , , , min-max. . , ANY- . , np.where 0.

, -

from scipy.ndimage.morphology import binary_dilation

def reset_arr(x, W, T): # x: input array, W : window size, T : threshold as +-
    mask = binary_dilation(x.clip(min=-T, max=T) != x,np.ones(2*W+1))
    return np.where(mask, x, 0)

-

In [376]: x
Out[376]: 
array([ 0.06821936,  0.66300942,  0.15449635,  1.52260898,  0.41346868,
       -0.48343499,  0.45386276,  2.1888203 ,  0.36947105, -0.17660172])

In [377]: reset_arr(x, W = 1, T = 0.5)
Out[377]: 
array([ 0.06821936,  0.66300942,  0.15449635,  1.52260898,  0.41346868,
        0.        ,  0.45386276,  2.1888203 ,  0.36947105,  0.        ])
+1

if

if (any(x[i:i+100:1]) > .05) and any((x[i-100:i+1:1]) < -.05):

i, x, vector_recode.

, , , , , .

+1

, Python abs() NumPy :

>>> import numpy as np
>>> abs(np.array([-5, 1, -3.3, -2, 0, 11]))
array([  5. ,   1. ,   3.3,   2. ,   0. ,  11. ])

, >, :

>>> abs(np.array([0.01, 0.07, -0.07, -0.02, 5])) > 0.05
array([False,  True,  True, False,  True], dtype=bool)

Divakar:

from scipy.ndimage.morphology import binary_dilation

def recode_function(data, k, threshold):
    mask = binary_dilation( abs(data) > threshold, np.ones(2 * k + 1) )
    return np.where(mask, data, 0)

, ():

from scipy.ndimage.morphology import binary_dilation

def recode_function(data, k, threshold):
    mask = binary_dilation( abs(data) > threshold, np.ones(2 * k + 1) )
    return mask * data
+1

Source: https://habr.com/ru/post/1663391/


All Articles