Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    nice use of nested numpy functions! but note that this does miss maxima at either end of the array :) Commented Feb 28, 2013 at 17:09
  • 2
    This will also act weird if there are repetitive values. e.g. if you take the array [1, 2, 2, 3, 3, 3, 2, 2, 1], the local maxima is obviously somewhere between the 3's in the middle. But if you run the functions you provided you get maximas at indices 2,6 and minimas at indices 1,3,5,7, which to me doesn't make much sense. Commented Mar 24, 2013 at 21:06
  • 6
    To avoid this +1 instead of np.diff() use np.gradient(). Commented Jan 30, 2015 at 13:41
  • I know this thread is years old, but it's worth adding that if your curve is too noisy, you can always try low-pass filtering first for smoothing. For me at least, most of my local max/min uses are for global max/min within some local area (e,g, the big peaks and valleys, not every variation in the data) Commented Jun 23, 2015 at 17:18
  • @ankostis Note that simply removing the +1 and substituting np.gradient() for np.diff in the code above produces the indices of each minima/maxima as well as their lowest/highest neighbors. Commented Jun 23, 2021 at 23:40