查找彼此标准偏差在 1% 以内的值的有效方法

Efficent way of finding values that are within 1% of standard deviation of eachother

我有一个值列表,我想删除与另一个值的标准偏差百分之一以内的所有值。

我目前有两个嵌套的 for 循环。我想知道是否有更有效的方法,也许使用 numpy 和矢量化? 我当前的低效代码。

# 1% of the std
std_range = np.std(values) / 100
# if value is in 1% of std, set to None
for idx, val in enumerate(values):
    for idx2, val2 in enumerate(values):
        if val is None or val2 is None:
            continue
        elif (val2 - std_range <= val <= val2 + std_range) and idx != idx2:
            values[idx] = None
# delete None values
values = list(filter(None, ml_results))

正如你提到的,这里的顺序不相关是一种方法。

    std_range = np.std(values) / 100
    sorted_values = sorted(values)
    for id, val in enumerate(sorted_values[:-1]):
        next_value = sorted_values[id + 1]

        if val + std_range >= next_value:  # value is within range
            sorted_values[id] = None

    print(list(filter(None, sorted_values)))