# bipartite graphs Classic List Threaded 4 messages Open this post in threaded view
|

## bipartite graphs

 Hello, I would like to test nSBM on bipartite graphs but before going on I need to be sure I'm able to build a bipartite graph in graph-tool starting from a matrix: A_nodes = np.arange(data.shape) #nodes for rows start from 0 B_nodes = np.arange(data.shape) + data.shape # nodes from columns start from the last A_node g = gt.Graph(directed=True) # directed or not directed... maybe not important at all g.add_vertex(len(A_nodes) + len(B_nodes)) #add all needed nodes partition = g.new_vertex_property('bool') # create a property indicating the node type for x in A_nodes:     partition[g.vertex(x)] = 0 # set all A nodes to 0 for x in B_nodes:     partition[g.vertex(x)] = 1 # set all B to 1 idx = np.nonzero(data) # take the edge values weights = adata.X[idx] idx = (idx,  idx + len(A_nodes)) # node number of columns need to be augmented by the offset g.add_edge_list(np.transpose(idx)) #add weights ew = g.new_edge_property("double") ew.a = weights g.ep['weight'] = ew Is there a more straightforward way to go? d _______________________________________________ graph-tool mailing list [hidden email] https://lists.skewed.de/mailman/listinfo/graph-tool
Open this post in threaded view
|

## Re: bipartite graphs

 Administrator Am 20.03.20 um 11:22 schrieb Davide Cittaro: > Hello, > I would like to test nSBM on bipartite graphs but before going on I need to be sure I'm able to build a bipartite graph in graph-tool starting from a matrix: > > A_nodes = np.arange(data.shape) #nodes for rows start from 0 > B_nodes = np.arange(data.shape) + data.shape # nodes from columns start from the last A_node > > g = gt.Graph(directed=True) # directed or not directed... maybe not important at all > g.add_vertex(len(A_nodes) + len(B_nodes)) #add all needed nodes > partition = g.new_vertex_property('bool') # create a property indicating the node type > > for x in A_nodes: >     partition[g.vertex(x)] = 0 # set all A nodes to 0 > > for x in B_nodes: >     partition[g.vertex(x)] = 1 # set all B to 1 > > idx = np.nonzero(data) # take the edge values > weights = adata.X[idx] > > idx = (idx,  idx + len(A_nodes)) # node number of columns need to be augmented by the offset > > g.add_edge_list(np.transpose(idx)) #add weights > > ew = g.new_edge_property("double") > ew.a = weights > g.ep['weight'] = ew > > Is there a more straightforward way to go? You can add the weights together with the edges in Graph.add_edge_list() via the eprops parameter, but otherwise the above is fine. Note that if your objective is to do SBM inference, it's better to make the graph undirected. Best, Tiago -- Tiago de Paula Peixoto <[hidden email]> _______________________________________________ graph-tool mailing list [hidden email] https://lists.skewed.de/mailman/listinfo/graph-tool signature.asc (849 bytes) Download Attachment -- Tiago de Paula Peixoto
 Administrator (Please answer to the mailing list, not directly Am 23.03.20 um 15:31 schrieb Davide Cittaro: >> Note that if your objective is to do SBM inference, it's better to make >> the graph undirected. >> > > Why so? Is this true in general? Well, if you have a bipartite network where it is important do distinct the direction of the edges, then you should use a directed model. Otherwise the model should be undirected. I assumed from your formulation that in your scenario the direction is unimportant. -- Tiago de Paula Peixoto <[hidden email]> _______________________________________________ graph-tool mailing list [hidden email] https://lists.skewed.de/mailman/listinfo/graph-tool signature.asc (849 bytes) Download Attachment -- Tiago de Paula Peixoto