检测数字列表中的峰值并记录它们的位置

我正在尝试创建一些代码来返回数值数组的“峰值”(或局部最大值)的位置和值。


例如,列表arr = [0, 1, 2, 5, 1, 0]在 position 处有一个峰值,3值为5(since arr[3]equals 5)。


数组的第一个和最后一个元素不会被视为峰值(在数学函数的上下文中,您不知道之后和之前是什么,因此,您不知道它是否是峰值)。


def pick_peaks(arr):

    print(arr)

    posPeaks = {

        "pos": [],

        "peaks": [],

    }

    startFound = False

    n = 0

    while startFound == False:

        if arr[n] == arr[n+1]:

            n += 1

        else:

            startFound = True


    endFound = False

    m = len(arr) - 1

    while endFound == False:

        if arr[m] == arr[m-1]:

            m -= 1

        else:

            endFound = True


    for i in range(n+1, m):

        if arr[i] == arr[i-1]:

            None

        elif arr[i] >= arr[i-1] and arr[i] >= arr[i+1]:

            posPeaks["pos"].append(i)

            posPeaks["peaks"].append(arr[i])


    return posPeaks

我的问题是高原。[1, 2, 2, 2, 1]有峰值而[1, 2, 2, 2, 3]没有。当平台为峰值时,记录平台的第一个位置。


任何帮助表示赞赏。


守着一只汪
浏览 214回答 3
3回答

catspeake

此代码采用窗口编号并给出该窗口大小内的峰值l=[1,2,3,4,5,4,3,2,1,2,3,4,3,2,4,2,1,2]n=int(input("The size of window on either side "))for i in range(n,len(l)-n):&nbsp; &nbsp; if max(l[i-n:i]+l[i+1:i+n+1])<l[i]:&nbsp; &nbsp; &nbsp; &nbsp; print(l[i],' at index = ',i)

慕斯709654

我知道我参加聚会可能有点晚了,但我想使用 NumPy 数组分享我的解决方案:def get_level_peaks(v):&nbsp; &nbsp; peaks = []&nbsp; &nbsp; i = 1&nbsp; &nbsp; while i < v.size-1:&nbsp; &nbsp; &nbsp; &nbsp; pos_left = i&nbsp; &nbsp; &nbsp; &nbsp; pos_right = i&nbsp; &nbsp; &nbsp; &nbsp; while v[pos_left] == v[i] and pos_left > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos_left -= 1&nbsp; &nbsp; &nbsp; &nbsp; while v[pos_right] == v[i] and pos_right < v.size-1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos_right += 1&nbsp; &nbsp; &nbsp; &nbsp; is_lower_peak = v[pos_left] > v[i] and v[i] < v[pos_right]&nbsp; &nbsp; &nbsp; &nbsp; is_upper_peak = v[pos_left] < v[i] and v[i] > v[pos_right]&nbsp; &nbsp; &nbsp; &nbsp; if is_upper_peak or is_lower_peak:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peaks.append(i)&nbsp; &nbsp; &nbsp; &nbsp; i = pos_right&nbsp; &nbsp; peaks = np.array(peaks)&nbsp; &nbsp; """&nbsp; &nbsp; # uncomment this part of the code&nbsp; &nbsp; # to include first and last positions&nbsp; &nbsp; first_pos, last_pos = 0, v.size-1&nbsp; &nbsp; peaks = np.append([first_pos], peaks)&nbsp; &nbsp; peaks = np.append(peaks, [last_pos])&nbsp; &nbsp; """&nbsp; &nbsp; return peaks示例 1(见图表):v = np.array([7, 2, 0, 4, 4, 6, 6, 9, 5, 5])p = get_peaks(v)print(v)&nbsp; &nbsp; &nbsp; &nbsp; # [7 2 0 4 4 6 6 9 5 5]print(p)&nbsp; &nbsp; &nbsp; &nbsp; # [0 2 7 9] (peak indexes)print(v[p])&nbsp; &nbsp; &nbsp;# [7 0 9 5] (peak elements)示例 2(见图表):v = np.array([8, 2, 1, 0, 1, 2, 2, 5, 9, 3])p = get_peaks(v)print(v)&nbsp; &nbsp; &nbsp; &nbsp; # [8 2 1 0 1 2 2 5 9 3]print(p)&nbsp; &nbsp; &nbsp; &nbsp; # [0 3 8 9] (peak indexes)print(v[p])&nbsp; &nbsp; &nbsp;# [8 0 9 3] (peak elements)示例 3(见图表):v = np.array([9, 8, 8, 8, 0, 8, 9, 9, 9, 6])p = get_peaks(v)print(v)&nbsp; &nbsp; &nbsp; &nbsp; # [9 8 8 8 0 8 9 9 9 6]print(p)&nbsp; &nbsp; &nbsp; &nbsp; # [0 4 6 9] (peak indexes)print(v[p])&nbsp; &nbsp; &nbsp;# [9 0 9 6] (peak elements)在示例 3 中,我们有一个从索引 6 到索引 8 的平坦上峰。在这种情况下,索引将始终指示平台的最左侧位置。如果要指示中间位置或最右边位置,只需更改这部分代码:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; if is_upper_peak or is_lower_peak:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peaks.append(i)&nbsp; &nbsp; &nbsp; &nbsp; ...对此:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; # middle position&nbsp; &nbsp; &nbsp; &nbsp; if is_upper_peak or is_lower_peak:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peaks.append((pos_left + pos_right) // 2)&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; # rightmost position&nbsp; &nbsp; &nbsp; &nbsp; if is_upper_peak or is_lower_peak:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peaks.append(pos_right)&nbsp; &nbsp; &nbsp; &nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python