I have a np.array with float values and a mask (True/False) and I want to calculate the difference between data in the array marked by the mask with "True" and the follow up value in the np.array. If the difference is under my defined threshold of 600, the follow up value should also be marked in the mask with "True". The usecase is for anomaly detection of two anomalies next to each other, since my algorithm is based on gradients and therefore currently not working for this specific case.
For example:
x = [1.5 16000 16100 2.5 NaN 3.1 3.4 -15000 4.1 NaN]
mask = [False True False False False False False True False False]
threshold = 600
The calculation in this case should be: abs(16000)-abs(16100) = 100 100<threshold = True
and abs(-15000)-4.1=14995.9 14995.9>threshold=True
Output:
mask_new = [False True True False False False False True False False]
It should also be considered, that the follow up value might sometimes be "NaN" and in this case the mask should stay "False". There might be up two 5 Trues behind each other.
I was thinking about using np.where, but I don't know how to calculate the difference to the follow up value within a np.where-function.
Can anyone help me to solve this?
You can use
np.isnan()[1]to check if the value isNaN. Then, based on the check you can calculate the difference.