# Find Peaks in Python

Shivam Arora Oct 10, 2023

A peak is a value higher than most of the local values. There can be a single global max peak or multiple peaks. Peaks in the graphs should be visible and defined and should not be hidden in data noise.

In this article, we will find the peaks of different sets of values in Python.

## Use the `scipy.signal.find_peaks()` Function to Detect Peaks in Python

The `scipy.signal.find_peaks()` can detect the peaks of the given data. Few parameters are associated with this function `width`, `threshold`, `distance`, and `prominence`. It returns the indexes of the value where the peak is found.

For example,

``````from scipy.signal import find_peaks

lst = [
5,
3,
2,
19,
17,
8,
13,
5,
0,
6,
1,
-5,
-10,
-3,
6,
9,
8,
14,
8,
11,
3,
2,
22,
8,
2,
1,
]
peaks, _ = find_peaks(lst, height=0)
print(peaks)
``````

Output:

``````[ 3  6  9 15 17 19 22]
``````

## Use the `scipy.signal.argrelextrema()` Function to Detect Peaks in Python

This function is similar to the `find_peaks()` function. In addition, it contains an `order` parameter. This parameter is a distance parameter that serves as a minimization filter. We need to provide the `comparator` argument as the `np.greater` method to calculate the indexes of the peaks.

For example,

``````import numpy as np
from scipy.signal import argrelextrema

lst = [
5,
3,
2,
19,
17,
8,
13,
5,
0,
6,
1,
-5,
-10,
-3,
6,
9,
8,
14,
8,
11,
3,
2,
22,
8,
2,
1,
]
peaks = argrelextrema(np.array(lst), np.greater)
print(peaks)
``````

Output:

``````(array([ 3,  6,  9, 15, 17, 19, 22], dtype=int64),)
``````

## Use the `detecta.detect_peaks()` Function to Detect Peaks in Python

An algorithm based on material written by Marcos Duarte is implemented in the `detect_peaks()` method to find the peaks in a given set of values. In this function, the tuning and filtering support is not as complete as other features.

For example,

``````from detecta import detect_peaks

lst = [
5,
3,
2,
19,
17,
8,
13,
5,
0,
6,
1,
-5,
-10,
-3,
6,
9,
8,
14,
8,
11,
3,
2,
22,
8,
2,
1,
]
index = detect_peaks(lst)
print(index)
``````

Output:

``````[ 3  6  9 15 17 19 22]
``````