跳到主要内容Python 知识图谱分析:NetworkX 库使用指南 | 极客日志PythonAI算法
Python 知识图谱分析:NetworkX 库使用指南
本文介绍 Python 图分析库 NetworkX。涵盖安装、核心概念(节点、边、图类型)、基础操作(增删查改)、常用算法(度分析、路径、连通性、中心性)及可视化方法。通过社交网络案例演示综合应用,适合中小规模图结构分析。
NetworkX 库介绍与使用指南
NetworkX 是 Python 中用于**创建、操作和分析复杂网络(图结构)**的核心库,支持无向图、有向图、加权图、多重图等多种图类型,内置丰富的图算法(路径分析、连通性、中心性、社区检测等),广泛应用于社交网络分析、网络拓扑研究、路径规划等场景。
一、安装
使用 pip 安装最新版本:
pip install networkx
pip install networkx[all]
验证安装:当前常用版本为 3.x
networkx nx
(nx.__version__)
import
as
print
二、核心概念
| 概念 | 说明 |
|---|
| 节点(Node) | 图的基本单元,可绑定任意可哈希对象(整数、字符串、元组、自定义对象等) |
| 边(Edge) | 连接节点的关系,可绑定属性(如权重、标签、时间戳) |
| 图类型 | - Graph:无向图(默认),无自环/多重边 - DiGraph:有向图 - MultiGraph:无向多重图(允许多条边连同一对节点) - MultiDiGraph:有向多重图 |
三、基础操作:创建与编辑图
1. 创建图
G = nx.Graph()
DG = nx.DiGraph()
MG = nx.MultiGraph()
MDG = nx.MultiDiGraph()
2. 添加节点
- 单个节点:
add_node()
- 多个节点:
add_nodes_from()
- 带属性的节点(如节点名称、权重):
G.add_node(1)
G.add_node("A", weight=0.8, label="核心节点")
G.add_nodes_from([2, 3, 4])
G.add_nodes_from([(5, {"weight": 0.5, "label": "普通节点"}), (6, {"weight": 0.3})])
G2 = nx.Graph()
G2.add_nodes_from(G.nodes)
3. 添加边
- 单个边:
add_edge()
- 多个边:
add_edges_from()
- 带权重/属性的边:
G.add_edge(1, 2)
G.add_edge(2, 3, weight=5.0, label="强连接")
G.add_edges_from([(3, 4), (4, 5, {"weight": 2.0}), (5, 6, {"weight": 1.0, "label": "弱连接"})])
MG.add_edge(1, 2, key=0, weight=1.0)
MG.add_edge(1, 2, key=1, weight=2.0)
4. 删除节点/边
G.remove_node(6)
G.remove_nodes_from([4, 5])
G.remove_edge(1, 2)
G.remove_edges_from([(2, 3), (3, 4)])
G.clear()
5. 查询图信息
print("所有节点:", list(G.nodes))
print("节点数量:", G.number_of_nodes())
print("节点 1 的属性:", G.nodes[1])
print("所有边:", list(G.edges))
print("边数量:", G.number_of_edges())
print("边 (2,3) 的属性:", G.edges[2, 3])
print("节点 2 的邻接节点:", list(G.neighbors(2)))
n_nodes = G.number_of_nodes()
n_edges = G.number_of_edges()
avg_degree = 2 * n_edges / n_nodes
print(f"图的摘要:节点数={n_nodes}, 边数={n_edges}, 平均度={avg_degree:.2f}")
四、图分析:常用算法与指标
NetworkX 内置大量图分析算法,以下是高频使用场景:
1. 度(Degree)分析
度表示节点的边数,有向图区分入度(in_degree) 和 出度(out_degree):
print("节点 2 的度:", G.degree[2])
print("节点 2 的加权度:", G.degree(2, weight="weight"))
DG.add_edges_from([(1, 2), (2, 3), (3, 2)])
print("节点 2 的入度:", DG.in_degree[2])
print("节点 2 的出度:", DG.out_degree[2])
2. 路径分析
G = nx.Graph()
G.add_edges_from([(1, 2, {"weight": 1}), (2, 3, {"weight": 5}), (1, 3, {"weight": 10}), (3, 4, {"weight": 2})])
shortest_path = nx.shortest_path(G, source=1, target=4)
print("无权重最短路径:", shortest_path)
weighted_shortest_path = nx.shortest_path(G, source=1, target=4, weight="weight")
print("加权最短路径:", weighted_shortest_path)
path_length = nx.shortest_path_length(G, source=1, target=4, weight="weight")
print("加权路径长度:", path_length)
3. 连通性分析
- 无向图:连通分量(子图内任意节点可达)
- 有向图:强连通分量(双向可达)/弱连通分量(忽略方向后可达)
G3 = nx.Graph()
G3.add_edges_from([(1, 2), (3, 4), (4, 5)])
components = list(nx.connected_components(G3))
print("连通分量:", components)
DG3 = nx.DiGraph()
DG3.add_edges_from([(1, 2), (2, 1), (2, 3), (3, 4)])
strong_components = list(nx.strongly_connected_components(DG3))
print("强连通分量:", strong_components)
4. 节点中心性(衡量节点重要性)
| 中心性类型 | 说明 | 函数 |
|---|
| 度中心性 | 节点度占总节点数的比例 | nx.degree_centrality() |
| 介数中心性 | 节点出现在最短路径的比例 | nx.betweenness_centrality() |
| 接近中心性 | 节点到其他节点的平均距离 | nx.closeness_centrality() |
degree_centrality = nx.degree_centrality(G)
print("度中心性:", degree_centrality)
betweenness = nx.betweenness_centrality(G, weight="weight")
print("介数中心性:", betweenness)
五、图的可视化
NetworkX 结合 Matplotlib 实现可视化,支持自定义节点/边样式:
基础可视化
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([(1, 2, {"weight": 1}), (2, 3, {"weight": 5}), (1, 3, {"weight": 10}), (3, 4, {"weight": 2})])
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size=800, node_color="lightblue")
edge_weights = [G[u][v]["weight"] for u, v in G.edges]
nx.draw_networkx_edges(G, pos, width=[w * 0.5 for w in edge_weights], edge_color="gray")
nx.draw_networkx_labels(G, pos, font_size=12)
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): G[u][v]["weight"] for u, v in G.edges})
plt.axis("off")
plt.title("加权无向图可视化")
plt.show()
常用布局
| 布局函数 | 适用场景 |
|---|
spring_layout() | 通用力导向布局(最常用) |
circular_layout() | 节点环形排列 |
random_layout() | 节点随机排列 |
kamada_kawai_layout() | 优化节点间距离,更美观 |
shell_layout() | 节点分层排列(如社交网络层级) |
六、综合示例:社交网络分析
social_graph = nx.Graph()
social_graph.add_nodes_from([
("Alice", {"age": 25, "gender": "F"}),
("Bob", {"age": 30, "gender": "M"}),
("Charlie", {"age": 28, "gender": "M"}),
("Diana", {"age": 26, "gender": "F"}),
("Eve", {"age": 27, "gender": "F"})
])
social_graph.add_edges_from([
("Alice", "Bob", {"intimacy": 9}),
("Alice", "Charlie", {"intimacy": 6}),
("Bob", "Diana", {"intimacy": 8}),
("Charlie", "Diana", {"intimacy": 5}),
("Diana", "Eve", {"intimacy": 7}),
("Alice", "Eve", {"intimacy": 4})
])
print("=== 好友数 ===")
for user in social_graph.nodes:
print(f"{user}: {social_graph.degree[user]} 个好友")
print("\n=== 亲密度中心性 ===")
intimacy_centrality = nx.degree_centrality(social_graph, weight="intimacy")
for user, score in intimacy_centrality.items():
print(f"{user}: {score:.2f}")
print("\n=== Alice 到 Eve 的最短路径 ===")
path = nx.shortest_path(social_graph, source="Alice", target="Eve", weight="intimacy")
path_length = nx.shortest_path_length(social_graph, source="Alice", target="Eve", weight="intimacy")
print(f"路径:{' → '.join(path)},总亲密度:{path_length}")
pos = nx.kamada_kawai_layout(social_graph)
nx.draw_networkx_nodes(social_graph, pos, node_size=1000, node_color=["pink" if social_graph.nodes[n]["gender"] == "F" else "lightblue" for n in social_graph.nodes])
nx.draw_networkx_edges(social_graph, pos, width=[e["intimacy"] * 0.3 for u, v, e in social_graph.edges(data=True)])
nx.draw_networkx_labels(social_graph, pos, font_size=12)
nx.draw_networkx_edge_labels(social_graph, pos, edge_labels={(u, v): e["intimacy"] for u, v, e in social_graph.edges(data=True)})
plt.axis("off")
plt.title("社交网络亲密度分析")
plt.show()
七、进阶扩展
- 与其他库结合:
- 高性能计算:结合
igraph/graph-tool(处理大规模图);
- 可视化增强:结合
Pyvis(交互式可视化)、Plotly(动态图)。
社区检测:使用 nx.community 模块(如 Louvain 算法):
from networkx.algorithms import community
communities = community.louvain_communities(G, weight="weight")
读取/保存图:支持 GraphML、GEXF、Pajek 等格式:
nx.write_graphml(G, "my_graph.graphml")
G_loaded = nx.read_graphml("my_graph.graphml")
总结
NetworkX 是 Python 图分析的入门与核心工具,优势在于易用性强、API 直观、内置算法丰富,适合中小规模图(百万节点以内)的分析;若需处理超大规模图,可结合 Dask 分布式计算或专用图数据库(如 Neo4j)。
微信扫一扫,关注极客日志
微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具
- 加密/解密文本
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
- RSA密钥对生成器
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
- Mermaid 预览与可视化编辑
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
- curl 转代码
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online