【发布时间】: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