2016-07-06 40 views
0

在igraph中读取我的二分矩阵时遇到问题。我创建使用Networkx二分图和导出它们作为biadjacency矩阵:使用igraph使用Networkx创建的二分图

data <-readMat("graphToMatlab1.mat") 
    data1 <- data$graphToMatlab[[1]][[1]] 
    graph <- graph_from_adjacency_matrix(data1, mode="undirected") 

这里是稀疏矩阵:

bipartite.biadjacency_matrix(Graph[i],row_order=bipartite.sets(stockGraph[i])[0], column_order=bipartite.sets(stockGraph[i])[1],format='csr') 

然后我使用的igraph导入R中的10×10矩阵

data1 10 x 10稀疏矩阵类“dgCMatrix”

[1,] 1 . . . . . . . . . 
[2,] . . . . 1 . . . . . 
[3,] . . 1 . . 1 . . . . 
[4,] . . . . 1 . 1 1 . . 
[5,] . . . . . . . . . 1 
[6,] . . 1 1 . . 1 . 1 . 
[7,] . . 1 1 1 2 . . . . 
[8,] . . 1 . . 1 . . . . 
[9,] . . 1 1 . . . 1 . . 
[10,] . 2 . . . . . . . . 

IGRAPH U--- 10 21 -- 
+ edges: 
[1] 1-- 1 2-- 5 2--10 2--10 3-- 3 3-- 6 3-- 7 3-- 8 3-- 9 4-- 5 4-- 6 4-- 7 4-- 8 4-- 9 5-- 7 5--10 6-- 7 6-- 7 6-- 8 6-- 9 
[21] 8-- 9 

因此,这是错误的,因为它没有考虑到,有两种类型的顶点(缺少的属性部分)。所以我认为这是因为我输出图的方式(使用bipartite.biadjacency matrix),但是有没有办法绕过这个问题?要么igraph读取矩阵,要么我在Networkx中导出数据?

回答

1

你可能只是想你的二分图

In [1]: import networkx as nx 

In [2]: G = nx.complete_bipartite_graph(5,3) 

In [3]: nx.adjacency_matrix(G,nodelist=range(8)).todense() 
Out[3]: 
matrix([[0, 0, 0, 0, 0, 1, 1, 1], 
     [0, 0, 0, 0, 0, 1, 1, 1], 
     [0, 0, 0, 0, 0, 1, 1, 1], 
     [0, 0, 0, 0, 0, 1, 1, 1], 
     [0, 0, 0, 0, 0, 1, 1, 1], 
     [1, 1, 1, 1, 1, 0, 0, 0], 
     [1, 1, 1, 1, 1, 0, 0, 0], 
     [1, 1, 1, 1, 1, 0, 0, 0]], dtype=int64) 

的邻接矩阵表示的biadjacency格式只有邻接矩阵

In [4]: from networkx.algorithms.bipartite import biadjacency_matrix 

In [5]: biadjacency_matrix(G,range(5)).todense() 
Out[5]: 
matrix([[1, 1, 1], 
     [1, 1, 1], 
     [1, 1, 1], 
     [1, 1, 1], 
     [1, 1, 1]], dtype=int64) 
+0

感谢您的一个部分,它的工作!我在一个循环中导出矩阵,导致列类型丢失。我只做了'g = bipartite.biadjacency_matrix(G,row_order = bipartite.sets(stockGraph [i])[0], column_order = bipartite.sets(stockGraph [i])[1]).todense()' 所以我删除了格式='csr')从bipartite.adjacency矩阵),它的工作! – user3767071