scipy sparse matrix: remove the rows whose all elements are zero

后端 未结 3 567
天命终不由人
天命终不由人 2021-02-09 14:23

I have a sparse matrix which is transformed from sklearn tfidfVectorier. I believe that some rows are all-zero rows. I want to remove them. However, as far as I know, the existi

3条回答
  •  走了就别回头了
    2021-02-09 15:25

    Thanks for your reply, @perimosocordiae

    I just find another solution by myself. I am posting here in case someone may need it in the future.

    def remove_zero_rows(X)
        # X is a scipy sparse matrix. We want to remove all zero rows from it
        nonzero_row_indice, _ = X.nonzero()
        unique_nonzero_indice = numpy.unique(nonzero_row_indice)
        return X[unique_nonzero_indice]
    

提交回复
热议问题