Randomly shuffle a sparse matrix in python

前端 未结 3 1516
孤城傲影
孤城傲影 2021-01-04 20:42

is there an easy way to shuffle a sparse matrix in python?

This is how I shuffle a non-sparse matrix:

    index = np.arange(np.shape(matrix)[0])
             


        
3条回答
  •  死守一世寂寞
    2021-01-04 21:05

    A better way can be shuffling the index of CSR Matrix and fetching the rows of matrix as such:

    from random import shuffle
    indices = np.arange(matrix.shape[0]) #gets the number of rows 
    shuffle(indices)
    shuffled_matrix = matrix[list(indices)] 
    

提交回复
热议问题