Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Networkx Statistical Inference

I have a directed weighted graph which I created successfully using networkx.

I'm trying to generate some statistical inferences on this network, but I'm having trouble. Here they are:

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

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

(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.') )

Thank you!

like image 589
BKS Avatar asked Mar 15 '15 18:03

BKS


3 Answers

As of Networkx 2.5 (2021) you can use networkx.info(G) to get the average degree of the graph

Code:

print(nx.info(G))

Output:

Name:
Type: Graph
Number of nodes: 85
Number of edges: 63
Average degree:   1.4824
like image 170
zachary chua Avatar answered Oct 15 '22 04:10

zachary chua


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

Assuming your Graph object is G.

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

Calculating the average is just a matter of division by the number of nodes.

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

Get a list of all nodes, for each of these get a list of all edges, for each of these sum up the weight property:

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.') )

The point is that it's not well-defined until you define it. That's a bit much to do for a StackOverflow answer, I think, so I'm leaving you with the problem of defining an algorithm for your specific problem.

(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?)

run ipython or something. Type in giant.; you will get a list of things that you can do with giant.

like image 9
Marcus Müller Avatar answered Oct 19 '22 02:10

Marcus Müller


Here are a couple more ideas to add to what @marcus-müller wrote.

For average degree (note for your digraph this is the sum of in and out degrees)

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

There are no definitions for weighted directed clustering in NetworkX. There have been proposals https://github.com/networkx/networkx/issues/859 but nobody has finished the implementation yet.

For directed graphs the concept of largest connected component is split into two definitions. Weakly connected components are those that are connected when you consider the undirected version of the graph (replace every directed edge with an undirected one). Strongly connected components are those components where every pair of nodes is reachable from each other (has a path).

like image 4
Aric Avatar answered Oct 19 '22 03:10

Aric