Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Network Analysis in Python
A network is a collection of nodes and edges that represent the relationships or connections between those nodes. The nodes can represent various entities, such as individuals, organizations, genes, or websites, while the edges represent the connections or interactions between them.
Network analysis is the study of the relationships between these entities represented as a network. It involves the use of mathematical, statistical and computational techniques to provide insights into the behavior of complex systems and help make informed decisions in various domains.
Python offers us a package called NetworkX which is of great help for creation, manipulation, and analysis of complex networks. Before proceeding, install NetworkX using the following command:
pip install networkx
Creating a Simple Graph
Here we create a simple graph using the NetworkX library with 3 nodes and 3 edges. We then calculate the degree and clustering coefficient of each node and visualize the graph.
Degree of a node: The number of neighbors a node has in the graph, calculated by counting the number of edges connected to the node.
Clustering coefficient: A measure of the degree to which the neighbors of a node are connected to each other. It measures the density of local connections around a particular node, calculated by dividing the number of edges between the node's neighbors by the maximum possible number of such edges.
Example
import networkx as nx
import matplotlib.pyplot as plt
# Create an empty graph
G = nx.Graph()
# Add nodes
G.add_node(1)
G.add_node(2)
G.add_node(3)
# Add edges
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
print("Node degree:")
for node in G.nodes():
print(f"{node}: {G.degree(node)}")
print("\nNode clustering coefficient:")
for node in G.nodes():
print(f"{node}: {nx.clustering(G, node)}")
# Draw the graph
nx.draw(G, with_labels=True)
plt.show()
Node degree: 1: 2 2: 2 3: 2 Node clustering coefficient: 1: 1.0 2: 1.0 3: 1.0
Identifying Communities
Community detection partitions graph nodes into groups based on similar characteristics. The Louvain algorithm is commonly used for this purpose. It optimizes modularity?a measure of how many edges exist within communities compared to a random graph.
The Louvain algorithm works in two phases:
- Each node starts in its own community, then nodes are moved between communities to increase modularity until no improvement is possible
- A new graph is constructed where each node represents a community from phase 1, then phase 1 is applied to this coarser-level graph
Example
import networkx as nx
import matplotlib.pyplot as plt
# Create a random graph with 7 nodes and 10 edges
G = nx.gnm_random_graph(7, 10)
print("Node degree:")
for node in G.nodes():
print(f"{node}: {G.degree(node)}")
print("\nNode betweenness centrality:")
bc = nx.betweenness_centrality(G)
for node in bc:
print(f"{node}: {bc[node]:.3f}")
# Community detection using greedy modularity
communities = nx.algorithms.community.modularity_max.greedy_modularity_communities(G)
# Print communities
for i, community in enumerate(communities, 1):
print(f"\nCommunity {i}: {community}")
print(f"Number of nodes: {len(community)}")
# Color nodes by community
color_map = []
for node in G.nodes():
for i, community in enumerate(communities):
if node in community:
color_map.append(i)
break
# Draw graph with community colors
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
Node degree:
0: 3
1: 2
2: 4
3: 3
4: 2
5: 2
6: 4
Node betweenness centrality:
0: 0.167
1: 0.000
2: 0.500
3: 0.133
4: 0.000
5: 0.000
6: 0.200
Community 1: frozenset({0, 1, 3, 5})
Number of nodes: 4
Community 2: frozenset({2, 4, 6})
Number of nodes: 3
Analyzing Homophily
Homophily is the tendency of similar individuals to associate with each other based on shared characteristics like beliefs, values, or demographics. This phenomenon shapes network structure by creating connections between similar nodes.
The homophily coefficient measures this tendency using nx.attribute_assortativity_coefficient(). It ranges from ?1 to 1, where positive values indicate higher likelihood of similar nodes connecting.
Example
import networkx as nx
import matplotlib.pyplot as plt
# Create a random graph
G = nx.gnm_random_graph(20, 40)
# Assign binary attributes to nodes
for node in G.nodes():
G.nodes[node]['type'] = 'A' if node < 10 else 'B'
# Color nodes by type
color_map = ['red' if G.nodes[node]['type'] == 'A' else 'blue' for node in G.nodes()]
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
# Calculate homophily coefficient
homophily_coeff = nx.attribute_assortativity_coefficient(G, 'type')
print(f"Homophily coefficient: {homophily_coeff:.4f}")
# Interpret the result
if homophily_coeff > 0:
print("Positive homophily: Similar nodes tend to connect")
elif homophily_coeff < 0:
print("Negative homophily: Different nodes tend to connect")
else:
print("No homophily pattern detected")
Homophily coefficient: -0.0244 Negative homophily: Different nodes tend to connect
Key NetworkX Functions
| Function | Purpose | Usage |
|---|---|---|
nx.degree() |
Node connectivity | Count edges per node |
nx.clustering() |
Local density | Measure neighborhood connectivity |
nx.betweenness_centrality() |
Node importance | Find bridge nodes |
greedy_modularity_communities() |
Community detection | Group similar nodes |
Conclusion
Network analysis with Python's NetworkX library enables researchers to study complex systems through graph theory. Key techniques include measuring node properties, detecting communities, and analyzing homophily patterns to understand network structure and dynamics.
