使用 Igraph 库确定介数中心性
我是一个非常非常平庸的程序员,但我仍然致力于使用 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
我做错了什么?
I am a very, very mediocre programmer, but I still aim to use the igraph python library to determine the effect of a user's centrality in a given forum to predict his later contributions to that forum.
I got in touch with someone else who used the NetworkX library to do something similar, but given the current size of the forum, calculating exact centrality indices is virtually impossible--it just takes too much time.
This was his code though:
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!'
I tried to write something similar with the igraph library (which allows to make quick estimates rather than exact calculations), but I cannot seem to write data to a CSV file.
My code:
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!'
I can't find how to call individual vertices (or nodes, in NetworkX jargon) in the igraph documentation, so that's where I am getting error messages). Perhaps I am forgetting something else as well; I am probably too bad a programmer to notice :P
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,为了清楚起见,以下内容最终被证明可以解决问题:
So, for the sake of clarity, the following eventually proved to do the trick:
正如您已经注意到的,igraph 中的各个顶点是使用图形对象的
vs
属性来访问的。vs
的行为类似于列表,因此迭代它会产生图的顶点。每个顶点都由Vertex
类的一个实例表示,顶点的 index 由其index
属性给出。 (请注意,igraph 对顶点和边都使用连续的数字索引,因此这就是您需要index
属性并且不能直接使用原始顶点名称的原因)。我认为您需要的是最初存储在输入文件中的顶点的名称。名称存储在
name
或id
顶点属性中(取决于您的输入格式),因此您需要的可能是这样的:请注意,顶点属性是通过索引顶点来访问的对象就好像它是一本字典一样。另一种方法是直接使用
vs
对象作为字典;这将为您提供一个列表,其中包含所有顶点的给定顶点属性的值。例如:使用生成器表达式的更快版本:
As you already noticed, individual vertices in igraph are accessed using the
vs
attribute of your graph object.vs
behaves like a list, so iterating over it will yield the vertices of the graph. Each vertex is represented by an instance of theVertex
class, and the index of the vertex is given by itsindex
attribute. (Note that igraph uses continuous numeric indices for both the vertices and the edges, so that's why you need theindex
attribute and cannot use your original vertex names directly).I presume that what you need is the name of the vertex that was stored originally in your input file. Names are stored in the
name
orid
vertex attribute (depending on your input format), so what you need is probably this:Note that vertex attributes are accessed by indexing the vertex object as if it was a dictionary. An alternative is to use the
vs
object directly as a dictionary; this will give you a list containing the values of the given vertex attribute for all vertices. E.g.:An even faster version using generator expressions: