How to do it...

The following steps will identify key players in this network of comic book characters:

  1. To find the top ten nodes in the heroes network, we compute the nodes' degree and sort them:
import operator>>> degrees = sorted(, key=operator.itemgetter(1), reverse=True)>>> for node in degrees: print node
  1. Additionally, we compute the percent of nodes in the graph that a node is connected to; NetworkX provides a helpful function, degree_centrality, to do this for us. While we're at it, we might as well also set this as a property for our nodes for easy lookup:
>>> centrality = nx.degree_centrality(graph)>>> nx.set_node_attribues(graph, 'centrality', centrality)>>> degrees = sorted(centrality.items(), key=itemgetter(1), ...

Get Practical Data Science Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.