I have a sparse matrix A
in csr_matrix format. A
is the weighted adjacency matrix of an undirected graph and thus symmetric and nonnegative. I want to calculate its graph Lapalican.
I used to work with MATLAB, where the code is pretty simple:
L = diag(sum(A,2)) - A % or L=diag(sum(A))-A because A is symmetric
But how can we do it in Python? I am new to sparse matrices in Python. I can only come up with a quite ugly solution:
import numpy as np
import scipy.sparse as sps
L = sps.diags(np.reshape(np.array(A.sum(axis=1)), A.shape[0])) - A
Does anyone know a more elegant solution?