python - Pandas/Numpy: Calculate current state series based on binary signals -
I have 2 chains of binary "signal", let's call them "entry" and "steve".
Entry == 1 means adding from current to 1 (for some maximum time) and staying with == 0 to set the current position in 0.
Entry: 0 My code now calculates a combined current state: State: 0 1 2 2 0 1 Currently, I use the following code, unfortunately, it is very slow (depending on the maximum time) (PDS time series in state / stay / entry):
State = copy.deepcopy (entry) state [stay == 0] = 0 # state first run [(entry.shift (1) == 1) & amp; (Stay == 1)] + = 1 # Maximum time (2, maximum_time + 1) second for the interval in the range: state [(entry.shift (lag) == 1) & amp; (Pd.rolling_mean (stay, interval) == 1)] + = 1 How to vector this code for better performance? thanks a lot! Finally, using some nimety functions, now found a solution:
def calc_state_series (insertion, stay, max_time = 5): reduce = (copy.deepcopy * entry) * 0) .filana (0) # to enter initalization ([entry.shift (max_time) == 1 ) & Amp; ; (Pd.rolling_mean (stay, max_time) == 1)] - = 1 Entry = (Entry + .shift (1)). Fill (0) #readuce condition max_time x = entry.values x = np.concatenate (([0], x) y = stay.values y = np.concatenate (([0], y)) Nan = y == 0x = np.array (x) x [nans] = 0 reset_idx = np .zeros (len (x), dtype = int) reset_idx [nans] = np.arange (len (x)) [ nans] reset_idx = np.maximum.accumulate (reset_idx) cumsum = np.cumsum (x) cumsum = cumsum - cumsum [reset_idx] returns pd.Series (cumsum [1:], index = entry.index) < / Pre> I manage to avoid the loop and this solution (depends on max_time) is still 100x faster for me - but probably still possible to optimize.
Comments
Post a Comment