Graph Theory in Networkx

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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?

回答1:

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 


回答2:

I just met the same error NameError: name 'best_partition' is not defined when using this example code.

This error occurs because I named my python file as networkx.py, then when we execute this program

import networkx as nx 

This program may import the networkx we defined instead of the library. In the program, best_partition is not defined. So this error occur.

Having same name with library is not appropriate. Maybe you should check this!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!