Is there a numpy.delete() equivalent for sparse matrices?

numpy, python, scipy

Solution

For CSR, this is probably the most efficient way to do it in-place:

def delete_row_csr(mat, i):
    if not isinstance(mat, scipy.sparse.csr_matrix):
        raise ValueError("works only for CSR format -- use .tocsr() first")
    n = mat.indptr[i+1] - mat.indptr[i]
    if n > 0:
        mat.data[mat.indptr[i]:-n] = mat.data[mat.indptr[i+1]:]
        mat.data = mat.data[:-n]
        mat.indices[mat.indptr[i]:-n] = mat.indices[mat.indptr[i+1]:]
        mat.indices = mat.indices[:-n]
    mat.indptr[i:-1] = mat.indptr[i+1:]
    mat.indptr[i:] -= n
    mat.indptr = mat.indptr[:-1]
    mat._shape = (mat._shape[0]-1, mat._shape[1])

In LIL format it's even simpler:

def delete_row_lil(mat, i):
    if not isinstance(mat, scipy.sparse.lil_matrix):
        raise ValueError("works only for LIL format -- use .tolil() first")
    mat.rows = np.delete(mat.rows, i)
    mat.data = np.delete(mat.data, i)
    mat._shape = (mat._shape[0] - 1, mat._shape[1])

Problem

Let's say I have a 2-dimensional matrix as a numpy array. If I want to delete rows with specific indices in this matrix, I use `numpy.delete()`. Here is an example of what I mean: ``` In [1]: my_matrix = numpy.array([ ...: [10, 20, 30, 40, 50], ...: [15, 25, 35, 45, 55], ...: [95, 96, 97, 98, 99] ...: ]) In [2]: numpy.delete(my_matrix, [0, 2], axis=0) Out[2]: array([[15, 25, 35, 45, 55]]) ``` I'm looking for a way to do the above with matrices from the `scipy.sparse` package. I know it's possible to do this by converting the entire matrix into a numpy array but I don't want to do that. Is there any other way of doing that? Thanks a lot!

Original source

Related problems