首页 > 解决方案 > 在 Python 中将图形转换为标记矩阵

问题描述

我是 Python 绘图的新手,并通过添加边创建了加权有向图。通过不使用有向图,它不是定向的吗?当我尝试将 nx.Graph 更改为 nx.digraph 时,我收到一条错误消息

TypeError: 'module' object is not callable

我还想从图中制作两个矩阵,一个是邻接矩阵,另一个是基于彼此连接的节点(如竞争矩阵)。我能够制作两者,但不再标记节点。我手工完成了连接的节点。

我最终试图对网络进行集群,因此标签很重要。我很感激任何建议。谢谢。

G = nx.Graph()
    
G.add_edge("N66", "N1", weight=1)
G.add_edge("N19", "N2", weight=1)
G.add_edge("N66", "N4", weight=3)
G.add_edge("N5", "N126", weight=1)
G.add_edge("N5", "N95", weight=1)
G.add_edge("N6", "N126", weight=1)
G.add_edge("N7", "N26", weight=1)
G.add_edge("N61", "N7", weight=1)
G.add_edge("N66", "N7", weight=1)
G.add_edge("N8", "N54", weight=3)
G.add_edge("N8", "N88", weight=1)
G.add_edge("N8", "N97", weight=1)
G.add_edge("N8", "N75", weight=1)
G.add_edge("N19", "N8", weight=1)
G.add_edge("N23", "N8", weight=1)
G.add_edge("N66", "N8", weight=3)
G.add_edge("N40", "N9", weight=1)
G.add_edge("N10", "N69", weight=1)
G.add_edge("N10", "N95", weight=1)...

标签: pythondirected-graphweighted-graph

解决方案


您可能会发现在外部构建一个矩阵更容易,比如 csv 文件并以这种方式读取图形,例如

nx.from_pandas_edgelist(dataframe, source='source', target='target', edge_attr=['weight'])

推荐阅读