我正在尝试使用Infomap算法,使用python来检测英国铁路网中的社区,我已经成功地在不考虑权重的情况下使用了它(权重是整个链接中的旅行者数量),我想考虑权重来做这件事
我试过了,但我不确定参数是否正确
这是未加权版本的代码:
infomap_wrapper = infomap.Infomap("--two-level --directed --teleportation-probability 0.07 --markov-time 6")
infomap_wrapper
# Build infomap graph
__nodes = communities_graph.nodes()
__nodenametointeger = dict(zip(__nodes, range(0, len(__nodes))))
# Convert named stations to integers
for u, v in communities_graph.edges():
u_, v_ = __nodenametointeger[u], __nodenametointeger[v]
infomap_wrapper.addLink(u_, v_)
# Run the infomap algorithm
infomap_wrapper.run()
# Extract the communities
tree = infomap_wrapper.iterTree()
# print("Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength()))
infomap_communities = {}
for node in tree:
# print(node.id(), node.moduleIndex())
infomap_communities[node.id()] = node.moduleIndex()
infomap_communities_fixed = [set() for _ in range(max(infomap_communities.values()) + 1)]
__integertonodename = { v:k for k, v in __nodenametointeger.items() }
for node, comm in infomap_communities.items():
infomap_communities_fixed[comm] = infomap_communities_fixed[comm].union({__integertonodename[node]})
这是加权版本:
# https://www.mapequation.org/code.html#Tuning
infomap_wrapper = infomap.Infomap("--two-level --directed --teleportation-probability 0.095 --markov-time 25")
# Build infomap graph
__nodes = communities_graph.nodes()
__nodenametointeger = dict(zip(__nodes, range(0, len(__nodes))))
# Convert named stations to integers
# Use weight parameter
for u, v, data in communities_graph.edges(data=True):
u_, v_ = __nodenametointeger[u], __nodenametointeger[v]
infomap_wrapper.addLink(u_, v_, weight=float(data['weight']))
# Run the infomap algorithm
infomap_wrapper.run()
# Extract the communities
tree = infomap_wrapper.iterTree()
infomap_communities = {}
for node in tree:
infomap_communities[node.id()] = node.moduleIndex()
# Improve the extracted communities
infomap_communities_fixed = [set() for _ in range(max(infomap_communities.values()) + 1)]
__integertonodename = { v:k for k, v in __nodenametointeger.items() }
for node, comm in infomap_communities.items():
infomap_communities_fixed[comm] = infomap_communities_fixed[comm].union({__integertonodename[node]})
infomap_labels = make_labels(communities_graph, infomap_communities_fixed)
没有错误,似乎是工作,但我要确保我的参数是正确的,我怎么能呢
目前没有回答
相关问题 更多 >
编程相关推荐