O'Reilly logo

Practical Data Science Cookbook - Second Edition by Abhijit Dasgupta, Benjamin Bengfort, Sean Patrick Murphy, Tony Ojeda, Prabhanjan Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(graph.degree().items(), 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), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required