def sp_reshape(A, shape, format='csr'): """ Reshapes a sparse matrix. Parameters ---------- A : sparse_matrix Input matrix in any format shape : list/tuple Desired shape of new matrix format : string {'csr','coo','csc','lil'} Optional string indicating desired output format Returns ------- B : csr_matrix Reshaped sparse matrix References ---------- http://stackoverflow.com/questions/16511879/reshape-sparse-matrix-efficiently-python-scipy-0-12 """ if not hasattr(shape, '__len__') or len(shape) != 2: raise ValueError('Shape must be a list of two integers') if format == 'csr': return zcsr_reshape(A, shape[0], shape[1]) C = A.tocoo() nrows, ncols = C.shape size = nrows * ncols new_size = shape[0] * shape[1] if new_size != size: raise ValueError('Total size of new array must be unchanged.') flat_indices = ncols * C.row + C.col new_row, new_col = divmod(flat_indices, shape[1]) B = sp.coo_matrix((C.data, (new_row, new_col)), shape=shape) if format == 'coo': return B elif format == 'csc': return B.tocsc() elif format == 'lil': return B.tolil() else: raise ValueError('Return format not valid.')