Thursday, April 5, 2012

Copy one sparse matrix into another in Python

Copying Xf into features. In this case, Xf is a scipy.sparse.csr_matrix and features is a dok_matrix.

        nz_indices = Xf.nonzero()
        for ix, jx in zip(nz_indices[0], nz_indices[1]):
            features[ix, jx] = Xf[ix, jx]


1. When I checked, the assignment of a 0 to an element of a dok_matrix resulted in a crash. So 0 assignments have to be avoided. 

No comments: