Asqan Asqan - 1 year ago 156
Python Question

Delete columns of matrix of CSR format in Python

I have a sparse matrix (22000x97482) in csr format and i want to delete some columns (indices of columns numbers are stored in a list)

Answer Source

You can use fancy indexing to obtain a new csr_matrix with the columns that you have in your list:

all_cols = np.arange(old_m.shape[1])
cols_to_keep = np.where(np.logical_not(np.in1d(all_cols, cols_to_delete)))[0]
m = old_m[:, cols_to_keep]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download