Finding Local Maxima/Minima in a 1D Numpy Array with Numpy
Identifying local maxima and minima in a 1D numpy array is a common task in signal processing and data analysis. While a simple approach involves comparing an element with its nearest neighbors, a more robust solution is sought within the numpy/scipy libraries.
Solution Using SciPy's argrelextrema
In SciPy versions 0.11 onwards, the argrelextrema function provides an efficient way to find local extrema in a 1D array:
1 2 3 4 5 6 7 8 9 10 |
|
The function returns tuples containing indices of elements that are local maxima or minima:
1 2 3 4 |
|
To obtain the actual values at these local extrema:
1 |
|
Additional Functions in SciPy
In addition to argrelextrema, SciPy provides specialized functions for finding only maxima or minima:
The above is the detailed content of How to Find Local Maxima and Minima in a 1D Numpy Array Using SciPy?. For more information, please follow other related articles on the PHP Chinese website!