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

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.