2016-07-06 15 views
0

Ich habe Probleme beim Lesen meiner zweiteiligen Matrix in Iigraph. Ich habe bipartite Graphen mit NetworkX und exportiert sie als biadjacency Matrix:plot bipartite Graph erstellt mit Networkx mit igrap

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

Dann importiere ich die 10x10-Matrix in R IGRAPH mit:

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

Hier ist die Sparse Matrix:

data1 10 x 10 dünne Matrix der Klasse "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 . . . . . . . . 

Graph

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 

Also das ist falsch, weil sie berücksichtigen nicht, dass es zwei Arten von Eckpunkten (Abschnitt Attribute fehlen). Ich denke, dies liegt an der Art und Weise, wie ich den Graphen exportiert habe (mit der bipartite.biadjacency Matrix), aber gibt es eine Möglichkeit, dieses Problem zu umgehen? Entweder in der Art wie igrigraph Matrizen liest oder wie ich in Networkx exportiere?

Antwort

1

Sie wollen wahrscheinlich nur die Adjazenzmatrix Darstellung Ihrer zweiteiligen Graphen

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) 

Das biadjacency Format ein Abschnitt der Adjazenzmatrix

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

Danke, es hat funktioniert nur hat! Ich habe die Matrix in eine Schleife exportiert, was zum Verlust des Säulentyps geführt hat. Ich habe nur 'g = bipartite.biadjacency_matrix (G, row_order = bipartite.sets (stockGraph [i]) [0], column_order = bipartite.sets (stockGraph [i]) [1]). Todense()' (also habe ich format = 'csr' aus bipartite.adjacency matrix entfernt) und es hat funktioniert! – user3767071