Home > Back-end >  Indexing: Expression for increasing sequence in Numpy NDArray
Indexing: Expression for increasing sequence in Numpy NDArray

Time:01-15

Very new to numpy, and I couldn't figure this one out with Numpy Documentation. Let's say I have defined a NDArray like this:

import numpy as np
a = np.array([100, 90, 80, 70, 60, 50, 51, 52, 53, 54, 55, 40, 30, 20, 10])

The sequence in this array is, for the most part, decreasing in value (100, 90, 80...). However, from Index 5 to 10 it is increasing from 50 to 55.

My question: How can I search for that sequence with a numpy expression? (Let's say I don't know where the increasing sequence is and I need to find it).

I do not want to use python list comprehensions for this!

Thanks

CodePudding user response:

You can use diff first, then look for all positive value with where:

increasing_indices = np.where( np.diff(a) > 0)[0]
  •  Tags:  
  • Related