Python 创建稀疏直径矩阵,然后更改列
我有两个向量t和delta,都是长度n。我想用这个向量创建一个稀疏的DIA矩阵a,然后调整列:对于所有I,我想将a by delta[I]列中的第I个条目向左移动 控制列的一种简单方法是采用COO格式。以下是我认为有效的方法:Python 创建稀疏直径矩阵,然后更改列,python,scipy,sparse-matrix,Python,Scipy,Sparse Matrix,我有两个向量t和delta,都是长度n。我想用这个向量创建一个稀疏的DIA矩阵a,然后调整列:对于所有I,我想将a by delta[I]列中的第I个条目向左移动 控制列的一种简单方法是采用COO格式。以下是我认为有效的方法: from scipy.sparse import diags A = diags([t], offsets=[-1]).tocoo() A.col = A.col - delta 然而,在我的示例中,A.nnz==lenA.col仅为216,而t和delta的长度为23
from scipy.sparse import diags
A = diags([t], offsets=[-1]).tocoo()
A.col = A.col - delta
然而,在我的示例中,A.nnz==lenA.col仅为216,而t和delta的长度为239。考虑到nnz的商店,我不明白这是怎么发生的
我如何解决这个问题?以下是我的示例数据:
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])
查看A.tocoo的代码,即dia-to-coo版本。它有自己的数据!=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>
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>