【问题标题】:Elementwise addition of sparse scipy matrix vector with broadcasting稀疏 scipy 矩阵向量的元素加法与广播
【发布时间】:2015-06-11 06:29:51
【问题描述】:

我试图弄清楚如何最好地执行稀疏矩阵和稀疏向量的元素加法(和减法)。我在 SO 上找到了this trick

mat = sp.csc_matrix([[1,0,0],[0,1,0],[0,0,1]])
vec = sp.csr_matrix([[1,2,1]])
mat.data += np.repeat(vec.toarray()[0], np.diff(mat.indptr))

但不幸的是它只更新非零值:

print(mat.todense())
[[2 0 0]
 [0 3 0]
 [0 0 2]]

SO 线程上实际接受的答案:

def sum(X,v):
    rows, cols = X.shape
    row_start_stop = as_strided(X.indptr, shape=(rows, 2),
                            strides=2*X.indptr.strides)
    for row, (start, stop) in enumerate(row_start_stop):
        data = X.data[start:stop]
        data -= v[row]

sum(mat,vec.A[0])

做同样的事情。不幸的是,我现在没有想法,所以我希望你能帮助我找出解决这个问题的最佳方法。

编辑: 我希望它和密集版本一样:

np.eye(3) + np.asarray([[1,2,1]])
array([[ 2.,  2.,  1.],
       [ 1.,  3.,  1.],
       [ 1.,  2.,  2.]])

谢谢

【问题讨论】:

  • 该添加应该产生什么?您是将vec 值添加到mat 的非零值,还是添加到所有值?结果还会稀疏吗?
  • 我建议一个更一般的例子,不只涉及 0 和 1。
  • mat+vec.A 有什么问题?或者sparse.csr_matrix(mat+vec.A) 如果结果必须是稀疏格式?查看mat.__add__的代码。
  • @hpaulj 这意味着有必要在再次将其转换为稀疏矩阵之前创建矩阵的密集版本。我的矩阵太大而无法选择密集矩阵。
  • 结果是稀疏的还是密集的——在是否有很多 0 的意义上?你的例子并不稀疏。

标签: python numpy scipy sparse-matrix


【解决方案1】:

一些使用 10x10 稀疏 mat 和 vec 的测试:

In [375]: mat=sparse.rand(10,10,.1) 
In [376]: mat
Out[376]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in COOrdinate format>

In [377]: mat.A
Out[377]: 
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.15568621,  0.59916335,  0.        ,  0.        ,  0.        ],
       ...
       [ 0.        ,  0.        ,  0.15552687,  0.        ,  0.        ,
         0.47483064,  0.        ,  0.        ,  0.        ,  0.        ]])

In [378]: vec=sparse.coo_matrix([0,1,0,2,0,0,0,3,0,0]).tocsr()
<1x10 sparse matrix of type '<class 'numpy.int32'>'
    with 3 stored elements in Compressed Sparse Row format>

maxymoo 的解决方案:

def addvec(mat,vec):
    Mc = mat.tocsc()
    for i in vec.nonzero()[1]:
        Mc[:,i]=sparse.csc_matrix(Mc[:,i].todense()+vec[0,i])
    return Mc    

以及使用lil 格式的变体,在更改稀疏结构时应该更有效:

def addvec2(mat,vec):
    Ml=mat.tolil()
    vec=vec.tocoo()                                            
    for i,v in zip(vec.col, vec.data):
        Ml[:,i]=sparse.coo_matrix(Ml[:,i].A+v)
    return Ml

求和有 38 个非零项,高于原始 mat 中的 10 个。它添加了来自vec 的 3 列。这是稀疏性的巨大变化。

In [382]: addvec(mat,vec)
Out[382]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 38 stored elements in Compressed Sparse Column format>

In [383]: _.A
Out[383]: 
array([[ 0.        ,  1.        ,  0.        ,  2.        ,  0.        ,
         0.        ,  0.        ,  3.        ,  0.        ,  0.        ],
       [ 0.        ,  1.        ,  0.        ,  2.        ,  0.        ,
         0.15568621,  0.59916335,  3.        ,  0.        ,  0.        ],
       ...
       [ 0.        ,  1.        ,  0.15552687,  2.        ,  0.        ,
         0.47483064,  0.        ,  3.        ,  0.        ,  0.        ]])

与 addvec2 相同的输出:

In [384]: addvec2(mat,vec)
Out[384]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 38 stored elements in LInked List format>

在时间上,addvec2 的表现优于 2 倍

In [385]: timeit addvec(mat,vec)
100 loops, best of 3: 6.51 ms per loop

In [386]: timeit addvec2(mat,vec)
100 loops, best of 3: 2.54 ms per loop

和密集的等价物:

In [388]: sparse.coo_matrix(mat+vec.A)
Out[388]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 38 stored elements in COOrdinate format>

In [389]: timeit sparse.coo_matrix(mat+vec.A)
1000 loops, best of 3: 716 µs per loop

In [390]: timeit sparse.coo_matrix(mat.A+vec.A)
1000 loops, best of 3: 338 µs per loop

一个可以节省临时密集矩阵空间的版本,同时运行:

In [393]: timeit temp=mat.A; temp+=vec.A; sparse.coo_matrix(temp)
1000 loops, best of 3: 334 µs per loop

所以密集版本比我的稀疏版本好 5-7 倍。

对于一个非常大的mat,内存问题可能会影响密集性能,但迭代稀疏解决方案也不会大放异彩。

我可以通过更有效地索引Ml 来从addvec2 中获得更多性能。 Ml.data[3],Ml.rows[3]Ml[3,:]Ml[:,3] 快得多。

def addvec3(mat,vec):
    Mtl=mat.T.tolil()
    vec=vec.tocoo()
    n = mat.shape[0]
    for i,v in zip(vec.col, vec.data):
        t = np.zeros((n,))+v
        t[Mtl.rows[i]] += Mtl.data[i]
        t = sparse.coo_matrix(t)
        Mtl.rows[i] = t.col
        Mtl.data[i] = t.data
    return Mtl.T

In [468]: timeit addvec3(mat,vec)
1000 loops, best of 3: 1.8 ms per loop

一个适度的改进,但没有我希望的那么多。再挤一点:

def addvec3(mat,vec):
    Mtl = mat.T.tolil()
    vec = vec.tocoo(); 
    t0 = np.zeros((mat.shape[0],))
    r0 = np.arange(mat.shape[0])
    for i,v in zip(vec.col, vec.data):
        t = t0+v
        t[Mtl.rows[i]] += Mtl.data[i]
        Mtl.rows[i] = r0
        Mtl.data[i] = t
    return Mtl.T

In [531]: timeit mm=addvec3(mat,vec)
1000 loops, best of 3: 1.37 ms per loop

【讨论】:

  • 嗯,很有趣...对于mat=sp.rand(10**3,10**3,.001),稀疏版本快了 5 倍...不过这并不令人惊讶。
  • vec 的稀缺性在相对时间上有很大的不同。它不会影响密集版本的时间,但其他版本会迭代非零值。
【解决方案2】:

所以你的原始矩阵是稀疏的,向量是稀疏的,但在结果矩阵中,对应于向量中非零坐标的列将是密集的。

所以我们不妨将这些列具体化为密集矩阵

def addvec(mat,vec):
   for i in vec.nonzero()[1]:
      mat[:,i] = sp.csc_matrix(mat[:,i].todense() + vec[0,i])
   return mat

【讨论】:

  • 我必须使用for i in vec.nonzero()[1]:。由于分配了mat[:,i],我还收到了效率警告。
  • 你说得对,我已经在我的回答中做出了改变。我没有得到效率警告。你用的是什么版本的scipy?我在 Python 3.4 上使用 0.14
  • 相同版本;我第一次(在会话中)收到这个警告,我做了一些事情来改变 csc 或 csr 矩阵的稀疏性。 Changing the sparsity structure of a csc_matrix is expensive. lil_matrix is more efficient.
猜你喜欢
  • 1970-01-01
  • 2021-02-28
  • 2017-03-26
  • 2017-03-31
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多