创建稀疏DIA矩阵,然后更改列

问题描述 投票:0回答:1

我有两个矢量tdelta - 两个长度n。我想用该向量创建一个稀疏的DIA矩阵A,然后调整列:对于所有i,我想通过ith列向左移动A中的delta[i]条目。

控制列的简单方法是COO格式。这是我认为会起作用的:

from scipy.sparse import diags
A = diags([t], offsets=[-1]).tocoo()
A.col = A.col - delta

然而,在我的例子中,A.nnz == len(A.col)只是216,而tdelta的长度是239。鉴于nnz存储"Number of stored values, including explicit zeros.",我不明白这是怎么回事。

我该如何解决这个问题?这是我的示例数据:

from numpy import np
t = np.array([ 2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.655,
        2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.655,
        2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.655,  2.155,
        2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  2.155,
        2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  2.155,
        2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  2.155,  1.655,
        1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.655,
        1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.655,
        1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.655,  1.155,
        1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  1.155,
        1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  1.155,
        1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  1.155,  0.655,
        0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.655,
        0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.655,
        0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.655,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,
        0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.405,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ])
delta = np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
   5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5,
   5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
   5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
   5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
   4, 5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
   4, 4, 5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
   4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
   4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3,
   3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 2, 0, 2, 2, 3, 2,
   1, 3, 4, 3, 0, 3, 5, 4, 1, 0])
python scipy sparse-matrix
1个回答
1
投票
In [29]: t = np.array([1.2, 3.2, 4, 0, 0])
In [30]: A = sparse.diags([t], offsets=[-1])
In [31]: A
Out[31]: 
<6x6 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements (1 diagonals) in DIAgonal format>

转换为coo剥离了0。

In [32]: Ac = A.tocoo()
In [33]: Ac
Out[33]: 
<6x6 sparse matrix of type '<class 'numpy.float64'>'
    with 3 stored elements in COOrdinate format>

看看A.tocoo的代码(diacoo版本)。它有(self.data != 0)面具。


如果我直接制作coo矩阵,它会保留零,至少是暂时的:

In [58]: A.A
Out[58]: 
array([[ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 1.2,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  3.2,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  4. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ]])
In [59]: M = sparse.coo_matrix((t, (np.arange(1,6),np.arange(5))),shape=(6,6))
In [60]: M
Out[60]: 
<6x6 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in COOrdinate format>
In [61]: M.A
Out[61]: 
array([[ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 1.2,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  3.2,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  4. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ]])

内部零删除:

In [64]: M.eliminate_zeros()
In [65]: M
Out[65]: 
<6x6 sparse matrix of type '<class 'numpy.float64'>'
    with 3 stored elements in COOrdinate format>
© www.soinside.com 2019 - 2024. All rights reserved.