bundles / scipy latest / scipy / sparse / csgraph / _laplacian / laplacian
function
scipy.sparse.csgraph._laplacian:laplacian
Signature
def laplacian ( csgraph , normed = False , return_diag = False , use_out_degree = False , * , copy = True , form = array , dtype = None , symmetrized = False ) Summary
Return the Laplacian of a directed graph.
Parameters
csgraph: array_like or sparse array or matrix, 2 dimensionscompressed-sparse graph, with shape (N, N).
normed: bool, optionalIf True, then compute symmetrically normalized Laplacian. Default: False.
return_diag: bool, optionalIf True, then also return an array related to vertex degrees. Default: False.
use_out_degree: bool, optionalIf True, then use out-degree instead of in-degree. This distinction matters only if the graph is asymmetric. Default: False.
copy: bool, optionalIf False, then change
csgraphin place if possible, avoiding doubling the memory use. Default: True, for backward compatibility.form: 'array', or 'function', or 'lo'Determines the format of the output Laplacian:
'array' is a numpy array;
'function' is a pointer to evaluating the Laplacian-vector or Laplacian-matrix product;
'lo' results in the format of the LinearOperator.
Choosing 'function' or 'lo' always avoids doubling the memory use, ignoring
copyvalue. Default: 'array', for backward compatibility.dtype: None or one of numeric numpy dtypes, optionalThe dtype of the output. If
dtype=None, the dtype of the output matches the dtype of the input csgraph, except for the casenormed=Trueand integer-like csgraph, where the output dtype is 'float' allowing accurate normalization, but dramatically increasing the memory use. Default: None, for backward compatibility.symmetrized: bool, optionalIf True, then the output Laplacian is symmetric/Hermitian. The symmetrization is done by
csgraph + csgraph.T.conjwithout dividing by 2 to preserve integer dtypes if possible prior to the construction of the Laplacian. The symmetrization will increase the memory footprint of sparse matrices unless the sparsity pattern is symmetric orformis 'function' or 'lo'. Default: False, for backward compatibility.
Returns
lap: ndarray, or sparse array or matrix, or `LinearOperator`The N x N Laplacian of csgraph. It will be a NumPy array (dense) if the input was dense, or a sparse array otherwise, or the format of a function or LinearOperator if
formequals 'function' or 'lo', respectively.diag: ndarray, optionalThe length-N main diagonal of the Laplacian matrix. For the normalized Laplacian, this is the array of square roots of vertex degrees or 1 if the degree is zero.
Notes
The Laplacian matrix of a graph is sometimes referred to as the "Kirchhoff matrix" or just the "Laplacian", and is useful in many parts of spectral graph theory. In particular, the eigen-decomposition of the Laplacian can give insight into many properties of the graph, e.g., is commonly used for spectral data embedding and clustering.
The constructed Laplacian doubles the memory use if copy=True and form="array" which is the default. Choosing copy=False has no effect unless form="array" or the matrix is sparse in the coo format, or dense array, except for the integer input with normed=True that forces the float output.
Sparse input is reformatted into coo if form="array", which is the default.
If the input adjacency matrix is not symmetric, the Laplacian is also non-symmetric unless symmetrized=True is used.
Diagonal entries of the input adjacency matrix are ignored and replaced with zeros for the purpose of normalization where normed=True. The normalization uses the inverse square roots of row-sums of the input adjacency matrix, and thus may fail if the row-sums contain negative or complex with a non-zero imaginary part values.
The normalization is symmetric, making the normalized Laplacian also symmetric if the input csgraph was symmetric.
Examples
import numpy as np from scipy.sparse import csgraph✓
G = np.arange(4) * np.arange(4)[:, np.newaxis] G✓
csgraph.laplacian(G)
✓G = np.arange(9).reshape(3, 3) G✓
L_in_degree = csgraph.laplacian(G) L_in_degree✓
L_out_degree = csgraph.laplacian(G, use_out_degree=True) L_out_degree✓
L_in_degree + L_out_degree.T
✗csgraph.laplacian(G, symmetrized=True)
✓csgraph.laplacian(G + G.T)
✓G = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) L, d = csgraph.laplacian(G, return_diag=True) L d scaling = np.sqrt(d)✓
scaling
✗(1/scaling)*L*(1/scaling)
✓L, d = csgraph.laplacian(G, return_diag=True, normed=True) L✓
d
✗G = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0]]) G L, d = csgraph.laplacian(G, return_diag=True, normed=True) L d✓
G = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]).astype(np.float32) G csgraph.laplacian(G)✓
L = csgraph.laplacian(G, form="lo") L L(np.eye(3))✓
L = csgraph.laplacian(G, form="function")
✓L
✗L(np.eye(3))
✓from scipy.sparse import diags_array, random_array from scipy.sparse.linalg import lobpcg✓
N = 35 G = diags_array(np.ones(N - 1), offsets=1, format="csr")✓
rng = np.random.default_rng() G += 1e-2 * random_array((N, N), density=0.1, rng=rng)✓
X = rng.random((N, 2))
✓Y = np.ones((N, 1))
✓for cut in ["max", "min"]: G = -G # 1. L = csgraph.laplacian(G, symmetrized=True, form="lo") # 2. _, eves = lobpcg(L, X, Y=Y, largest=False, tol=1e-2) # 3. eves *= np.sign(eves[0, 0]) # 4. print(cut + "-cut labels:\n", 1 * (eves[:, 0]>0)) # 5.✗
Aliases
-
scipy.sparse.csgraph.laplacian