| name | Network Analysis |
| description | Analyze network structures, identify communities, measure centrality, and visualize relationships for social networks and organizational structures |
Network Analysis
Overview
This skill enables analysis of network structures to identify communities, measure centrality, detect influential nodes, and visualize complex relationships in social networks, organizational structures, and interconnected systems.
When to Use
- Analyzing social networks to identify influential users and community structures
- Mapping organizational hierarchies and identifying key connectors or bottlenecks
- Studying citation networks to find impactful research papers and collaboration patterns
- Building recommendation systems based on network relationships and similarities
- Analyzing supply chain networks to optimize logistics and identify vulnerabilities
- Detecting fraud patterns through network analysis of financial transactions
Network Concepts
- Nodes: Individual entities
- Edges: Connections/relationships
- Degree: Number of connections
- Centrality: Node importance measures
- Community: Densely connected groups
- Clustering Coefficient: Local density
Key Metrics
- Degree Centrality: Number of connections
- Betweenness Centrality: Control over paths
- Closeness Centrality: Average distance to others
- Eigenvector Centrality: Connections to important nodes
- Modularity: Community structure strength
Implementation with Python
pandas pd
numpy np
matplotlib.pyplot plt
networkx nx
collections defaultdict, Counter
seaborn sns
G = nx.Graph()
nodes = [
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
(, {: , : }),
]
node, attrs nodes:
G.add_node(node, **attrs)
edges = [
(, ), (, ), (, ),
(, ), (, ), (, ),
(, ), (, ), (, ),
(, ), (, ), (, ),
(, ), (, ), (, ),
(, ), (, ),
]
G.add_edges_from(edges)
()
()
()
()
degree_centrality = nx.degree_centrality(G)
()
node, score (degree_centrality.items(), key= x: x[], reverse=)[:]:
()
betweenness_centrality = nx.betweenness_centrality(G)
()
node, score (betweenness_centrality.items(), key= x: x[], reverse=)[:]:
()
closeness_centrality = nx.closeness_centrality(G)
()
node, score (closeness_centrality.items(), key= x: x[], reverse=)[:]:
()
:
eigenvector_centrality = nx.eigenvector_centrality(G, max_iter=)
()
node, score (eigenvector_centrality.items(), key= x: x[], reverse=)[:]:
()
:
()
networkx.algorithms community
communities = (community.greedy_modularity_communities(G))
()
()
i, comm (communities):
()
degrees = [G.degree(n) n G.nodes()]
()
()
()
()
()
()
fig, axes = plt.subplots(, , figsize=(, ))
pos = nx.spring_layout(G, k=, iterations=, seed=)
ax = axes[, ]
node_colors = [degree_centrality[node] node G.nodes()]
nx.draw_networkx_nodes(G, pos, node_color=node_colors, node_size=, cmap=, ax=ax)
nx.draw_networkx_edges(G, pos, alpha=, ax=ax)
nx.draw_networkx_labels(G, pos, font_size=, ax=ax)
ax.set_title()
ax.axis()
ax = axes[, ]
color_map = []
colors = plt.cm.Set3(np.linspace(, , (communities)))
node_to_color = {}
i, comm (communities):
node comm:
node_to_color[node] = colors[i]
color_map = [node_to_color[node] node G.nodes()]
nx.draw_networkx_nodes(G, pos, node_color=color_map, node_size=, ax=ax)
nx.draw_networkx_edges(G, pos, alpha=, ax=ax)
nx.draw_networkx_labels(G, pos, font_size=, ax=ax)
ax.set_title()
ax.axis()
ax = axes[, ]
centrality_df = pd.DataFrame({
: degree_centrality,
: betweenness_centrality,
: closeness_centrality,
}).head()
centrality_df.plot(kind=, ax=ax, width=)
ax.set_xlabel()
ax.set_title()
ax.legend(loc=)
ax.grid(, alpha=, axis=)
ax = axes[, ]
degree_sequence = ([d n, d G.degree()], reverse=)
degree_count = Counter(degree_sequence)
degrees_unique = (degree_count.keys())
counts = [degree_count[d] d degrees_unique]
ax.bar(degrees_unique, counts, color=, edgecolor=, alpha=)
ax.set_xlabel()
ax.set_ylabel()
ax.set_title()
ax.grid(, alpha=, axis=)
plt.tight_layout()
plt.show()
()
:
shortest_path = nx.shortest_path_length(G, , )
()
nx.NetworkXNoPath:
()
()
()
num_components = nx.number_connected_components(G)
()
():
neighbors1 = (G.neighbors(node1)) | {node1}
neighbors2 = (G.neighbors(node2)) | {node2}
intersection = (neighbors1 & neighbors2)
union = (neighbors1 | neighbors2)
intersection / union union >
()
()
()
influence_score = {}
node G.nodes():
score = (degree_centrality[node] * +
betweenness_centrality[node] * +
closeness_centrality[node] * )
influence_score[node] = score
()
node, score (influence_score.items(), key= x: x[], reverse=)[:]:
()
( + *)
()
(*)
()
()
()
()
(*)