[英]Python: Grouping numbers that are within n SD of central value for the group
我有一個看起來像這樣的多個浮動列表
mylist = [10, 10.2, 10.5, 11, 15, 15.3, 15.4, 16, 27, 27.4, 28, 28.1, 28.2]
我想對彼此接近的值進行分組。 例如。 我想將 10 到 11 的值分組為 4 個值的平均值。 我很難確定中心值,然后選擇屬於該組的左右值。 我怎么能這樣做?
使用defaultdict
怎么樣:
In [1]: from collections import defaultdict
In [2]: group = defaultdict(list)
In [3]: mylist = [10, 10.2, 10.5, 11, 15, 15.3, 15.4, 16, 27, 27.4, 28, 28.1, 28
...: .2]
In [4]: for val in mylist:
...: group[int(val)].append(val)
...:
In [5]: group
Out[5]:
defaultdict(list,
{10: [10, 10.2, 10.5],
11: [11],
15: [15, 15.3, 15.4],
16: [16],
27: [27, 27.4],
28: [28, 28.1, 28.2]})
它不需要排序輸入。 此外,它保留了相關值的順序
假設,我正確理解您的要求。
我聽起來你想要一個通用的方法,可能是這樣的:
from scipy.stats import binned_statistic
data = [10, 10.2, 10.5, 11, 15, 15.3, 15.4, 16, 27, 27.4, 28, 28.1, 28.2]
stats, edges, binarray = binned_statistic(data,data,bins=4)
edges # Is the boundary values that split the data evenly into 4 bins.
binarray # Shows which numbers in your original array belong to which equal sized bin.
# Note that nothing belongs to bin-3 because the gap is too wide.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.