SciPy

scipy.sparse.csgraph.csgraph_from_masked

scipy.sparse.csgraph.csgraph_from_masked(graph)

Construct a CSR-format graph from a masked array.

New in version 0.11.0.

Parameters:
graph : MaskedArray

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

Returns:
csgraph : csr_matrix

Compressed sparse representation of graph,

Examples

>>> import numpy as np
>>> from scipy.sparse.csgraph import csgraph_from_masked
>>> graph_masked = np.ma.masked_array(data =[
... [0, 1 , 2, 0],
... [0, 0, 0, 1],
... [0, 0, 0, 3],
... [0, 0, 0, 0]
...  ],
... mask=[[ True, False, False , True],
... [ True,  True , True, False],
... [ True , True,  True ,False],
... [ True ,True , True , True]],
... fill_value = 0)
>>> csgraph_from_masked(graph_masked)
<4x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

Previous topic

scipy.sparse.csgraph.csgraph_from_dense

Next topic

scipy.sparse.csgraph.csgraph_masked_from_dense