欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于networkx可视化处聚类算法的结果

程序员文章站 2022-03-10 16:20:37
...
if __name__ == '__main__':
    import networkx as nx
    import matplotlib.pyplot as plt
    rpath4 = r'E:\community_detcttion\Dataset\football.gml'
    G =nx.read_gml(rpath4,'id')
    adjacency = nx.to_numpy_array(G)
    #将数据集进行处理转换为矩阵
    for i in range(adjacency.shape[0]):
        for j in range(adjacency.shape[1]):
            if i == j and adjacency[i, j] == 1:
                adjacency[i, j] = 0
            else:
                if adjacency[i, j] == adjacency[j, i]:
                    adjacency[j, i] = 0
    #处理完矩阵转换为图
    G2 =nx.from_numpy_matrix(adjacency)
    #算法最后的聚类结果其中列表索引代表节点,索引对应的数值为label,相同label的节点在同一社区(或者说聚类)
    community =[7, 25, 13, 81, 7, 81, 13, 7, 7, 7, 81, 7, 43, 13, 43, 13, 7, 96, 43, 91, 96, 7, 7, 7, 7, 25, 43, 96, 7, 91, 91, 43, 13, 25, 43, 91, 43, 25, 43, 13, 81, 7, 43, 43, 48, 25, 88, 13, 48, 88, 7, 7, 81, 88, 43, 91, 96, 48, 88, 113, 13, 43, 96, 48, 13, 96, 48, 88, 7, 7, 96, 43, 81, 88, 81, 48, 96, 7, 7, 91, 91, 81, 91, 88, 81, 43, 48, 96, 88, 25, 7, 48, 48, 7, 91, 96, 96, 48, 81, 43, 13, 91, 81, 25, 7, 25, 13, 81, 7, 25, 88, 7, 48, 96, 88]
    edge_list =G2.edges()
    pos =nx.spring_layout(G2)
    colors = community
    #先画点根据community来进行颜色的区分
    nx.draw_networkx_nodes(G2,pos,nodelist=G2.nodes(),node_color=colors)
    #再画边
    nx.draw_networkx_edges(G2,pos,nodelist=G2.edges())
    #最后显示点的节点号
    nx.draw_networkx_labels(G2,pos)
    plt.show()