我尝试使用代码:'nx.average_clustering(G1)'
但返回:'NetworkXError:图形未连接。'
我想知道不连接Graph时如何计算平均聚类。非常感谢您的帮助。
我已经找到了解决此问题的方法。参考链接:https://groups.google.com/forum/#!topic/networkx-discuss/mp5AEebIeKo代码:
import pprint
import numpy
import networkx as nx
def average_clustering_for_all(G1):
tempgraph1=G1.copy();
if nx.is_connected(tempgraph1):
# Normal case, the graph is connected
average=nx.average_clustering(tempgraph1);
else:
# Try to see if the graph is not connected because of isolated nodes
tempgraph1.remove_nodes_from(nx.isolates(tempgraph1));
if nx.is_connected(tempgraph1):
# Compute the graph average path without isolated nodes
average=nx.average_clustering(tempgraph1);
else:
# Compute the average shortest path for each subgraph and mean it!
subgraphs = nx.connected_component_subgraphs(tempgraph1)
average=0;
for sb in subgraphs:
pprint.pprint(sb.degree());
average+=nx.average_clustering(sb);
if len(list(subgraphs)) == 0:
continue
if len(list(subgraphs))!=0:
average/=(len(list(subgraphs)))
return average;
average_clustering_for_all(G1) code here