Networkx集中度度量“入站/出站边缘”的“计数”

时间:2019-05-22 14:05:42

标签: python-3.x networkx

我正在尝试获取有向图的入站/出站边的绝对数量。中心性为我提供了可从选定节点连接的部分节点。我想比较多个子图,分数/比率没有给我精确的比较。

我目前正在将DiGraph.number_of_nodes()与Centrality度量相乘。但我不确定这是最简单的方法。是否有现有的Networkx内置算法或功能?

G=nx.from_pandas_edgelist(df,'Id_From','Id_To',edge_attr=True,create_using=nx.DiGraph)
size=G.number_of_nodes()
DegreeCentrality=nx.degree_centrality(G)
DegreeCentrality.update(x*size for x in DegreeCentrality.items())


Example expected result
Node1: InDegreeCentrality: 4 (number of incoming edges to node1)
Node1: OutDegreeCentrality: 3 (number of outgoing edges from node1)
Node1: DegreeCentrality: 7 (Total number of edges linked to node1)
..
..

2 个答案:

答案 0 :(得分:0)

看起来您想对所有节点进行度内和度外汇总。 Networkx具有内置功能-in_degree和out_degree。您可以使用遍历节点和以下功能的生成器:

import networkx as nx
import numpy as np

G = nx.fast_gnp_random_graph(20, 0.3, directed=True)
print('IN AVG:', np.mean([G.in_degree(n) for n in G.nodes]))
print('OUT AVG:', np.mean([G.out_degree(n) for n in G.nodes]))
print('ALL AVG:', np.mean([G.degree(n) for n in G.nodes]))
print('IN SUM:', np.sum([G.in_degree(n) for n in G.nodes]))
print('OUT SUM:', np.sum([G.out_degree(n) for n in G.nodes]))
print('ALL SUM:', np.sum([G.degree(n) for n in G.nodes]))

它将打印:

IN AVG: 5.65
OUT AVG: 5.65
ALL AVG: 11.3
IN SUM: 113
OUT SUM: 113
ALL SUM: 226

答案 1 :(得分:0)