Networks, which are also called graphs, are one of the most interesting parts of data science, and interest in them has exploded in recent years. It is very useful to be able to model the relationship between data points. This module explains some basic ideas in network science and shows how to do network analysis in Python using the networkx package, which is the most popular way to do network analysis in Python.
Lesson 1: Why Analyzing Networks?
A network also called a "graph," is a group of "nodes" and "edges" that lets us show how the nodes are connected. Network science has become very popular in recent years because it can be used in so many different ways.
Since social media has grown so quickly, social network analysis is a common use case. This is what we will talk about here. Networks can also be used to model pandemics and biological systems. The ability to model how agents work together can be used in many different ways. That is why we should learn to analyze networks.
The Code:
#Import the required packages
import sys
import networkx as nx
import matplotlib.pyplot as plt
print(f"Python version {sys.version}")
print(f"networkx version: {nx.__version__}")
#Let's import the ZKC graph:
ZKC_graph = nx.karate_club_graph()
#Let's keep track of which nodes represent John A and Mr Hi
Mr_Hi = 0
John_A = 33
#Let's display the labels of which club each member ended up joining
club_labels = nx.get_node_attributes(ZKC_graph,'club')
#just show a couple of the labels
print({key:club_labels[key] for key in range(10,16)})
A = nx.convert_matrix.to_numpy_matrix(ZKC_graph)
A
#To plot using networkx we first need to get the positions we want for each node.
#Here we will use a ciruclar layout but there are many other variations you could choose!
circ_pos = nx.circular_layout(ZKC_graph)
#Use the networkx draw function to easily visualise the graph
nx.draw(ZKC_graph,circ_pos)
#let's highlight Mr Hi (green) and John A (red)
nx.draw_networkx_nodes(ZKC_graph, circ_pos, nodelist=[Mr_Hi], node_color='g', alpha=1)
nx.draw_networkx_nodes(ZKC_graph, circ_pos, nodelist=[John_A], node_color='r', alpha=1)
density = nx.density(ZKC_graph)
print('The edge density is: ' + str(density))
#the degree function in networkx returns a DegreeView object capable of iterating through (node, degree) pairs
degree = ZKC_graph.degree()
degree_list = []
for (n,d) in degree:
degree_list.append(d)
av_degree = sum(degree_list) / len(degree_list)
print('The average degree is ' + str(av_degree))
#we now plot the degree distribution to get a better insight
plt.hist(degree_list,label='Degree Distribution')
plt.axvline(av_degree,color='r',linestyle='dashed',label='Average Degree')
plt.legend()
plt.ylabel('Number of Nodes')
plt.title('Karate Club: Node Degree')
#Now we can compute the local clustering coefficient
local_clustering_coefficient = nx.algorithms.cluster.clustering(ZKC_graph)
#lets find the average clustering coefficient
av_local_clustering_coefficient = sum(local_clustering_coefficient.values())/len(local_clustering_coefficient)
#similarly to the degree lets plot the local clustering coefficient distribution
plt.hist(local_clustering_coefficient.values(),label='Local Clustering Coefficient Distribution')
plt.axvline(av_local_clustering_coefficient,color='r',linestyle='dashed',label='Average Local Clustering Coefficient')
plt.legend()
plt.ylabel('Number of Nodes')
plt.title('Local Clustering Coefficient')
plt.show()
from networkx.algorithms.community.modularity_max import greedy_modularity_communities
#preform the community detection
c = list(greedy_modularity_communities(ZKC_graph))
#Let's find out how many communities we detected
print(len(c))
#Lets see these 3 clusters
community_0 = sorted(c[0])
community_1 = sorted(c[1])
community_2 = sorted(c[2])
print(community_0)
print(community_1)
print(community_2)
#draw each set of nodes in a seperate colour
nx.draw_networkx_nodes(ZKC_graph,circ_pos, nodelist=community_0, node_color='g', alpha=0.5)
nx.draw_networkx_nodes(ZKC_graph,circ_pos, nodelist=community_1, node_color='r', alpha=0.5)
nx.draw_networkx_nodes(ZKC_graph,circ_pos, nodelist=community_2, node_color='b', alpha=0.5)
#now we can add edges to the drawing
nx.draw_networkx_edges(ZKC_graph,circ_pos,width = 0.2)
#finally we can add labels to each node corresponding to the final club each member joined
nx.draw_networkx_labels(ZKC_graph,circ_pos,club_labels,font_size=9)
plt.show()
#draw the network as before
nx.draw_networkx_nodes(ZKC_graph, circ_pos, nodelist=community_0, node_color='g', alpha=0.5)
nx.draw_networkx_nodes(ZKC_graph, circ_pos, nodelist=combined_community, node_color='m', alpha=0.5)
nx.draw_networkx_edges(ZKC_graph, circ_pos,width = 0.5)
nx.draw_networkx_labels(ZKC_graph, circ_pos, club_labels, font_size=9)
plt.show()
Watch video Network Analysis in Python online without registration, duration hours minute second in high quality. This video was added by user Nuruzzaman Faruqui 24 August 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 14,078 once and liked it 193 people.