how do I cluster a list of geographic points by distance?

前端 未结 3 1109
天命终不由人
天命终不由人 2021-01-02 21:29

I have a list of points P=[p1,...pN] where pi=(latitudeI,longitudeI).

Using Python 3, I would like to find a smallest set of clusters (disjoint subsets of P) such th

3条回答
  •  长发绾君心
    2021-01-02 21:59

    Here is a solution that seems correct and will behave O(N^2) worst case and better depending on the data:

    def my_cluster(S,distance):
        coords=set(S)
        C=[]
        while len(coords):
            locus=coords.pop()
            cluster = [x for x in coords if vincenty(locus,x).km <= distance]
            C.append(cluster+[locus])
            for x in cluster:
                coords.remove(x)
        return C
    

    NOTE: I am not marking this as an answer because one of my requirements is that it be a smallest set of clusters. My first pass is good but I haven't proven that it is a smallest set.

    The result (on a larger set of points) can be visualized as follows:

提交回复
热议问题