根据我的数据集,Karata_Club数据集共有4个社区。但在绘制图表时,该图显示了五个不同的社区,两者应该是相同的。
import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nxcom
import community
G=nx.read_adjlist('D:\Research Folder\Datasets\Karata club\karate.txt') \\You can take any Karate club dataset to check.
# find the value of K-core
G2 = nx.k_core(G,k=4)
nx.draw(G2 , with_labels=True)
print(G2)
print(G2.nodes())
print(G2.edges())
# Find the communities
communities = sorted(nxcom.greedy_modularity_communities(G), key=len, reverse=True)
# Count the communities
print(f"The Total {len(communities)} communities.")
print(communities)
for node in G:
partition = community.best_partition(G) # compute communities
print(partition)
pos = nx.spring_layout(G) # compute graph layout
plt.figure(figsize=(9, 9)) # image is 9 x 9 inches
plt.axis('off')
nx.draw_networkx_nodes(G, pos, node_size=600, cmap=plt.cm.RdYlBu, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.3)
nx.draw_networkx_labels(G, pos)
plt.show(G)

发布于 2022-04-18 08:36:11
在这里,您使用networkx计算社区数量。
sorted(nxcom.greedy_modularity_communities(G),key=len,reverse=True)
它使用algo在图中找到社区。
在计算partition (也用于打印图形)时,您使用的是"community“模块
G中节点的
:分区= community.best_partition(G) #计算社区打印(分区)
该模块采用了路易文启发式算法进行社区检测。
由于这两种算法都使用不同的社区检测方法,因此这两种算法的结果可能不同,这就是在您的情况下发生的情况。
https://stackoverflow.com/questions/71899861
复制相似问题