Local maxima or peaks are the values or points in a series where the value is higher than its neighboring points i.e., it is a point where the function is increasing on both sides of it. In time-series analysis, identifying peaks is a crucial step in detecting anomalies, which can also provide valuable insights for businesses.
In this thread, different methods and techniques will be discussed including built-in functions in Pandas, and SciPy, as well as loops and list comprehensions. For an efficient understanding of each method, a simple example code is also there for demonstration of the method.
1. Using a "for" loop:
- This method is probably the best method if you want to learn the logic of what local maxima mean.
- As defined in the introduction, it is any value that is increasing on both sides, hence we have defined a condition in the loop where we consider a value and check if it’s greater than both values adjacent to it.
- The code is also efficient as it handles errors like empty series or series with non-numeric datatypes before actually searching for the local maxima.
2. Using list comprehension:
- List comprehension is a short way of achieving this task using loops, the same condition and error checks are used which were used in method 1.
3. Using Pandas "shift()" function:
shift()function is used to shift/move the index of a series by a specified number of periods. In this example, we used
s.shift(-1)to shift the series by one position to the right and left, respectively.
- After shifting, we compared each element to its neighbors using boolean operations. If the element is greater than both its left and right neighbors, it’s considered a local maximum, and we add it to our list.
4. Using SciPy's "find_peaks()" function:
find_peaks()function takes a one-dimensional array or sequence as input and returns the indices of all peaks.
- By default, it identifies peaks as local maxima, but it also allows you to specify a minimum height or prominence of the peak, which can be useful when working with noisy data or when one is interested in identifying only the most significant peaks.
- The second variable returned by
find_peaks()is an empty array that is ignored, but it can be used to obtain additional information about the peaks, such as their heights or widths.
5. Using SciPy's "argrelextrema()" function:
argrelextrema()function takes an array or sequence as input and returns the indices of all local extrema, which can be local maxima or local minima. By default, it identifies both local maxima and minima.
- In this example, we pass series values to this function using the
valuesattribute to find the indices. The
argrelextrema()to identify local maxima, rather than local minima.
- The function returns a tuple of array containing the indices of the local maxima, which are used to access the values from the series.