如果它们在Python中接近,则平均峰值索引

时间:2015-03-08 19:31:08

标签: python arrays sorting replace average

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

请注意,数组的二进制值仅用于说明,峰值可以是任何实数。

1 个答案:

答案 0 :(得分:0)

您可以使用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]