我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用networkx.out_degree_centrality()。
def central_list(E): centralities = [] centralities.append(nx.in_degree_centrality(E)) centralities.append(nx.out_degree_centrality(E)) centralities.append(nx.closeness_centrality(E)) centralities.append(nx.betweenness_centrality(E)) centralities.append(nx.eigenvector_centrality(E)) for node in E.nodes_iter(): measures = ("\t").join(map(lambda f: str(f[node]), centralities)) print("%s: %s" % (node, measures))
def sorting(E): in_degree_central = nx.in_degree_centrality(E) sorted(in_degree_central.items(), key=lambda x: x[1], reverse=True)[:10] out_degree_central = nx.out_degree_centrality(E) sorted(out_degree_central.items(), key=lambda x: x[1], reverse=True)[:10] print(in_degree_central) print(out_degree_central)
def centrality(DG): in_degree_centrality = nx.in_degree_centrality(DG) out_degree_centrality = nx.out_degree_centrality(DG) with open('/home/sun/PycharmProjects/Network/in_degree_centrality.csv', 'w') as f: for k, v in in_degree_centrality.items(): f.write(str(k) + ': ' + str(v) + '\n') f.close() with open('/home/sun/PycharmProjects/Network/out_degree_centrality.csv', 'w') as f: for k, v in out_degree_centrality.items(): f.write(str(k) + ': ' + str(v) + '\n') f.close() # def main(): # data = '/home/sun/PycharmProjects/Network/C-elegans-frontal.txt' # # data = 'www.adj' # DG = create_network(data) # # # draw_network(DG) # # clustering_coefficient(DG) # # centrality(DG) # degree_distribution(DG) # # if __name__ == '__main__': # main() # # # DG = nx.DiGraph() # # DG.add_edge(1,2) # # print(DG.edges()) # # # pos = nx.nx_agraph.graphviz_layout(DG) # # nx.draw_networkx(DG, pos = nx.spring_layout(DG)) # # plt.show() # # plt.ishold() # # plt.draw(DG)
def out_degree_centrality(self): """ Parameters ---------- Returns ------- NxGraph: Graph object Examples -------- >>> """ return nx.out_degree_centrality(self._graph)
def graph_info(g): result = {} components = list(nx.strongly_connected_component_subgraphs(g)) in_degrees = g.in_degree() out_degrees = g.out_degree() highest_in_degree_node = sorted(in_degrees, key = lambda x: in_degrees[x], reverse = True)[0] highest_out_degree_node = sorted(out_degrees, key = lambda x: out_degrees[x], reverse = True)[0] result['highest in_degree node'] = highest_in_degree_node result['highest out_degree_node'] = highest_out_degree_node result['numnber of components'] = len(components) result['number of nodes'] = g.number_of_nodes() result['number of edges'] = g.number_of_edges() #Degree centrality in_degree_centrality = nx.in_degree_centrality(g) out_degree_centrality = nx.out_degree_centrality(g) result['sorted in_degree centrality'] = sorted([(el,in_degree_centrality[el]) for el in g.nodes()], key = lambda x: x[1], reverse = True) result['sorted out_degree centrality'] = sorted([(el,out_degree_centrality[el]) for el in g.nodes()], key = lambda x: x[1], reverse = True) result['closeness_centrality'] = sorted([(el,nx.closeness_centrality(g)[el]) for el in nx.closeness_centrality(g)], key = lambda x: x[1], reverse = True) result['highest in_degree node closeness'] = nx.closeness_centrality(g)[highest_in_degree_node] result['highest out_degree node closeness'] = nx.closeness_centrality(g)[highest_out_degree_node] result['betweenness centrality'] = sorted([(el,nx.betweenness_centrality(g)[el]) for el in nx.betweenness_centrality(g)], key = lambda x: x[1], reverse = True) result['highest in_degree node betweenness'] = nx.betweenness_centrality(g)[highest_in_degree_node] result['highest in_degree node betweenness'] = nx.betweenness_centrality(g)[highest_out_degree_node] largest_component = sorted (components, key = lambda x: x.number_of_nodes(), reverse = True)[0] result['largest strongly component percent'] = largest_component.number_of_nodes()/float(g.number_of_nodes()) result['largest strongly component diameter'] = nx.diameter(largest_component) result['largest strongly component average path length'] = nx.average_shortest_path_length(largest_component) result['average_degree (undireceted)'] = sum(g.degree().values())/float(g.number_of_nodes()) result['avg_cluster_coefficient (transitivity)'] = nx.transitivity(g) return result
def main(): domain_name = 'baidu.com' domain_pkts = get_data(domain_name) node_cname, node_ip, visit_total, edges, node_main = get_ip_cname(domain_pkts[0]['details']) for i in domain_pkts[0]['details']: for v in i['answers']: edges.append((v['domain_name'],v['dm_data'])) DG = nx.DiGraph() DG.add_edges_from(edges) # ?????????IP?node for node in DG: if node in node_main and DG.successors(node) in node_ip: print node # ??cname???IP???? for node in DG: if node in node_cname and DG.successors(node) not in node_cname: # ???ip?????cname print "node",DG.out_degree(node),DG.in_degree(node),DG.degree(node) # ?cname??????? # for node in DG: # if node in node_cname and DG.predecessors(node) not in node_cname: # print len(DG.predecessors(node)) for node in DG: if node in node_main: if len(DG.successors(node)) ==3: print node print DG.successors(node) # print sorted(nx.degree(DG).values()) print nx.degree_assortativity_coefficient(DG) average_degree = sum(nx.degree(DG).values())/(len(node_cname)+len(node_ip)+len(node_main)) print average_degree print len(node_cname)+len(node_ip)+len(node_main) print len(edges) print nx.degree_histogram(DG) # print nx.degree_centrality(DG) # print nx.in_degree_centrality(DG) # print nx.out_degree_centrality(DG) # print nx.closeness_centrality(DG) # print nx.load_centrality(DG)