Network Analysis
Network analysis has a large variety of applications you can try your hand at including social structure analysis, fraud detection and more.
In this playbook you will analyze the structure of graphs using the NetworkX
package. You will use the popular Zachary's karateclub dataset (source).
In the first step of this template, you will load the data, which is in a matrix format, into a NetworkX
graph. Afterwards, you will explore the network and derive insights from it in step 2 and 3. Finally, you will visualize these insights in step 4.
# Load packages
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import pandas as pd
import seaborn as sns
color = sns.color_palette()
1. Load your data
Load data from file
The data should be in matrix format. E.g:
where every element is seperated by a comma in a csv file.
This network is symmetric and would indicate that person 1 and 2 are connected, so are person 1 and 5, and so on.
We will load the very well-known Zachary's karateclub dataset. This dataset is also symmetric, which means that if person 1 is connected to person 2, person 2 is also connected to person 1.
# Load data into numpy matrix
FILENAME = "zachary_karateclub.csv"
data=np.loadtxt(open(FILENAME, "rb"), delimiter=",")
# Show matrix to check if it is correctly loaded
# A black dot indicates that there is a connection between the two persons
_=plt.imshow(data, cmap='gray_r', aspect='equal')
data
Load data into networkx
graph
networkx
graph# Set up networkx graph G from numpy matrix
G = nx.from_numpy_matrix(data)
2. Explore the network
Now that the data has been loaded in a graph correctly, we can start exploring it.
Number of edges and nodes in graph
print(f'The number of edges in the graph: {len(G.nodes())}')
print(f'The number of nodes in the graph: {len(G.edges())}')
List of neighbors for any node
# Node to determine the neighbors for
node = 2
print(f'The neighbors of node {node} are {list(G.neighbors(node))}')
# You can also print it for every node
#for node in G.nodes():
# print(f'The neighbors of node {node} are {list(G.neighbors(node))}')
Show a basic visualization of the graph
# Show basic visualization of graph
nx.draw(G, with_labels=True)