networkx.convert_matrix.to_scipy_sparse_matrix¶
-
to_scipy_sparse_matrix
(G, nodelist=None, dtype=None, weight='weight', format='csr')[source]¶ Returns the graph adjacency matrix as a SciPy sparse matrix.
Parameters: - G (graph) – The NetworkX graph used to construct the NumPy matrix.
- nodelist (list, optional) – The rows and columns are ordered according to the nodes in
nodelist
. Ifnodelist
is None, then the ordering is produced by G.nodes(). - dtype (NumPy data-type, optional) – A valid NumPy dtype used to initialize the array. If None, then the NumPy default is used.
- weight (string or None optional (default=’weight’)) – The edge attribute that holds the numerical value used for the edge weight. If None then all edge weights are 1.
- format (str in {‘bsr’, ‘csr’, ‘csc’, ‘coo’, ‘lil’, ‘dia’, ‘dok’}) – The type of the matrix to be returned (default ‘csr’). For some algorithms different implementations of sparse matrices can perform better. See [1] for details.
Returns: M – Graph adjacency matrix.
Return type: SciPy sparse matrix
Notes
The matrix entries are populated using the edge attribute held in parameter weight. When an edge does not have that attribute, the value of the entry is 1.
For multiple edges the matrix values are the sums of the edge weights.
When
nodelist
does not contain every node inG
, the matrix is built from the subgraph ofG
that is induced by the nodes innodelist
.Uses coo_matrix format. To convert to other formats specify the format= keyword.
The convention used for self-loop edges in graphs is to assign the diagonal matrix entry value to the weight attribute of the edge (or the number 1 if the edge has no weight attribute). If the alternate convention of doubling the edge weight is desired the resulting Scipy sparse matrix can be modified as follows:
>>> import scipy as sp >>> G = nx.Graph([(1, 1)]) >>> A = nx.to_scipy_sparse_matrix(G) >>> print(A.todense()) [[1]] >>> A.setdiag(A.diagonal() * 2) >>> print(A.todense()) [[2]]
Examples
>>> G = nx.MultiDiGraph() >>> G.add_edge(0, 1, weight=2) 0 >>> G.add_edge(1, 0) 0 >>> G.add_edge(2, 2, weight=3) 0 >>> G.add_edge(2, 2) 1 >>> S = nx.to_scipy_sparse_matrix(G, nodelist=[0, 1, 2]) >>> print(S.todense()) [[0 2 0] [1 0 0] [0 0 4]]
References
[1] Scipy Dev. References, “Sparse Matrices”, https://docs.scipy.org/doc/scipy/reference/sparse.html