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

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.