If you want to drop one or more columns, you can use boolean masking approach. In this approach, you will create a boolean array of size equal to the number of columns in the CSR matrix with values set to True. Then set the value to False for the columns you want to drop and apply the mask to drop columns. This approach is fast, memory-efficient, and preserves CSR format, ideal for big sparse matrices.
import numpy as np
from scipy.sparse import csr_matrix
X = np.array([[0, 5, 0, 0, 2], [3, 0, 0, 7, 0], [0, 0, 4, 0, 0], [0, 1, 0, 0, 6], [8, 0, 0, 0, 4]])
# Convert to CSR format
X_csr = csr_matrix(X)
# boolean mask
sel_cols = np.ones(X_csr.shape[1], dtype=bool)
# columns that need to be dropped
cols_to_drop = [1,2]
sel_cols[cols_to_drop] = False
# select remaining columns
X_csr = X_csr[:, sel_cols]
The code will produce a CSR matrix by removing columns 1 and 2.