首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图形和控制台输出是不同的。两个应该是相同的.Where是我的错误吗?

图形和控制台输出是不同的。两个应该是相同的.Where是我的错误吗?
EN

Stack Overflow用户
提问于 2022-04-17 06:43:16
回答 1查看 71关注 0票数 0

根据我的数据集,Karata_Club数据集共有4个社区。但在绘制图表时,该图显示了五个不同的社区,两者应该是相同的。

代码语言:javascript
运行
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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) #计算社区打印(分区)

该模块采用了路易文启发式算法进行社区检测。

由于这两种算法都使用不同的社区检测方法,因此这两种算法的结果可能不同,这就是在您的情况下发生的情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71899861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档