我是一个非常非常平庸的程序员,但我仍然打算使用igraph python 库来确定用户在给定论坛中的中心性的影响,以预测他以后对该论坛的贡献。
我与使用NetworkX库做类似事情的其他人取得了联系,但鉴于论坛的当前规模,计算精确的中心性指数几乎是不可能的——这需要太多时间。
这是他的代码:
import networkx as netx
import sys, csv
if len(sys.argv) is not 2:
print 'Please specify an input graph.'
sys.exit(1)
ingraph = sys.argv[1]
graph = netx.readwrite.gpickle.read_gpickle(ingraph)
num_nodes = len(graph.nodes())
print '%s nodes found in input graph.' % num_nodes
print 'Recording data in centrality.csv'
# Calculate all of the betweenness measures
betweenness = netx.algorithms.centrality.betweenness_centrality(graph)
print 'Betweenness computations complete.'
closeness = netx.algorithms.centrality.closeness_centrality(graph)
print 'Closeness computations complete.'
outcsv = csv.writer(open('centrality.csv', 'wb'))
for node in graph.nodes():
outcsv.writerow([node, betweenness[node], closeness[node]])
print 'Complete!'
我试图用 igraph 库编写类似的东西(它允许进行快速估计而不是精确计算),但我似乎无法将数据写入 CSV 文件。
我的代码:
import igraph
import sys, csv
from igraph import *
graph = Graph.Read_Pajek("C:\karate.net")
print igraph.summary(graph)
estimate = graph.betweenness(vertices=None, directed=True, cutoff=2)
print 'Betweenness computation complete.'
outcsv = csv.writer(open('estimate.csv', 'wb'))
for v in graph.vs():
outcsv.writerow([v, estimate[vs]])
print 'Complete!'
我在 igraph 文档中找不到如何调用单个顶点(或 NetworkX 术语中的节点),所以这就是我收到错误消息的地方)。也许我也忘记了其他事情;我可能太糟糕的程序员没有注意到:P
我究竟做错了什么?