有不同种类的 scipy.sparse 矩阵: csr_matrix 对矩阵代数来说很快但更新很慢, coo_matrix 对代数来说很慢/更新很快。它们在scipy.org/SciPyPackages/Sparse中进行了描述。
如果一个稀疏矩阵是 99% 0,那么sparsematrix + 1
99% 就是密集的。
例如,您可以手动将其扩展
y = dot( x + bias, npvec )
到以后使用的dot( x, npvec ) + bias * npvec
任何地方y
——可能适用于短代码,但没有乐趣。
我强烈推荐IPython进行尝试:
# add some combinations of scipy.sparse matrices + numpy vecs
# see http://www.scipy.org/SciPyPackages/Sparse
from __future__ import division
import numpy as np
from scipy import sparse as sp
npvec = np.tile( [0,0,0,0,1.], 20 )
Acsr = sp.csr_matrix(npvec)
Acoo = Acsr.tocoo()
for A in (Acsr, Acoo, npvec):
print "\n%s" % type(A)
for B in (Acsr, Acoo, npvec):
print "+ %s = " % type(B) ,
try:
AplusB = A + B
print type(AplusB)
except StandardError, errmsg:
print "Error", errmsg