Skip to main content
 首页 » 编程设计

python之如何在不指定聚类数的情况下对列表中的项目进行聚类

2024年11月01日13shihaiming

我的目标是根据任意两个连续项目之间的距离对下面列表中的项目进行聚类。未指定簇数,仅指定最大距离 在任何两个连续的项目之间,不得超过它们才能在同一集群中。

我的尝试

import itertools 
max_dist=20 
_list=[1,48,52,59,89,94,103,147,151,165] 
Ideal_result= [[1],[48,52,59],[89,94,103],[147,151,165]] 
 
def clust(list_x, max_dist): 
    q=[] 
    for a, b in itertools.combinations(list_x,2): 
        if b-a<=20: 
            q.append(a),(b) 
        else:continue 
        yield q 
print list(clust(_list,max_dist)) 

输出:

[[48,48,52,89,89,94,147,147,151],[48,48,52,89,89,94,147,147,151],..]` 

输出完全错误,但我只是想包括我的尝试。

关于如何获得理想结果有什么建议吗?谢谢。

请您参考如下方法:

这通过了你的测试:

def cluster(items, key_func): 
    items = sorted(items) 
    clusters = [[items[0]]] 
    for item in items[1:]: 
        cluster = clusters[-1] 
        last_item = cluster[-1] 
        if key_func(item, last_item): 
            cluster.append(item) 
        else: 
            clusters.append([item]) 
    return clusters 

如果当前项和先前项应属于同一集群,则 key_func 返回 True:

>>> cluster([1,48,52,59,89,94,103,147,151,165], lambda curr, prev: curr-prev < 20) 
[[1], [48, 52, 59], [89, 94, 103], [147, 151, 165]] 

另一种可能性是修改 "equivalent code" for itertools.groupby()同样为键函数采用多个参数。