从scipy的稀疏矩阵中删除对角元素

9
我想从稀疏矩阵中删除对角线元素。由于矩阵是稀疏的,这些元素在删除后不应存储。Scipy提供了一种设置对角线元素值的方法:setdiag。如果我使用lil_matrix尝试它,它可以工作:
>>> a = np.ones((2,2))
>>> c = lil_matrix(a)
>>> c.setdiag(0)
>>> c
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements in LInked List format>

然而使用csr_matrix时,似乎对角线元素并没有从存储中移除:
>>> b = csr_matrix(a)
>>> b
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

>>> b.setdiag(0)
>>> b
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

>>> b.toarray()
array([[ 0.,  1.],
       [ 1.,  0.]])

通过密集的数组,我们当然有:
>>> csr_matrix(b.toarray())
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>

那是有意为之吗?如果是,是因为csr矩阵的压缩格式吗?除了从稀疏到密集再到稀疏的方法,还有其他解决方法吗?

1个回答

8

仅将元素设置为0并不会改变 csr 矩阵的稀疏度。 您需要应用 eliminate_zeros

In [807]: a=sparse.csr_matrix(np.ones((2,2)))
In [808]: a
Out[808]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [809]: a.setdiag(0)
In [810]: a
Out[810]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>
In [811]: a.eliminate_zeros()
In [812]: a
Out[812]: 
<2x2 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>

由于更改csr矩阵的稀疏度相对昂贵,因此它们允许您将值更改为0而不更改稀疏度。

In [829]: %%timeit a=sparse.csr_matrix(np.ones((1000,1000)))
     ...: a.setdiag(0)
100 loops, best of 3: 3.86 ms per loop

In [830]: %%timeit a=sparse.csr_matrix(np.ones((1000,1000)))
     ...: a.setdiag(0)
     ...: a.eliminate_zeros()
SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
10 loops, best of 3: 133 ms per loop

In [831]: %%timeit a=sparse.lil_matrix(np.ones((1000,1000)))
     ...: a.setdiag(0)
100 loops, best of 3: 14.1 ms per loop

我正需要的东西。谢谢! - kevad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接