【问题标题】:How to detect peaks at the extreme ends of a function in Python?如何在 Python 中检测函数极端的峰值?
【发布时间】:2020-06-10 11:44:10
【问题描述】:

我正在尝试使用 scipy 查找函数的峰值,但是我希望它也能够检测边界处的峰值。这是一个这样的案例的快照。正如我们看到的,该方法检测到了 10 个峰,但我希望总峰为 12,包括边界处的峰。有什么办法吗?我也不想像那样包含开始和结束索引。我将在大型数据集上运行此方法,因此寻找通用解决方案。

coordinates = df.loc[:, columns].sum(axis=1)
peaks_max_y = argrelextrema(coordinates.to_numpy(), np.greater)[0]

index = np.arange(df.shape[0])
_ = plt.plot(index, coordinates, 'b-', linewidth = 2)
_ = plt.plot(index[peaks_max_y], coordinates[peaks_max_y], 'ro', label = 'minima peaks')
plt.title(pid)

【问题讨论】:

  • 如果峰总是在边界上,你可以只取数组的第一个和最后一个元素
  • 在我看来,SciPy 正确地找到了 all 10 个峰值(局部最大值)。如果您还想要端点,只需将data[0]data[-1] 添加到您的峰值列表中。
  • 你至少应该添加生成这个的代码。
  • 这能回答你的问题吗? Peak-finding algorithm for Python/SciPy
  • 看我的回答stackoverflow.com/questions/1713335/…@ashish14。

标签: python matplotlib plot scipy


【解决方案1】:

既然find_peaks 会处理其余的点,那么检查端点处的斜率怎么样?如果左侧的斜率为负,则您在开始时有一个峰值。同样,如果最后是积极的,你就有了一个高峰。

类似这样的:

if np.sign(np.diff(y))[0] < 0:
    # accept y[0] as peak
if np.sign(np.diff(y))[-1] > 0:
    # accept y[-1] as peak

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 2012-11-11
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多