我使用 Python 库 NetworkX 创建了一个图表,现在想要实现模块性算法来对我的图表中的节点进行聚类。我找到了下面这段代码:
import community
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G = nx.read_weighted_edgelist('graphs/fashionGraph_1.edgelist')
nx.transitivity(G)
# Find modularity
part = community.best_partition(G)
mod = community.modularity(part,G)
# Plot, color nodes using community structure
values = [part.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap=plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
我的图形有4267和3692条边。结果图如下: