NetworkX - writing results to CSV for multiple measures

这一生的挚爱 提交于 2021-01-24 18:58:03

问题


I'm using NetworkX to calculate 4 separate centrality metrics for a large network and now I would like to write the results to a CSV file. I don't want to write a single CSV file for each of the 4 metrics, but would rather have something like below:

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

Below is the my code to show what I've done so far:

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)

回答1:


I found a simple answer that will print the results of multiple measures for a node on a single line.

## 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]))



回答2:


You can iterate over the dictionaries in parallel.

First, convert each dictionary to a list of (key, value) tuples, sorted on keys:

degree = sorted(degree.items())
between = sorted(between.items())
close = sorted(close.items())
eigen = sorted(eigen.items())

Next, create a parallel iterator:

p = zip(degree, between, close, eigen)

Now, p is:

[((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))]

Now, iterate over each row of 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]

The output is:

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']

Now, instead of printing, you can save them to a csv file.



来源:https://stackoverflow.com/questions/23768079/networkx-writing-results-to-csv-for-multiple-measures

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