【问题标题】:Finding local maxima using find_peaks使用 find_peaks 查找局部最大值
【发布时间】:2022-01-12 00:19:53
【问题描述】:

我正在使用scipy.signal.find_peaks 来尝试找到波动很大的数据的最大值。使用以下数据框:

import pandas as pd
import numpy as np
from scipy.signal import find_peaks
Data = [95,95,95,95,95,95,95,95,94,94,94,94,94,94,94,94,229,444,457,387,280,188,236,181,183,183,185,186,189,190,190,190,179,165,151,151,161,214,213,213,214,213,212,195,179,160,158,155,114,98,164,346,229,39,134,149,194,1,153,171,187,185,104,102,100,90,90,92,92,92,93,93,93,93,93,93,94,94,94,94,94,11,1,11,11,70,182,104,58,60,134,115,99,97,99,98,98,97,97,97,97,97,97,97,97,97,96,96,96,96,96,96,96,96,96,96,96,96,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,93,93,152,206,221,286,326,341,360,377,391,392,393,393,393,394,406,418,420,422,422,408,389,345,329,276,224,166,113,-6,91,91,91,442,324,387,389,387,443,393,393,393,393,391,381,379,377,303,174,131,0,115,112,112,111,111,109,107,106,104,104,103,102,101,101,101,101,100,100,1,1,12,13,65,138,87]
df2 = pd.DataFrame(Data)


#convert to 1D array
number_column = df.loc[:,'Data']
numbers = number_column.values

#finding peaks for 1D array
peaks = find_peaks(numbers, height = 300, threshold = 1, distance = 5)
height = peaks[1]['peak_heights'] #list of heights of peaks
peak_pos = numbers[peaks[0]]
print(peaks)

#plot the peaks
fig = plt.figure()
ax = fig.subplots()
ax.plot(numbers)
ax.scatter(peak_pos, height,color = 'r', s = 25, label = 'Maxima')
ax.legend

我得到了 457、346、442、443 的局部极值。但是,在这个系统中,我需要获得以下值作为极值:(457、346、422、443)

当绘制我的极值时,我得到了这个:

所以我的问题是有人知道如何获得我需要的正确极值吗?我只是缺少那个 422 值,并且一直在玩设置但没有成功。

【问题讨论】:

    标签: python pandas scipy maxima


    【解决方案1】:

    您应该将 peak_pos = numbers[peaks[0]] 行更改为 peak_pos = peaks[0],因为 peaks[0] 为您提供峰的索引,即您要传递给 ax.scatter 的实际 x 坐标。

    要获得 422 处的峰值,我们可以将阈值设置为 None(这样您就不会被与邻居的垂直距离限制),并将距离设置得更大,例如 10。

    然后您可以将高度添加为文本注释:

    import pandas as pd
    import numpy as np
    from scipy.signal import find_peaks
    import matplotlib.pyplot as plt
    
    Data = [95,95,95,95,95,95,95,95,94,94,94,94,94,94,94,94,229,444,457,387,280,188,236,181,183,183,185,186,189,190,190,190,179,165,151,151,161,214,213,213,214,213,212,195,179,160,158,155,114,98,164,346,229,39,134,149,194,1,153,171,187,185,104,102,100,90,90,92,92,92,93,93,93,93,93,93,94,94,94,94,94,11,1,11,11,70,182,104,58,60,134,115,99,97,99,98,98,97,97,97,97,97,97,97,97,97,96,96,96,96,96,96,96,96,96,96,96,96,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,93,93,152,206,221,286,326,341,360,377,391,392,393,393,393,394,406,418,420,422,422,408,389,345,329,276,224,166,113,-6,91,91,91,442,324,387,389,387,443,393,393,393,393,391,381,379,377,303,174,131,0,115,112,112,111,111,109,107,106,104,104,103,102,101,101,101,101,100,100,1,1,12,13,65,138,87]
    df = pd.DataFrame({'Data':Data})
    
    # convert to 1D array
    number_column = df.loc[:,'Data']
    numbers = number_column.values
    
    #finding peaks for 1D array
    # peaks = find_peaks(numbers, height = 300, threshold = 1, distance = 5)
    
    peaks = find_peaks(numbers, height = 300, threshold = None, distance=10)
    height = peaks[1]['peak_heights'] #list of heights of peaks
    peak_pos = peaks[0]
    print(peaks)
    
    # plot the peaks
    fig = plt.figure()
    ax = fig.subplots()
    ax.plot(numbers)
    ax.scatter(peak_pos, height,color = 'r', s = 25, label = 'Maxima')
    ax.legend
    
    ## add numbers as text annotations
    for i, text in enumerate(height):
        if text.is_integer():
            ax.annotate(int(text), (peak_pos[i], height[i]), size=10)
        else:
            ax.annotate(text, (peak_pos[i], height[i]), size=10)
    plt.show()
    

    【讨论】:

    • 感谢您的帮助,这解决了我的位置问题,以及添加文本的出色输入!我想知道你是否知道如何解决另一个问题,我一直在玩参数,但不应该出现 442 峰值,而应该是 422,这是它之前的“驼峰”峰值的最大值
    • 哦,我错过了。我将检查文档,看看是否有任何参数可以提供帮助:docs.scipy.org/doc/scipy/reference/generated/…
    • 谢谢您,我在获取您的标签结果后编辑了问题,使其更加准确。我一直在搞乱该表中的不同设置,但没有成功
    • @SeanK22 您可以设置threshold=Nonedistance=10,您将获得所需的输出。我已经更新了我的答案
    • 谢谢德里克!我已将您的解决方案标记为正确,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多