安装
louvain当前最新版本:0.14
pip install python-louvain
由于是处理社区的数据,这里还是安装networkx
pip install networkx

使用
不妨来运行下一个案例:
import community as community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx
# load the karate club graph
G = nx.karate_club_graph()
#first compute the best partition
partition = community_louvain.best_partition(G)
# compute the best partition
partition = community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.show()

API学习
使用Louvain heuristices算法来进行社区的划分,试图获得最多的社区。
community.best_partition(graph, partition=None, weight='weight', resolution=1.0, randomize=None, random_state=None)
| 参数 | 参数说明 |
|---|---|
graph | networkx.Graph |
partition | dict, optional;将使用这些节点开始划分,最终字典键是这些节点,值是所划分的社区 |
weight | str, optional;使用图中的键作为权重,默认是weight |
resolution | double, optional; 将会改变社区的大小,默认是1,该值越大划分的社区越少,反之亦然 |
randomize | boolean, optional;随机化节点评估顺序和社区评估顺序,以在每次调用时获得不同的划分 |
random_state | int, RandomState instance or None, optional (default=None);如果int,random_state是随机数生成器使用的种子;如果RandomState实例,random_state是随机数生成器;如果没有,则随机数生成器是使用np.random的RandomState实例. |
返回:社区划分后的字典对象
当然,网站中还有其余的API接口,感觉我不会用到,就不介绍了。
——————
github地址:python-louvain
5467

被折叠的 条评论
为什么被折叠?



