gpt4 book ai didi

python - NetworkX/Pandas - 无法将节点的度数输出到 .txt 文件中(错误消息)

转载 作者:行者123 更新时间:2023-12-01 09:06:51 29 4
gpt4 key购买 nike

我需要将网络节点的度(以及其他中心性指标)输出到 .txt 文件中。我在以前的 NetworkX/Pandas 版本中能够做到这一点,但现在我收到错误。

我正在使用 NetworkX 版本。 2.1 和 Pandas 版本。 0.23.4:

import pandas as pd
import networkx as nx

G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6)])

df = pd.DataFrame(dict(
DEGREE = nx.degree(G),
DEGREE_CENTRALITY = nx.degree_centrality(G),
EIGENVECTOR = nx.eigenvector_centrality(G),
KATZ = nx.katz_centrality_numpy(G),
CLOSENESS_CENTRALITY = nx.closeness_centrality(G),
BETWEENNESS_CENTRALITY = nx.betweenness_centrality(G),
CLUSTCOEF = nx.clustering(G),
))
#df.index += 1
#df.to_csv('centrality-metrics.csv')

错误信息是:

Traceback (most recent call last):
File "<stdin>", line 8, in <module>
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 348, in __init__
mgr = self._init_dict(data, index, columns, dtype=dtype)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 459, in _init_dict
return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 7356, in _arrays_to_mgr
index = extract_index(arrays)
File "/home/arthur/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 7405, in extract_index
raise ValueError('Mixing dicts with non-Series may lead to '
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.

更新:如果我注释(#) DEGREE 行,则不会发生错误。输出 .txt 与此类似(我的原始数据来自基因网络):

GENE    BETWEENNESS CLOSENESS   CLUSTCOEF   DEGREE  DEGREE_CENTRALITY   EIGENVECTOR KATZ
A1BG 0.000142303010695361 0.318553702985653 0.0350877192982456 19 0.00115635080031647 7.26316435442522e-05 0.00556190357079266
A1CF 5.71271005407417e-05 0.33571705861921 0.375324675324675 56 0.00340819183251172 0.000225887126821305 -0.00251062164644857
A2M 0.000886299232394493 0.370534908894101 0.109265734265734 146 0.00888564299190554 0.00294316042120819 0.0163918804690203
A4GALT 7.38971769935498e-07 0.286369102602088 0 4 0.000243442273750837 1.35050378606586e-05 -0.000361261465931375
A4GNT 1.03038198147882e-05 0.297404430929626 0 15 0.000912908526565638 2.45985150882602e-05 0.00562955611859571

最佳答案

好吧,在阅读了上面的评论(@Joel)并执行了little research之后,我们可以使用dict(G. Degree)而不是nx. Degree:

import networkx as nx

G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6)])

df = pd.DataFrame(dict(
DEGREE = dict(G.degree),
DEGREE_CENTRALITY = nx.degree_centrality(G),
EIGENVECTOR = nx.eigenvector_centrality(G),
KATZ = nx.katz_centrality_numpy(G),
CLOSENESS_CENTRALITY = nx.closeness_centrality(G),
BETWEENNESS_CENTRALITY = nx.betweenness_centrality(G),
CLUSTCOEF = nx.clustering(G),
))

输出:

   DEGREE  DEGREE_CENTRALITY  EIGENVECTOR      KATZ  CLOSENESS_CENTRALITY  BETWEENNESS_CENTRALITY  CLUSTCOEF
1 2 0.4 0.456984 0.408764 0.500000 0.0 1.000000
2 2 0.4 0.456984 0.408764 0.500000 0.0 1.000000
3 3 0.6 0.584217 0.448883 0.714286 0.6 0.333333
4 3 0.6 0.417120 0.441314 0.714286 0.7 0.000000
5 1 0.2 0.183076 0.367131 0.454545 0.0 0.000000
6 1 0.2 0.183076 0.367131 0.454545 0.0 0.000000

关于python - NetworkX/Pandas - 无法将节点的度数输出到 .txt 文件中(错误消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974825/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com