Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly shuffle a sparse matrix in python

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])
    np.random.shuffle(index)
    return matrix[index]

How can I do it with numpy sparse?

like image 495
Puckl Avatar asked Sep 01 '12 19:09

Puckl


2 Answers

Ok, found it. The sparse format looks a bit confusing in the print-out.

    index = np.arange(np.shape(matrix)[0])
    print index
    np.random.shuffle(index)
    return matrix[index, :]
like image 191
Puckl Avatar answered Oct 08 '22 12:10

Puckl


In case anyone is looking to randomly get a subsample of rows from a sparse matrix, this related post may also be useful: How should I go about subsampling from a scipy.sparse.csr.csr_matrix and a list

like image 29
shaneb Avatar answered Oct 08 '22 11:10

shaneb