微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中使用 Louvain 返回集群列表

如何解决如何在 Python 中使用 Louvain 返回集群列表

我想以这种格式使用 Louvain 算法创建一个包含每个集群中所有节点的数组:

clusters = [[16,17,18,19,20,24,27,31,33,35,46,47,48,49,54,58,63,64,65,66,68,69,70,82,84,85,86,87,90,91,93,98,103,104,106,107,115,116,117,119,121,122,123,124,125,129,132,137,141,148,150,151,152,154,160,162,164,165,168,171,183,185,190,193,204,211,215,216,218,227,231,233,235,238,239],[26,40,74,79,158,177,195,203,225,230],[179,1,3,9,13,14,15,21,22,25,29,34,36,41,45,52,56,59,60,61,62,73,78,88,95,100,101,102,109,112,120,133,135,138,145,146,149,155,156,163,170,172,173,175,181,184,186,191,192,194,198,202,205,207,208,210,212,223,226,229,234,237,240,241,220,178,232],[4,5,6,8,28,37,39,71,72,92,94,127,147,157,169,180,182,187,209,219,228],[2,7,10,11,12,23,30,32,38,42,43,44,50,51,53,55,57,67,75,76,77,80,81,83,89,96,97,99,105,108,110,111,113,114,118,126,128,130,131,134,136,139,140,142,143,144,153,159,161,166,167,174,176,188,189,196,197,199,200,201,206,213,214,217,221,222,224,236]]
 

为此,我在 python 中创建了以下代码

    import community as community_louvain
    import networkx as nx
    import networkx.algorithms.community as nx_comm
    import copy
    
    G = nx.read_edgelist(r"C:\Users\secret\Desktop\graph-edges.txt",create_using=nx.Graph(),nodetype=int)
    
    
    def compute_clustering(G,required_number_of_clusters,min_size_of_clusters,min_modularity,weight=None):
        i = 0.1
        while i <= 1:
            partition = community_louvain.best_partition(G,random_state=1500,resolution=i)
            if (max(partition.values())+1) == required_number_of_clusters:
                b = sorted(partition.items())
                c = dict(b)
                labels = []
                for node in c.keys():
                    clusterz = c[node]
                    labels.append(clusterz)
                num_of_nodes = len(labels)
                num_of_clusters = len(set(labels))
                clusters = []
                for i in range(num_of_clusters):
                    clusters.append(list())
                for i in range(num_of_nodes):
                    cluster_id = labels[i]
                    clusters[cluster_id].append(i)
                lc = len(clusters)
                temp = []
                cluster = copy.deepcopy(clusters)
                for i in range(lc):
                    for j in range(len(clusters[i])):
                        if len(cluster[i]) > min_size_of_clusters:
                            m = nx_comm.modularity(G,cluster)
                            temp.append(cluster[i][0])
                            cluster[i].remove(cluster[i][0])
                            count = 0
                            for w in range(len(clusters)):
                                if w != i:
                                    cluster[w].append(temp[0])
                                    if nx_comm.modularity(G,cluster) > m:
                                        temp.remove(temp[0])
                                        break
                                    else:
                                        cluster[w].remove(temp[0])
                                        count = count + 1
                                        if count == len(clusters) - 1:
                                            cluster[i].append(temp[0])
                                            temp.remove(temp[0])
                                            break
                mod = nx_comm.modularity(G,cluster)
                if mod > min_modularity:
                    print(mod)
                    return cluster
            else:
                i = i+0.1
    
    
    compute_clustering(G,0.13,weight='weight')

graph-edges.txt 是一个加权图,太大而无法在此处发布,但这里是其中的一部分:

0 45 {'weight': 0.3488372093023256}
0 33 {'weight': 0.11627906976744186}
0 71 {'weight': 0.046511627906976744}
0 78 {'weight': 0.37209302325581395}
0 150 {'weight': 0.09302325581395349}
...
(about 29.000 lines of data)

问题是这段代码没有返回我想要的集群列表,所以我希望有一种不同的方式来生成它。

提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。