Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NetworkX-将多个度量值的结果写入CSV_Python_Csv_Networkx - Fatal编程技术网

Python NetworkX-将多个度量值的结果写入CSV

Python NetworkX-将多个度量值的结果写入CSV,python,csv,networkx,Python,Csv,Networkx,我正在使用NetworkX为一个大型网络计算4个独立的中心度指标,现在我想将结果写入一个CSV文件。我不想为4个指标中的每一个都编写一个CSV文件,而是希望有如下内容: Id, degree, between, close, eigen 1, 0.4, 0.0, 0.5, 0.45 2, 0.4, 0.0, 0.5, 0.45 3, 0.6, 0.6, 0.71, 0.58 4, 0.6, 0.7, 0.71, 0.47 5, 0.2, 0.0, 0.45, 0.18 6, 0.2,

我正在使用NetworkX为一个大型网络计算4个独立的中心度指标,现在我想将结果写入一个CSV文件。我不想为4个指标中的每一个都编写一个CSV文件,而是希望有如下内容:

Id, degree, between, close, eigen

1, 0.4, 0.0, 0.5, 0.45

2, 0.4, 0.0, 0.5, 0.45

3, 0.6, 0.6, 0.71, 0.58

4, 0.6, 0.7, 0.71, 0.47

5, 0.2, 0.0, 0.45, 0.18

6, 0.2, 0.0, 0.45, 0.18
下面是我的代码,显示我迄今为止所做的工作:

import networkx as nx
G = nx.Graph()

# add nodes and edges
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6)])

# calculate centrality metrics    
degree = nx.degree_centrality(G)
between = nx.betweenness_centrality(G)
close = nx.closeness_centrality(G)
eigen = nx.eigenvector_centrality(G)

您可以并行地迭代字典

首先,将每个字典转换为一个元组列表,按键排序:

degree = sorted(degree.items())
between = sorted(between.items())
close = sorted(close.items())
eigen = sorted(eigen.items())
接下来,创建一个并行迭代器:

p = zip(degree, between, close, eigen)
现在,
p
是:

[((1, 0.4), (1, 0.0), (1, 0.5), (1, 0.45698629803118)),
 ((2, 0.4), (2, 0.0), (2, 0.5), (2, 0.45698629803118)),
 ((3, 0.6000000000000001),
  (3, 0.6000000000000001),
  (3, 0.7142857142857143),
  (3, 0.5842167062067959)),
 ((4, 0.6000000000000001),
  (4, 0.7000000000000001),
  (4, 0.7142857142857143),
  (4, 0.4171170012545873)),
 ((5, 0.2), (5, 0.0), (5, 0.45454545454545453), (5, 0.1830727919118216)),
 ((6, 0.2), (6, 0.0), (6, 0.45454545454545453), (6, 0.1830727919118216))]
现在,迭代
p
的每一行:

for row in p:
    this_keys, this_values = zip(*row)
    assert len(set(this_keys)) == 1 # make sure we got what we expected
    this_key = this_keys[0]
    # Now, this_key is the key and this_values is a tuple of values
    print this_key, ['%.2f' % v for v in this_values]
输出为:

1 ['0.40', '0.00', '0.50', '0.46']
2 ['0.40', '0.00', '0.50', '0.46']
3 ['0.60', '0.60', '0.71', '0.58']
4 ['0.60', '0.70', '0.71', '0.42']
5 ['0.20', '0.00', '0.45', '0.18']
6 ['0.20', '0.00', '0.45', '0.18']

现在,不用打印,您可以将它们保存到csv文件中。

我找到了一个简单的答案,可以在一行上打印一个节点的多个测量结果

## calculate centrality metrics:
degree = nx.degree_centrality(G)
between = nx.betweenness_centrality(G)
close = nx.closeness_centrality(G)
eigen = nx.eigenvector_centrality(G)

## print the multiple centrality metrics to a single line for each node:
for n in G:
    print ("%d, %f, %f, %f, %f"%(n, degree[n], between[n], close[n], eigen[n]))