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

These steps will guide you through the detection of communities within social networks:

  1. Let's actually get into some clustering. The python-louvain library uses NetworkX to perform community detection with the louvain method. Here is a simple example of cluster partitioning on a small, built-in social network:
G = nx.karate_club_graph()#first compute the best partitionpartition = community.best_partition(G)#drawingpos = nx.spring_layout(G)plt.figure(figsize=(12,12))plt.axis('off')nx.draw_networkx_nodes(G, pos, node_size=200, cmap=plt.cm.RdYlBu, node_color=partition.values())nx.draw_networkx_edges(G,pos, alpha=0.5)plt.savefig("figure/karate_communities.png")  

The following is the resulting graph with shades of grey and/or ...

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