Networkx 统计推断

Networkx Statistical Inference

我有一个使用 networkx 成功创建的有向加权图。

我正在尝试在此网络上生成一些统计推断,但我遇到了麻烦。他们在这里:

(i) 网络的平均度数。 (我能找到的唯一一个是average_degree_connectivity,returns一个字典,而不是一个全网平均度的浮点数)

(ii) 网络的平均加权度。 (同上)

(iii) 网络的平均聚类系数。 (我知道我必须使用 nx.average_clustering(g),但是我如何考虑它是加权有向图这一事实?我不断收到错误消息: NetworkXError: ('Clustering algorithms are not defined ', 'for directed graphs.') )

谢谢!

(i) The Average degree of the network. (The only one I could find was average_degree_connectivity, which returns a dictionary and not a single float with the average degree of the whole network)

假设您的 Graph 对象是 G

degrees = G.degree()
sum_of_edges = sum(degrees.values())

计算平均值只是除以节点数的问题。

(ii) The Average weighted degree of the network. (same as above)

获取所有节点的列表,为每个节点获取所有边的列表,为每个节点求和权重 属性:

edgesdict = G.edge
total = 0
for node_adjacency_dict in edgesdict.valuess():
    total += sum([adjacency.get("weight",0) for adjacency in node_adjacency_dict.values()]) 

(iii) The Average clustering coefficient of the network. (I know that I have to use nx.average_clustering(g), however how do I take the fact that it is weighted directed graph into consideration? I keep getting the error: NetworkXError: ('Clustering algorithms are not defined ', 'for directed graphs.') )

要点是,在您定义它之前,它的定义并不明确。我认为,对于 Whosebug 的回答来说,这有点繁琐,所以我留给您解决为您的特定问题定义算法的问题。

(iv) The maximum shortest path length in the giant component of the network. (i know you find the giant component as such: giant = max(nx.connected_component_subgraphs(G), key=len) but how do we get the max shortest path length in it?)

运行 ipython 之类的。输入 giant.;你会得到一个你可以用 giant.

做的事情的列表

这里还有一些想法可以添加到@marcus-müller 所写的内容中。

对于平均度数(注意你的有向图,这是进出度数的总和)

In [1]: import networkx as nx
G
In [2]: G = nx.DiGraph()

In [3]: G.add_edge(1,2,weight=7)

In [4]: G.add_edge(3,4,weight=17)

In [5]: sum(G.degree().values())/float(len(G))
Out[5]: 1.0

In [6]: sum(G.degree(weight='weight').values())/float(len(G))
Out[6]: 12.0

NetworkX 中没有加权定向聚类的定义。已经有提案 https://github.com/networkx/networkx/issues/859 但还没有人完成实施。

对于有向图,最大连通分量的概念分为两​​个定义。弱连通分量是指当您考虑图的无向版本(用无向边替换每条有向边)时连通的分量。强连通组件是指每对节点都可以相互访问(有路径)的组件。

关于第 (i) 点:网络的平均度数,对于有向图, Networkx 内部计算如下:

nnodes = G.number_of_nodes()
deg = sum(d for n, d in G.in_degree()) / float(nnodes)
info += "Average in degree: %8.4f\n" % deg
deg = sum(d for n, d in G.out_degree()) / float(nnodes)
info += "Average out degree: %8.4f" % deg

你可以check it on github.

自 Networkx 2.5 (2021) 起,您可以使用 networkx.info(G) 获取图形的平均度数

代码:

print(nx.info(G))

输出:

Name:
Type: Graph
Number of nodes: 85
Number of edges: 63
Average degree:   1.4824