jeudi 22 juillet 2021

Conditional variable with np.where, while loop or nested if statement

I have the following soil moisture time series data (index given in place of time-date stamp). I am trying to identify initial large increases and when the soil is saturated at both sensors (sm2 and sm8 plus 1-hour and 2-hour changes in the values). I am using np.where but I need to additionally identify when these conditions are met and continue increases by >= 0.1 over the previous 3 values until that condition is not met. I am classifying these as water_release = 1 when true and 0 when false to use for machine learning so the values need to be output into its own column in the data frame to later split and test the data. For the data below all of the values except the last three should be identified as 1. Should I use a for loop, while or if statements?

CLASSIFICATION CODE:

manual['water_release1'] = np.where((manual['delta_sm2']>0.5) | (manual['delta_sm8']>0.5), 1, 0)
manual['water_release2'] = np.where((manual['delta_sm2_2h']>=1) | (manual['delta_sm8_2h']>=1), 1, 0)
manual['saturation'] = np.where((manual['sh_sm2_auto_%']>=39) & (manual['sh_sm8_auto_%']>=39), 1, 0)
manual['water_release_target'] = np.where((manual['water_release1'] == 1) | (manual['water_release2'] == 1) | (manual['saturation'] == 1), 1,0)

DATA:

index       sm2     sm8     sm20    delta_sm2   delta_sm2_2h    delta_sm8   delta_sm8_2h
    2170    21.3    31.79   40.15   2.25    2.25    0.14    0.14
    2171    28.1    32.8    40.3    6.80    9.05    1.0     1.14
    2172    33.9    34.1    40.4    5.79    12.59   1.30    2.30
    2173    35.2    34.8    40.4    1.30    7.10    0.69    2.0
    2174    35.95   37.45   40.4    0.75    2.05    2.65    3.35
    2175    36.59   40.2    40.4    0.64    1.39    2.75    5.40
    2176    36.9    40.65   40.4    0.30    0.94    0.45    3.20
    2177    37.25   40.7    40.4    0.35    0.65    0.04    0.5
    2178    37.5    40.7    40.4    0.25    0.60    0.0     0.04
    2179    37.55   40.75   40.4    0.04    0.29    0.04    0.04
    2180    37.7    40.8    40.4    0.15    0.20    0.04    0.09
    2181    37.9    40.8    40.4    0.19    0.35    0.0     0.04
    2182    38.05   40.8    40.4    0.14    0.34    0.0     0.0
    2183    38.1    40.8    40.4    0.05    0.20    0.0     0.0
    2184    38.15   40.8    40.4    0.05    0.10    0.0     0.0
    2185    38.25   40.8    40.4    0.09    0.14    0.0     0.0
    2186    38.5    40.84   40.4    0.25    0.34    0.04    0.04
    2187    38.75   40.9    40.4    0.25    0.5     0.05    0.10
    2188    38.8    40.9    40.4    0.04    0.29    0.0     0.05
    2189    38.84   40.9    40.4    0.04    0.09    0.0     0.0
    2190    38.9    40.9    40.4    0.05    0.10    0.0     0.0
    2191    38.9    40.9    40.4    0.0     0.056   0.0     0.0
    2192    38.9    40.9    40.4    0.0     0.0     0.0     0.0
    2193    38.95   40.9    40.4    0.05    0.05    0.0     0.0
    2194    38.95   40.9    40.4    0.0     0.05    0.0     0.0
    2195    38.95   40.9    40.4    0.0     0.0     0.0     0.0
    2196    38.15   40.9    40.4    -0.80   -0.80   0.0     0.0
    2197    38.95   40.9    40.4    -2.39   -3.20   0.0     0.0
    2198    33.3    40.9    40.4    -2.45   -4.85   0.0     0.0
    2199    31.6    40.9    40.4    -1.69   -4.14   0.0     0.0
    2200    30.15   40.9    40.4    -1.45   -3.14   0.0     0.0

Aucun commentaire:

Enregistrer un commentaire