闲社服务运行正常AI智能体自动化平台
g

图分析

技能标识:graph-analysis

Analyze graphs and networks using Python NetworkX — centrality, shortest paths, community detection, and visualization.

作者:admin | 来源记录:ClawHub
登记来源
ClawHub
版本
V 1.0.0
检测标记
后台标记通过
免费
免费获取
0
收藏
来源与检测标记为平台登记信息,并不代表已展示可复核的检测报告。使用前请核对版本、依赖和所需权限,建议先在隔离环境中运行。
概述
安装方式
版本历史

图分析

图分析技能

使用Python和NetworkX分析图与网络。支持从数据构建图、运行图算法以及生成可视化结果。

环境配置

首次使用前,请安装依赖项:

bash
pip install networkx matplotlib numpy --break-system-packages

功能说明

1. 构建图

通过边列表、邻接矩阵、CSV文件或JSON数据创建图。

从边列表(CSV或内联数据):
python
import networkx as nx

从包含列:source, target, weight(可选)的CSV文件读取

import csv G = nx.Graph() with open(edges.csv) as f: reader = csv.DictReader(f) for row in reader: weight = float(row.get(weight, 1.0)) G.add_edge(row[source], row[target], weight=weight)

从内联数据:
python
import networkx as nx
G = nx.Graph()
G.addedgesfrom([(A, B), (B, C), (A, C), (C, D)])

有向图:
python
G = nx.DiGraph()
G.addedgesfrom([(A, B), (B, C)])

2. 图度量指标

运行以下代码以回答关于图结构的问题:

python
import networkx as nx

基本统计信息

print(f节点数:{G.numberofnodes()}) print(f边数:{G.numberofedges()}) print(f密度:{nx.density(G):.4f}) print(f连通性:{nx.is_connected(G)})

中心性度量

degreecent = nx.degreecentrality(G) betweenness = nx.betweenness_centrality(G) closeness = nx.closeness_centrality(G) pagerank = nx.pagerank(G)

查找最重要的节点

topbydegree = sorted(degree_cent.items(), key=lambda x: x[1], reverse=True)[:10] topbybetweenness = sorted(betweenness.items(), key=lambda x: x[1], reverse=True)[:10] topbypagerank = sorted(pagerank.items(), key=lambda x: x[1], reverse=True)[:10]

print(按度中心性排名前10的节点:, topbydegree)
print(按介数中心性排名前10的节点:, topbybetweenness)
print(按PageRank排名前10的节点:, topbypagerank)

3. 最短路径

python

两个节点之间的最短路径


path = nx.shortest_path(G, source=A, target=D)
length = nx.shortestpathlength(G, source=A, target=D)
print(f路径:{ -> .join(path)}(长度:{length}))

加权最短路径

pathw = nx.shortestpath(G, source=A, target=D, weight=weight)

所有节点对的最短路径长度

alllengths = dict(nx.allpairsshortestpath_length(G))

4. 社区检测

python
from networkx.algorithms.community import greedymodularitycommunities, louvain_communities

贪心模块度

communitiesgreedy = list(greedymodularity_communities(G)) print(f发现 {len(communities_greedy)} 个社区(贪心算法))

Louvain算法(适用于大型图)

communitieslouvain = list(louvaincommunities(G)) print(f发现 {len(communities_louvain)} 个社区(Louvain算法))

打印社区成员

for i, comm in enumerate(communities_louvain): print(f 社区 {i}:{sorted(comm)})

5. 可视化

将图的可视化结果保存为PNG图片:

python
import matplotlib
matplotlib.use(Agg)
import matplotlib.pyplot as plt
import networkx as nx

fig, ax = plt.subplots(figsize=(12, 8))

布局选项:springlayout, circularlayout, kamadakawailayout, shell_layout

pos = nx.spring_layout(G, seed=42)

根据度数设置节点大小

node_sizes = [300 * G.degree(n) for n in G.nodes()]

根据社区设置节点颜色(如果已计算社区)

nx.draw( G, pos, ax=ax, with_labels=True, nodesize=nodesizes, node_color=steelblue, edge_color=#cccccc, font_size=8, font_weight=bold, width=0.8, ) ax.set_title(图可视化) plt.tight_layout() plt.savefig(graph.png, dpi=150) plt.close() print(已保存 graph.png)

按社区着色:
python
import matplotlib
matplotlib.use(Agg)
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms.community import louvain_communities

communities = list(louvain_communities(G))
color_map = {}
for i, comm in enumerate(communities):
for node in comm:
color_map[node] = i

nodecolors = [colormap[n] for n in G.nodes()]
pos = nx.spring_layout(G, seed=42)

fig, ax = plt.subplots(figsize=(12, 8))
nx.draw(
G, pos, ax=ax,
with_labels=True,
nodecolor=nodecolors,
cmap=plt.cm.Set3,
node_size=500,
edge_color=#cccccc,
font_size=8,
)
ax.set_title(社区分布)
plt.tight_layout()
plt.savefig(communities.png, dpi=150)
plt.close()
print(已保存 communities.png)

6. 图生成器(用于测试或模拟)

python
import networkx as nx

常用测试图

G = nx.karateclubgraph() # 经典社交网络(34个节点) G = nx.barabasialbertgraph(100, 3) # 无标度网络 G = nx.erdosrenyigraph(50, 0.1) # 随机图 G = nx.wattsstrogatzgraph(30, 4, 0.3) # 小世界网络

使用场景

当用户提出以下需求时使用此技能:

  • - 分析数据中的关系、连接或网络结构
  • 查找网络中重要/核心/有影响力的节点
  • 检测图中的社区或聚类
  • 查找实体之间的最短路径
  • 可视化网络或关系图
  • 从CSV、JSON或其他结构化数据构建图
  • 运行图算法(PageRank、中心性、聚类系数等)

注意事项

  • - 对于大型图(超过10,000个节点),优先使用louvaincommunities而非greedymodularity_communities
  • 在导入pyplot之前始终使用matplotlib.use(Agg)(无显示服务器环境)
  • 将可视化结果保存为PNG文件并展示给用户
  • 读取用户数据时,根据上下文推断源/目标列——常见模式包括:from/to、source/target、node1/node2、parent/child

标签

skill ai

通过对话安装

以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 graph-analysis-1776075375 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 graph-analysis-1776075375 技能

通过命令行安装

skillhub install graph-analysis-1776075375

下载

⬇ 下载 graph-analysis v1.0.0(免费)

文件大小: 2.93 KB | 发布时间: 2026-4-14 12:14

v1.0.0 最新 2026-4-14 12:14
Initial release of the graph-analysis skill.

- Analyze graphs and networks using Python NetworkX: centrality, shortest paths, community detection, and visualization.
- Supports building graphs from edge lists, CSV/JSON data, and test graph generators.
- Provides metrics (degree, betweenness, closeness, PageRank), pathfinding, and community detection.
- Enables saving network visualizations as PNG images, including community coloring.
- Includes setup and usage instructions for interactive data analysis.
返回顶部