Categories
Python Answers

How to find local maxima or minima with Numpy in a 1D Numpy array with Python?

Spread the love

Sometimes, we want to find local maxima or minima with Numpy in a 1D Numpy array with Python.

In this article, we’ll look at how to find local maxima or minima with Numpy in a 1D Numpy array with Python.

How to find local maxima or minima with Numpy in a 1D Numpy array with Python?

To find local maxima or minima with Numpy in a 1D Numpy array with Python, we can use the argrelextrema methods.

For instance, we write

import numpy as np
from scipy.signal import argrelextrema

x = np.random.random(12)

argrelextrema(x, np.greater)
argrelextrema(x, np.less)

to create a NumPy array with

x = np.random.random(12)

Then we find the local maxima of array x by calling argrelextrema with x and np.greater.

Likewise, we find the local minima of array x by calling argrelextrema with x and np.less.

Conclusion

To find local maxima or minima with Numpy in a 1D Numpy array with Python, we can use the argrelextrema methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *