How can I remove a column from a sparse matrix efficiently?

Brandon Pelfrey picture Brandon Pelfrey · Mar 3, 2010 · Viewed 13k times · Source

If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently?

Answer

Newmu picture Newmu · Jun 20, 2013

Much simpler and faster. You might not even need the conversion to csr, but I just know for sure that it works with csr sparse matrices and converting between shouldn't be an issue.

from scipy import sparse

x_new = sparse.lil_matrix(sparse.csr_matrix(x)[:,col_list])