计算网络属性:如度分布,密度,与python的直径

时间:2018-05-24 06:50:46

标签: networkx

我有一个数据集csv,其中包含2列user_a和user_b(总计1900655行),每行都是友谊连接。

对于直接朋友的每一对用户,连接边缘是无向的

列user_a包含用户的ID。列user_b包含user_a的朋友的id。

user_a,user_b
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
0,10
0,11
0,12
0,13
....
196584,196537
196585,196539
196586,196539
196587,196540
196588,196540
196589,196547
196590,196561

为了找到网络属性:例如度数分布,密度,直径我使用networkx时运行以下代码我只需要使用python。

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

#%matplotlib.inline


with open("user_social.csv","r") as inf:
    next(inf, '')   # skip a line
    G = nx.read_edgelist(inf, delimiter=',', nodetype=int, encoding="utf-8")


#print nx.info(G)
print("Number of nodes in the graph")
print(len(G.nodes()))
print("Number of edges in the graph")
print(len(G.edges()))
print nx.diameter(G)

spring_pos = nx.spring_layout(G)

#plt.axis("off")
#nx.draw_networkx(G, pos = spring_pos, with_labels = False, node_size = 35)

plt.figure(figsize=(8,8))
plt.axis('off')
nx.draw_networkx_nodes(G, pos, node_size=600,node_color="blue",alpha=0.3)
nx.draw_networkx_edges(G, pos, alpha=0.3)
nx.draw_networkx_labels(G, pos)
plt.show(G)

它只显示以下结果

Number of nodes in the graph
196591
Number of edges in the graph
950327

并且其保持运行数小时,没有任何其他图形或结果或任何错误。我是python的新手,也许我错过了我的代码,所以如何更改我的代码来计算python的度分布,密度,直径?

1 个答案:

答案 0 :(得分:0)

我建议您使用Gephi https://gephi.org/之类的工具来获取网络统计信息。或者,您可以使用NEO4J https://neo4j.com之类的网络数据库系统,然后运行查询以了解您的数据。使用诸如NEO4J之类的数据库系统的优点是,您可以在一个地方保存和管理所有信息。

相关问题