nx.bipartite.biadacency_矩阵()跟踪节点

2024-06-25 07:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python3.6和networkx2.1。我从加权边列表中导入一个二部图,然后调用双邻接矩阵函数并将结果转换为熊猫数据帧,对其执行一些操作,例如计算一些相关系数(执行这些计算的函数将邻接矩阵作为输入,因此我需要改造我的边缘派)

import pandas as pd
from networkx import bipartite
A=bipartite.biadjacency_matrix(G, nodes1, nodes2).todense()
A=pd.DataFrame(A)

事实上,当我调用bipartite.biadjacency_matrix()时,我丢失了节点的名称,结果数据帧类似于:

^{pr2}$

所以在计算了相关系数之后,我可以根据我得到的相似矩阵来构建一个网络,但是我不知道哪个是谁,谁是谁。关于如何恢复这些名字有什么建议吗?在


Tags: 数据函数fromimportnetworkxpandas列表as
1条回答
网友
1楼 · 发布于 2024-06-25 07:16:00

来自bipartite.biadjacency_matrix的文档:

row_order : list of nodes The rows of the matrix are ordered according to the list of nodes.

column_order : list, optional The columns of the matrix are ordered according to the list of nodes. If column_order is None, then the ordering of columns is arbitrary.

pd.DataFrame

index : Index or array-like Index to use for resulting frame. Will default to np.arange(n) if no indexing information part of input data and no index provided

columns : Index or array-like Column labels to use for resulting frame. Will default to np.arange(n) if no column labels are provided

您推断出矩阵A行使用nodes1排序,列使用nodes2排序。要在数据帧中获得相同的顺序,请执行以下操作:

A = pd.DataFrame(A, index=nodes1, columns=nodes2)

相关问题 更多 >