SciPy

scipy.sparse.csgraph.csgraph_from_dense

scipy.sparse.csgraph.csgraph_from_dense(graph, null_value=0, nan_null=True, infinity_null=True)

Construct a CSR-format sparse graph from a dense matrix.

New in version 0.11.0.

Parameters
grapharray_like

Input graph. Shape should be (n_nodes, n_nodes).

null_valuefloat or None (optional)

Value that denotes non-edges in the graph. Default is zero.

infinity_nullbool

If True (default), then infinite entries (both positive and negative) are treated as null edges.

nan_nullbool

If True (default), then NaN entries are treated as non-edges

Returns
csgraphcsr_matrix

Compressed sparse representation of graph,

Examples

>>> from scipy.sparse.csgraph import csgraph_from_dense
>>> graph = [
... [0, 1 , 2, 0],
... [0, 0, 0, 1],
... [0, 0, 0, 3],
... [0, 0, 0, 0]
... ]
>>> csgraph_from_dense(graph)
<4x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

Previous topic

scipy.sparse.csgraph.construct_dist_matrix

Next topic

scipy.sparse.csgraph.csgraph_from_masked