如果峰值在 Python 内接近,则平均峰值索引

Averaging indexes of peaks if they are close in Python

这可能是一个简单的问题,但我还没有想出解决办法。 假设我有一个数组 np.array([0,1,0,1,0,0,0,1,0,1,0,0,1]),在索引 [1,3,7,9,12] 处有峰值。如果在本例中将峰之间的阈值距离设置为大于 2,如何将索引替换为 [2,8,12],即距离接近的平均索引?

请注意,数组的二进制值只是为了说明,峰值可以是任何实数。

您可以使用 Raymond Hettinger's cluster function:

from __future__ import division

def cluster(data, maxgap):
    """Arrange data into groups where successive elements
       differ by no more than *maxgap*

        >>> cluster([1, 6, 9, 100, 102, 105, 109, 134, 139], maxgap=10)
        [[1, 6, 9], [100, 102, 105, 109], [134, 139]]

        >>> cluster([1, 6, 9, 99, 100, 102, 105, 134, 139, 141], maxgap=10)
        [[1, 6, 9], [99, 100, 102, 105], [134, 139, 141]]
    """
    data.sort()
    groups = [[data[0]]]
    for item in data[1:]:
        val = abs(item - groups[-1][-1])
        if val <= maxgap:
            groups[-1].append(item)
        else:
            groups.append([item])
    return groups

peaks = [1,3,7,9,12]
print([sum(arr)/len(arr) for arr in cluster(peaks, maxgap=2)])

产量

[2.0, 8.0, 12.0]