2017-12-31 69 views
0

tdeltaの2つのベクトル - 両方とも長さがnです。すべてのiについてはにというエントリを左にdelta[i]列で移動したいと考えていますが、私はこのベクトルを使ってDIAの行列Aを作成して列を調整したいと思います。疎DIA行列を作成して列を変更する

カラムを簡単に制御する方法は、COOフォーマットです。ここで私は仕事だろうと思ったものだ:tdeltaの長さは239ている間

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

しかし、私の例ではA.nnz == len(A.col)は、唯一216です。私はそれが起こった方法を理解していない、nnz店舗"Number of stored values, including explicit zeros."を与えられた。

この問題にはどのように対処できますか? 0アウト

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]) 

答えて

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にストリップ:ここに私の例のデータです。

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)です。


Iは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> 
関連する問題