Reader small image

You're reading from  Network Science with Python

Product typeBook
Published inFeb 2023
PublisherPackt
ISBN-139781801073691
Edition1st Edition
Right arrow
Author (1)
David Knickerbocker
David Knickerbocker
author image
David Knickerbocker

David Knickerbocker is the chief engineer and co-founder of VAST-OSINT. He has over two decades of rich experience working with and around data in his career, with his focus being on data science, data engineering, software development, and cybersecurity.
Read more about David Knickerbocker

Right arrow

Getting started with community detection

Before we can start, we need a network to use. Let’s use NetworkX’s Les Miserables graph that we used in the previous chapter since it held several separate communities:

  1. Loading the network is simple:
    import networkx as nx
    import pandas as pd
    G = nx.les_miserables_graph()

That’s all it takes to load the graph.

  1. There is a weight attribute that I do not want to include in the network because we don’t need edge weights for this simple demonstration. So, I’m going to drop it and rebuild the graph:
    df = nx.to_pandas_edgelist(G)[['source', 'target']]
    # dropping 'weight'
    G = nx.from_pandas_edgelist(df)

In those two steps, we converted the Les Miserables graph into a pandas edge list, and we kept only the source and target fields, effectively dropping the weight field. Let’s see how many nodes and edges exist in the network:

nx.info(G)
'Graph with...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Network Science with Python
Published in: Feb 2023Publisher: PacktISBN-13: 9781801073691

Author (1)

author image
David Knickerbocker

David Knickerbocker is the chief engineer and co-founder of VAST-OSINT. He has over two decades of rich experience working with and around data in his career, with his focus being on data science, data engineering, software development, and cybersecurity.
Read more about David Knickerbocker