使用Networkx编写一个加载多重双邻接矩阵

2024-09-26 18:09:35 发布

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

我在python中将我的二部图作为稀疏矩阵来读写有困难。我想读回python中的mat文件,但是我无法返回到图形,因为mat文件给出了努比·恩达雷我需要一个稀疏矩阵来重建我的图形。你知道如何让我的代码工作吗?我认为这与loadmat的“对象”规范有关,但我不知道如何进一步。。。在

import setuptools
import math
from networkx.utils import powerlaw_sequence
from networkx import *
import networkx as nx
from scipy import sparse, io # 
G= nx.Graph()
stockGraph = []
G =bipartite.configuration_model([1, 1], [1, 1], create_using=None, seed=None)
stockGraph.append(G) # stock new graph
G =bipartite.configuration_model([0, 1], [1, 0], create_using=None, seed=None)
stockGraph.append(G) # stock new graph 

graphToMatlab=[]  #then export all my graphs as biadjacency matrix
n=2
for i in range(n):
       myGraphToMatlab=bipartite.biadjacency_matrix(stockGraph[i], row_order=bipartite.sets(stockGraph[i])[0], column_order=bipartite.sets(stockGraph[i])[1], weight='weight', format='csr')  
       graphToMatlab.append(myGraphToMatlab) # stacks adjacency matrix.
io.savemat('manygraphs.mat', dict(graphToMatlab=graphToMatlab)) # save all matrices

mat_contents=io.loadmat('manygraphs.mat', mdict=None, appendmat=True) # then open biadjacency matrices : where the problem lies !!
mat1 = mat_contents['graphToMatlab']
m1=mat1[0,0] # remove the object part WITH THIS LINE, IT WORKS !!
bipartite.from_biadjacency_matrix(mat1[1], create_using=None, edge_attribute='weight')# recreate the first graph

我明白了:

^{pr2}$

Tags: fromioimportnetworkxnonecreatematrixgraph

热门问题