Networkx Statistical Inference

BKS picture BKS · Mar 15, 2015 · Viewed 8k times · Source

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!

Answer

Marcus Müller picture Marcus Müller · Mar 15, 2015

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