您要做的并不是真正的numpy或scipy理解为重塑。但是对于您的特定情况,您可以重新创建CSR矩阵
data,
indices并重新使用
indptr,而无需复制它们:
import scipy.sparse as spsa = sps.rand(10000, 10000, density=0.01, format='csr')In [19]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),... shape=(10000, 10020), copy=True)100 loops, best of 3: 6.26 ms per loopIn [20]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),... shape=(10000, 10020), copy=False)10000 loops, best of 3: 47.3 us per loopIn [21]: %timeit sps.csr_matrix((a.data, a.indices, a.indptr),... shape=(10000, 10020))10000 loops, best of 3: 48.2 us per loop
因此,如果您不再需要原始矩阵
a,因为默认值为
copy=False,则只需执行以下操作:
a = sps.csr_matrix((a.data, a.indices, a.indptr), shape=(10000, 10020))



