X_sparse = sparse.isspmatrix(X)

is enough rather than these 3 lines