Last active
January 29, 2017 15:07
-
-
Save vtraag/582176c436749dad95de to your computer and use it in GitHub Desktop.
Create sparse matrix from igraph in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_sparse_adjacency_matrix(G, attr=None): | |
if attr: | |
source, target, data = zip(*[(e.source, e.target, e[attr]) | |
for e in G.es if not np.isnan(e[attr])]); | |
else: | |
source, target = zip(*[(e.source, e.target) | |
for e in G.es]); | |
data = np.ones(len(source)).astype('int').tolist(); | |
if not G.is_directed(): | |
# If not directed, also create the other edge | |
source, target = source + target, target + source; | |
data = data + data; | |
L = sparse.coo_matrix((data, (source, target)), shape=[G.vcount(), G.vcount()]); | |
return L; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment