如何解决这是在networkx中选择适当数量的社区的正确方法吗?
我有一个图 g
,我想使用 fluid communities algorithm 和 Girvan-Newman method 来检测 g
中的社区。我预定的社区数量是 [2,3,4,5]
之一。为了选择最佳社区数量 best_k
,我使用 quality coverage。
因为 Girvan-Newman 方法返回 best_k = 2
,其中一个社区仅包含 3
个节点。我怀疑我做错了什么。能否请您检查我是否正确实施了这些方法?
import urllib3
import io
import networkx as nx
from networkx.algorithms import community
import matplotlib.pyplot as plt
## Import dataset
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/WebMining/main/lesmis.gml'
f = http.request('GET',url)
data = io.BytesIO(f.data)
g = nx.read_gml(data)
## Define a function to add community attributes
def add_att(g,att,att_name):
att = list(att)
for i in g.nodes:
for j in range(len(att)):
if i in list(att[j]):
nx.set_node_attributes(g,{i: j},name = att_name)
break
list_k = [2,5]
best_k = 2
qua_cov = 0
## Fluid communities algorithm
for k in list_k:
fluid = community.asyn_fluid.asyn_fluidc(g,k) # fluid is the partition of nodes
fluid = list(fluid)
cur_qua_cov = community.quality.coverage(g,fluid) # compute quality coverage
if cur_qua_cov >= qua_cov:
qua_cov = cur_qua_cov
best_k = k
commu_fluid = community.asyn_fluid.asyn_fluidc(g,best_k)
## Girvan-Newman method
commu_gn = community.centrality.girvan_newman(g)
commu_gn = list(commu_gn) # commu_gn is the list of partitions of nodes
best_k = 2
qua_cov = 0
for k in list_k:
cur_qua_cov = community.quality.coverage(g,commu_gn[k - 2])
if cur_qua_cov >= qua_cov:
qua_cov = cur_qua_cov
best_k = k
commu_gn = commu_gn[best_k - 2]
## Add attributes
att = [commu_fluid,commu_gn]
att_name = ['commu_fluid','commu_gn']
for i in range(len(att)):
add_att(g,att[i],att_name[i])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。