【问题标题】:Finding a local Maxima/minimum using python使用python查找本地最大值/最小值
【发布时间】:2015-05-06 09:20:44
【问题描述】:

我的代码基于以下评论: Finding local maxima/minima with Numpy in a 1D numpy array

它有效,但它并没有为我重现所有的峰值。它似乎总是错过第一个高峰。有没有什么办法可以让这段代码在确定每个峰值时更加健壮?

我尝试过平滑数据、更改顺序等均无济于事。

import numpy as np
from scipy.signal import argrelextrema

profile1 = "data posted below"

profile1_filtered = ndimage.filters.gaussian_filter(profile1, 1, mode='nearest')

plot(profile1_filtered[300:740])

# for local maxima
result = argrelextrema(profile1[300:740], np.greater, order =15)

print result

for i in result:
    plot([i,i],[-16380,-16300], color='k', linestyle='--', linewidth=1)

提前感谢任何帮助/建议!

由于我在工作,我的上传/下载非常受限。通常我会发布一个指向我的数据的链接,但是我希望这样做: (等我回家后,我会用指向我的数据的链接对此进行修改)

array([-16368, -16366, -16365, -16369, -16366, -16369, -16368, -16362,
       -16368, -16366, -16367, -16364, -16367, -16367, -16367, -16366,
       -16363, -16366, -16369, -16370, -16365, -16364, -16362, -16363,
       -16365, -16363, -16364, -16362, -16361, -16362, -16364, -16364,
       -16365, -16361, -16363, -16363, -16366, -16361, -16360, -16363,
       -16362, -16362, -16357, -16360, -16359, -16362, -16359, -16359,
       -16358, -16358, -16355, -16356, -16355, -16349, -16350, -16349,
       -16343, -16338, -16332, -16329, -16329, -16339, -16339, -16348,
       -16351, -16351, -16354, -16355, -16355, -16356, -16357, -16355,
       -16355, -16356, -16356, -16358, -16354, -16355, -16356, -16357,
       -16356, -16355, -16355, -16352, -16357, -16353, -16353, -16352,
       -16357, -16351, -16347, -16347, -16345, -16342, -16332, -16321,
       -16315, -16311, -16315, -16320, -16327, -16335, -16341, -16346,
       -16347, -16349, -16350, -16350, -16347, -16351, -16354, -16354,
       -16352, -16353, -16352, -16354, -16356, -16354, -16358, -16354,
       -16353, -16356, -16351, -16352, -16351, -16351, -16352, -16347,
       -16344, -16344, -16340, -16335, -16326, -16315, -16305, -16299,
       -16303, -16309, -16322, -16331, -16335, -16342, -16342, -16346,
       -16349, -16353, -16352, -16350, -16350, -16350, -16354, -16353,
       -16353, -16355, -16355, -16356, -16353, -16352, -16352, -16356,
       -16354, -16354, -16354, -16353, -16354, -16352, -16355, -16349,
       -16350, -16347, -16346, -16342, -16338, -16334, -16338, -16335,
       -16341, -16344, -16345, -16349, -16349, -16349, -16353, -16353,
       -16353, -16353, -16354, -16353, -16351, -16351, -16352, -16354,
       -16353, -16353, -16353, -16355, -16354, -16352, -16353, -16356,
       -16354, -16353, -16353, -16353, -16352, -16349, -16350, -16347,
       -16345, -16340, -16337, -16331, -16332, -16329, -16337, -16339,
       -16342, -16344, -16345, -16348, -16351, -16351, -16350, -16352,
       -16351, -16346, -16353, -16349, -16350, -16352, -16353, -16349,
       -16352, -16351, -16354, -16352, -16354, -16352, -16354, -16352,
       -16354, -16349, -16348, -16347, -16347, -16343, -16343, -16338,
       -16333, -16330, -16331, -16336, -16340, -16341, -16345, -16346,
       -16347, -16346, -16348, -16352, -16350, -16349, -16351, -16351,
       -16354, -16353, -16353, -16355, -16351, -16352, -16353, -16354,
       -16351, -16353, -16351, -16352, -16349, -16351, -16350, -16351,
       -16352, -16348, -16352, -16349, -16345, -16346, -16341, -16337,
       -16338, -16339, -16342, -16346, -16344, -16348, -16352, -16350,
       -16352, -16353, -16350, -16353, -16356, -16355, -16356, -16354,
       -16352, -16357, -16355, -16356, -16353, -16352, -16354, -16356,
       -16355, -16358, -16352, -16352, -16353, -16351, -16353, -16350,
       -16350, -16349, -16351, -16346, -16344, -16340, -16339, -16338,
       -16339, -16343, -16344, -16345, -16348, -16351, -16350, -16351,
       -16353, -16354, -16353, -16350, -16353, -16354, -16352, -16355,
       -16353, -16356, -16353, -16354, -16356, -16352, -16353, -16355,
       -16353, -16353, -16351, -16352, -16354, -16349, -16349, -16349,
       -16348, -16343, -16340, -16334, -16323, -16322, -16326, -16328,
       -16334, -16343, -16347, -16349, -16348, -16350, -16353, -16353,
       -16354, -16354, -16357, -16356, -16353, -16354, -16358, -16356,
       -16354, -16357, -16357, -16358, -16356, -16357, -16356, -16354,
       -16355, -16356, -16354, -16355, -16353, -16352, -16351, -16351,
       -16346, -16342, -16336, -16333, -16334, -16336, -16342, -16345,
       -16347, -16354, -16354, -16357, -16358, -16359, -16359, -16362,
       -16361, -16360, -16361, -16360, -16363, -16362, -16362, -16364,
       -16363, -16363, -16363, -16363, -16364, -16363, -16364, -16366,
       -16365, -16363, -16362, -16365, -16366, -16364, -16368, -16367])

【问题讨论】:

  • 你能用一个小数据集重现你的问题并发布吗?
  • 您的预期输出是什么?你有一个print result,它打印什么?

标签: python numpy


【解决方案1】:

问题似乎源于您的原始数据。与所有其他峰值不同,第一个峰值包含两次相同的值-16329, -16329,。即使应用了高斯滤波器,这仍然是一个平台而不是一个峰值。

当您使用np.greater 作为比较器时,由于输入数据的性质,它会失败。一个快速的解决方案是使用np.greater_equal 作为比较器,但这会记录第一个“峰值”两次。另一种选择可能是检查输入数据中是否存在重复值或应用其他过滤器。

local_maxima = argrelextrema(profile, np.greater_equal, order = 15, mode = 'clip')

【讨论】:

  • 我明白你的意思,高原确实让事情变得更加困难。我将不得不为这样的重复值更正我的数据。我的数据越嘈杂,这将变得更加困难。我会玩一点,看看我能得到什么,谢谢!
  • 您还可以使用np.greater_equal 返回来检查索引argrelextrema。如果它们相距 1,则您正在查看一个用更大_equal 计算两次的高原。这可以通过np.diff()np.where() 轻松完成。
猜你喜欢
  • 1970-01-01
  • 2012-09-16
  • 2013-10-05
  • 1970-01-01
  • 2017-09-20
  • 2018-06-19
  • 2017-04-20
  • 2020-07-11
  • 2018-09-11
相关资源
最近更新 更多