Networkx中的图论

Wor*_*rse 5 python graph networkx

我现在开始使用这个界面,我有一些Python的经验但没什么广泛的.我正在计算一个小图的传递性和社区结构:

import networkx as nx

G = nx.read_edgelist(data, delimiter='-', nodetype=str)
nx.transitivity(G)

#find modularity
part = best_partition(G)
modularity(part, G)
Run Code Online (Sandbox Code Playgroud)

我得到了传递性,但是 - 计算模块性存在以下错误.

NameError: name 'best_partition' is not defined
Run Code Online (Sandbox Code Playgroud)

我只是按照networkx网站提供的文档,有什么我做错了吗?

dra*_*nxo 9

据我所知best_partition,不是networkx的一部分.您似乎想使用https://sites.google.com/site/findcommunities/,可以从https://bitbucket.org/taynaud/python-louvain/src安装

安装完成后请community尝试以下代码:

import networkx as nx
import community
import matplotlib.pyplot as plt

G = nx.random_graphs.powerlaw_cluster_graph(300, 1, .4)
nx.transitivity(G)

#find modularity
part = community.best_partition(G)
mod = community.modularity(part,G)

#plot, color nodes using community structure
values = [part.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑:我如何安装社区检测库

ryan@palms ~/D/taynaud-python-louvain-147f09737714> pwd
/home/ryan/Downloads/taynaud-python-louvain-147f09737714
ryan@palms ~/D/taynaud-python-louvain-147f09737714> sudo python3 setup.py install
Run Code Online (Sandbox Code Playgroud)