Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 527
pemistahl Avatar asked Oct 25 '12 21:10

pemistahl


People also ask

Does NumPy have sparse matrices?

Sparse Matrices in PythonA dense matrix stored in a NumPy array can be converted into a sparse matrix using the CSR representation by calling the csr_matrix() function.

How do you delete a matrix in python?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.).

How do I delete multiple rows in NumPy?

delete() – The numpy. delete() is a function in Python which returns a new array with the deletion of sub-arrays along with the mentioned axis. By keeping the value of the axis as zero, there are two possible ways to delete multiple rows using numphy. delete().


1 Answers

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]) 
like image 52
pv. Avatar answered Sep 18 '22 13:09

pv.