Book Image

Network Science with Python and NetworkX Quick Start Guide

By : Edward L. Platt
Book Image

Network Science with Python and NetworkX Quick Start Guide

By: Edward L. Platt

Overview of this book

NetworkX is a leading free and open source package used for network science with the Python programming language. NetworkX can track properties of individuals and relationships, find communities, analyze resilience, detect key network locations, and perform a wide range of important tasks. With the recent release of version 2, NetworkX has been updated to be more powerful and easy to use. If you’re a data scientist, engineer, or computational social scientist, this book will guide you in using the Python programming language to gain insights into real-world networks. Starting with the fundamentals, you’ll be introduced to the core concepts of network science, along with examples that use real-world data and Python code. This book will introduce you to theoretical concepts such as scale-free and small-world networks, centrality measures, and agent-based modeling. You’ll also be able to look for scale-free networks in real data and visualize a network using circular, directed, and shell layouts. By the end of this book, you’ll be able to choose appropriate network representations, use NetworkX to build and characterize networks, and uncover insights while working with real-world systems.
Table of Contents (15 chapters)

Your first network in NetworkX

Now, let's create and visualize a small network using NetworkX! For the code in this book, you will need Python 3.4 or higher and NetworkX 2.2 or greater. I also highly recommend Jupyter Lab as an interactive Python environment. The code in this book is available as Jupyter notebooks at https://github.com/PacktPublishing/Network-Science-with-Python-and-NetworkX-Quick-Start-Guide.

The code in this book was written using NetworkX 2.3. At time of writing, NetworkX 2.3 is available but has not been officially released. All of the examples will work with NetworkX 2.2, but may have minor differences in node color and formatting.

The following example creates an undirected, unweighted network, adds edges and nodes, and then generates a visualization. Other types of networks will be discussed in Chapter 2, Working with Networks in NetworkX. First, we import the networkx package. In this book, I will use the convention of importing the library with the alias nx:

import networkx as nx

Next, we create a Graph object, representing an undirected network, given as follows:

G = nx.Graph()

Now that the graph exists, we can add nodes one at a time with the add_node() method, or all at once with add_nodes_from(). When adding nodes to a network, each node has to have a unique ID. The ID can be a number, a string, or a tuple. In fact, you can use any Python object as an ID, as long as it has a __hash__() method defined. For this example, we'll use letters as node IDs, shown as follows:

G.add_node('A')
G.add_nodes_from(['B', 'C'])

Similarly, edges can be added one at a time with add_edge(), or all at once with add_edges_from(), shown as follows:

G.add_edge('A', 'B')
G.add_edges_from([('B', 'C'), ('A', 'C')])

So far, the A, B, and C nodes, as well as the edges connecting them, have been added. The following code draws a simple visualization of the network:

plt.figure(figsize=(7.5, 7.5))
nx.draw_networkx(G)
plt.show()

In the preceding code, figure() is used to create a 7.5 by 7.5 inch figure, which will hold the visualization. The draw_networkx() function uses the Graph object G to produce a visualization. The show() function renders the visualization, but can be omitted if you are running the examples in Jupyter Lab. The visualization should appear similar to the following:

Output of draw_networkx()
NetworkX has several functions for visualizing networks, each of which allows you to customize the visualization style. Some of these functions and parameters are discussed over the remaining chapters, in particular Chapter 11, Visualization.

Your visualization may look slightly different from the one shown previously. This is because visualizations in NetworkX sometimes use randomized algorithms. The randomized algorithms can be configured to produce the same output each time by setting the random seed. The following code sets the random seeds used by NetworkX (this code will also appear at the beginning of the example code for all future chapters):

import random
from numpy import random as nprand
seed = hash("Network Science in Python") % 2**32
nprand.seed(seed)
random.seed(seed)
The pyplot figure() function is used in the preceding code to set the size of the visualization figure, but doing that each time can be tedious. Instead, it is possible to set the default figure size as follows:

plt.rcParams.update({'figure.figsize': (7.5, 7.5)})

Nodes can also be added using a nifty shortcut. If you try to add an edge referring to a node ID that isn't in the network, NetworkX will automatically add the node! So, in practice, you won't often need to call add_node() directly. The following code adds nodes and edges so that the network matches the example network from earlier and creates a new visualization:

G.add_edges_from([('B', 'D'), ('C', 'E')]) 
nx.draw_networkx(G)

The draw_networkx() function now produces a visualization including the new D and E nodes:

Output of draw_networkx() after adding nodes