networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersBackRef
from_scipy_sparse_matrix(A, parallel_edges=False, create_using=None, edge_attribute='weight')

Notes

For directed graphs, explicitly mention create_using=nx.DiGraph, and entry i,j of A corresponds to an edge from i to j.

If :None:None:`create_using` is networkx.MultiGraph or networkx.MultiDiGraph , :None:None:`parallel_edges` is True, and the entries of A are of type int , then this function returns a multigraph (constructed from :None:None:`create_using`) with parallel edges. In this case, :None:None:`edge_attribute` will be ignored.

If :None:None:`create_using` indicates an undirected multigraph, then only the edges indicated by the upper triangle of the matrix A will be added to the graph.

Parameters

A: scipy sparse matrix :

An adjacency matrix representation of a graph

parallel_edges : Boolean

If this is True, :None:None:`create_using` is a multigraph, and A is an integer matrix, then entry (i, j) in the matrix is interpreted as the number of parallel edges joining vertices i and j in the graph. If it is False, then the entries in the matrix are interpreted as the weight of a single edge joining the vertices.

create_using : NetworkX graph constructor, optional (default=nx.Graph)

Graph type to create. If graph instance, then cleared before populated.

edge_attribute: string :

Name of edge attribute to store matrix numeric value. The data will have the same type as the matrix entry (int, float, (real,imag)).

Creates a new graph from an adjacency matrix given as a SciPy sparse matrix.

Examples

>>> import scipy as sp
... import scipy.sparse # call as sp.sparse
... A = sp.sparse.eye(2, 2, 1)
... G = nx.from_scipy_sparse_matrix(A)

If :None:None:`create_using` indicates a multigraph and the matrix has only integer entries and :None:None:`parallel_edges` is False, then the entries will be treated as weights for edges joining the nodes (without creating parallel edges):

>>> A = sp.sparse.csr_matrix([[1, 1], [1, 2]])
... G = nx.from_scipy_sparse_matrix(A, create_using=nx.MultiGraph)
... G[1][1] AtlasView({0: {'weight': 2}})

If :None:None:`create_using` indicates a multigraph and the matrix has only integer entries and :None:None:`parallel_edges` is True, then the entries will be treated as the number of parallel edges joining those two vertices:

>>> A = sp.sparse.csr_matrix([[1, 1], [1, 2]])
... G = nx.from_scipy_sparse_matrix(
...  A, parallel_edges=True, create_using=nx.MultiGraph
... )
... G[1][1] AtlasView({0: {'weight': 1}, 1: {'weight': 1}})
See :

Back References

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

networkx.convert_matrix.from_scipy_sparse_matrix

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#1025
type: <class 'function'>
Commit: