networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
to_scipy_sparse_array(G, nodelist=None, dtype=None, weight='weight', format='csr')

Notes

For directed graphs, matrix entry i,j corresponds to an edge from i to j.

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 :None:None:`nodelist` does not contain every node in G, the adjacency matrix is built from the subgraph of G that is induced by the nodes in :None:None:`nodelist`.

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:

>>> G = nx.Graph([(1, 1)])
>>> A = nx.to_scipy_sparse_array(G)
>>> print(A.todense())
[[1]]
>>> A.setdiag(A.diagonal() * 2)
>>> print(A.toarray())
[[2]]

Parameters

G : graph

The NetworkX graph used to construct the sparse matrix.

nodelist : list, optional

The rows and columns are ordered according to the nodes in :None:None:`nodelist`. If :None:None:`nodelist` 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 for details.

Returns

A : SciPy sparse array

Graph adjacency matrix.

Returns the graph adjacency matrix as a SciPy sparse array.

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_array(G, nodelist=[0, 1, 2])
... print(S.toarray()) [[0 2 0] [1 0 0] [0 0 4]]
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.linalg.graphmatrix.adjacency_matrix networkx.convert_matrix.to_scipy_sparse_array

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/convert_matrix.py#794
type: <class 'function'>
Commit: