sort list of floating-point numbers in groups

前端 未结 2 1828
借酒劲吻你
借酒劲吻你 2021-02-15 11:12

I have an array of floating-point numbers, which is unordered. I know that the values always fall around a few points, which are not known. For illustration, this list



        
2条回答
  •  执念已碎
    2021-02-15 11:29

    Check python-cluster. With this library you could do something like this :

    from cluster import *
    
    data = [10.01,5.001,4.89,5.1,9.9,10.1,5.05,4.99]
    cl = HierarchicalClustering(data, lambda x,y: abs(x-y))
    print [mean(cluster) for cluster in cl.getlevel(1.0)]
    

    And you would get:

    [5.0062, 10.003333333333332]
    

    (This is a very silly example, because I don't really know what you want to do, and because this is the first time I've used this library)

提交回复
热议问题