scipy.sparse.csgraph.

csgraph_from_dense#

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

從密集矩陣建構 CSR 格式的稀疏圖。

在 0.11.0 版本中新增。

參數:
grapharray_like

輸入圖。形狀應為 (n_nodes, n_nodes)。

null_valuefloat 或 None (選用)

表示圖中非邊緣的值。預設值為零。

infinity_nullbool

若為 True (預設),則無限大的條目 (正負皆可) 會被視為空邊緣。

nan_nullbool

若為 True (預設),則 NaN 條目會被視為非邊緣

回傳值:
csgraphcsr_array

圖的壓縮稀疏表示法,

範例

>>> 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)
<Compressed Sparse Row sparse array of dtype 'float64'
    with 4 stored elements and shape (4, 4)>