I have a 1d numpy array It's mostly decreasing, but it increases in a few places I'm interested in the places where it increases in several consecutive elements, and how many consecutive elements it increases for in each case In other words, I'm interested in the lengths of increasing contiguous sub-arrays I'd like to compute and store this information in an array with the same shape as the input (EG that I could use for plotting) This could be achieved using cumsum on a binary mask, except I want to reset the accumulation every time the array starts decreasing again See example input and expected output below How do I do that? import numpy as np def count_consecutive_increases(y: np.ndarray) -> np.ndarray: ... y = np.array([9, 8, 7, 9, 6, 5, 6, 7, 8, 4, 3, 1, 2, 3, 0]) c = count_consecutive_increases(y) print(y) print(c) # >>> [9 8 7 9 6 5 6 7 8 4 3 1 2 3 0] # >>> [0 0 0 1 0 0 1 2 3 0 0 0 1 2 0] Continue reading...