4

I have a fairly large sparse matrix. The sparse matrix have elements in the below format. I want to create a graph network using a networkx library. Now, how should I approach?

Row and column are nodes and they are connected if the value of the matrix element is 1.

In [44]: print(a)

(0, 0)    1
(1, 2)    1
(1, 3)    1
(2, 3)    1
2
  • Is your matrix stored as a scipy sparse matrix? If so, there's a command for that: link Commented Apr 24, 2019 at 7:12
  • Yes. it is scipy.sparse.csc.csc_matrix Commented Apr 24, 2019 at 7:17

1 Answer 1

5

Take a look at

from_scipy_sparse_matrix

The call looks like G=nx.from_scipy_sparse_matrix(A, parallel_edges=False, create_using=None, edge_attribute='weight')

A is the sparse matrix.

If parallel_edges=False, then the entry is considered an edge weight

create_using says what kind of graph it is. It defaults to nx.Graph.

If create_using is MultiGraph of MultiDiGraph, and parallel_edges=True, and all edges are entries, then a 2 would mean 2 edges.

Otherwise the entries are treated as edge attributes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.