NetworkX-将结果写入CSV以进行多种测量 [英] NetworkX - writing results to CSV for multiple measures

查看:69
本文介绍了NetworkX-将结果写入CSV以进行多种测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetworkX为大型网络计算4个单独的中心度度量,现在我想将结果写入CSV文件.我不想为这四个指标中的每个指标编写一个CSV文件,但是希望有以下内容:

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

以下是我的代码,以显示到目前为止的操作:

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)

推荐答案

我找到了一个简单的答案,该答案将在一行上打印一个节点的多个度量的结果.

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

这篇关于NetworkX-将结果写入CSV以进行多种测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆