首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何运行露易万社区检测算法?

我如何运行露易万社区检测算法?
EN

Stack Overflow用户
提问于 2014-09-27 00:13:30
回答 2查看 10.3K关注 0票数 6

请有人向我提供一个简单的例子,说明如何使用python接口在someone中运行louvain社区检测算法。有文件吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-27 02:23:16

它叫multilevel.community

根据https://bugs.launchpad.net/igraph/+bug/925038 ..。这个功能确实存在--它被称为igraph_community_multilevel

如果您在github存储库中查找in

https://github.com/igraph/igraph/blob/master/src/community.c

igraph_community_multilevel确实存在,它是用C编写的。我不是100%肯定的,这是你想要的算法,但它可能是。

这是个好消息!谢谢!这个功能是否被导出到R中?为什么函数有一个通用名称(igraph_community_multilevel)而不是作者指定的名称("louvain方法“)?使用"louvain“这个名称将使用户更容易找到该函数!

票数 7
EN

Stack Overflow用户

发布于 2020-03-30 12:53:34

下面是如何使用Q中的三个不同模块(igraphnetworkxbct)中的louvain算法来估计模块化的igraph

代码语言:javascript
复制
import numpy as np
import networkx as nx
np.random.seed(9)

# I will generate a stochastic block model using `networkx` and then extract the weighted adjacency matrix.
sizes = [50, 50, 50] # 3 communities
probs = [[0.25, 0.05, 0.02],
         [0.05, 0.35, 0.07],
         [0.02, 0.07, 0.40]]

# Build the model
Gblock = nx.stochastic_block_model(sizes, probs, seed=0)

# Extract the weighted adjacency
W = np.array(nx.to_numpy_matrix(Gblock, weight='weight'))
W[W==1] = 1.5
print(W.shape)
# (150, 150)

#* Modularity estimation using Louvain algorithm 
# 1. `igraph` package

from igraph import *
graph = Graph.Weighted_Adjacency(W.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)
louvain_partition = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)
modularity1 = graph.modularity(louvain_partition, weights=graph.es['weight'])
print("The modularity Q based on igraph is {}".format(modularity1))

# 2. `networkx`package using `python-louvain`
# https://python-louvain.readthedocs.io/en/latest/

import networkx as nx
import community
G = nx.from_numpy_array(W)
louvain_partition = community.best_partition(G, weight='weight')
modularity2 = community.modularity(louvain_partition, G, weight='weight')
print("The modularity Q based on networkx is {}".format(modularity2))

# 3. `bct` module
# https://github.com/aestrivex/bctpy

import bct
com, q = bct.community_louvain(W)
print("The modularity Q based on bct is {}".format(q))

这些指纹:

代码语言:javascript
复制
The modularity Q based on igraph is 0.4257613861340037
The modularity Q based on networkx is 0.4257613861340036
The modularity Q based on bct is 0.42576138613400366
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26070021

复制
相关文章

相似问题

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