Networkx中的图论 [英] Graph Theory in Networkx

查看:749
本文介绍了Networkx中的图论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import networkx as nx 

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

#find modularity
part = best_partition(G)
模块化(部分,G)

然而,我得到的传递性很好 - 有以下几点计算模块化的错误。

  NameError:name'best_partition'未定义

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

解决方案

据我所知, best_partition 不属于networkx。您好像要使用 https://sites.google.com/site/findcommunities/您可以从 https://bitbucket.org/taynaud/python-louvain/src



一旦您安装了 community ,请尝试以下代码:

 导入networkx为nx 
导入社区
导入matplotlib.pyplot为plt

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

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

#plot,使用社区结构的颜色节点
values = [part.get(node)for G.nodes()]
nx.draw_spring G,cmap = plt.get_cmap('jet'),node_color = values,node_size = 30,with_labels = False)
plt.show()

  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


I am starting to use this interface now, I have some experience with Python but nothing extensive. I am calculating the transitivity and community structure of a small graph:

import networkx as nx

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

#find modularity
part = best_partition(G)
modularity(part, G)

I get the transitivity just fine, however - there is the following error with calculating modularity.

NameError: name 'best_partition' is not defined

I just followed the documentation provided by the networkx site, is there something I am doing wrong?

解决方案

As far as I can tell best_partition isn't part of networkx. It looks like you want to use https://sites.google.com/site/findcommunities/ which you can install from https://bitbucket.org/taynaud/python-louvain/src

Once you've installed community try this code:

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()

edit: How I installed the community detection library

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

这篇关于Networkx中的图论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆