Remove/set the non-zero diagonal elements of a sparse matrix in scipy
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given