CSC形式を知ってはいけませんすべてのゼロでないエントリの行インデックスのリストを保持する場合、CSRフォーマットはすべての非ゼロエントリのカラムインデックスのリストを保持します。私はあなたが以下のように周りのものを交換することの利点を取ることができると思うし、私はそれにどんな副作用があってはならないと思う:あなたは今、このような何かを行うことができ
def swap_rows(mat, a, b) :
mat_csc = scipy.sparse.csc_matrix(mat)
a_idx = np.where(mat_csc.indices == a)
b_idx = np.where(mat_csc.indices == b)
mat_csc.indices[a_idx] = b
mat_csc.indices[b_idx] = a
return mat_csc.asformat(mat.format)
def swap_cols(mat, a, b) :
mat_csr = scipy.sparse.csr_matrix(mat)
a_idx = np.where(mat_csr.indices == a)
b_idx = np.where(mat_csr.indices == b)
mat_csr.indices[a_idx] = b
mat_csr.indices[b_idx] = a
return mat_csr.asformat(mat.format)
:
>>> mat = np.zeros((5,5))
>>> mat[[1, 2, 3, 3], [0, 2, 2, 4]] = 1
>>> mat = scipy.sparse.lil_matrix(mat)
>>> mat.todense()
matrix([[ 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 1., 0., 1.],
[ 0., 0., 0., 0., 0.]])
>>> swap_rows(mat, 1, 3)
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 4 stored elements in LInked List format>
>>> swap_rows(mat, 1, 3).todense()
matrix([[ 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 1.],
[ 0., 0., 1., 0., 0.],
[ 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
>>> swap_cols(mat, 0, 4)
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 4 stored elements in LInked List format>
>>> swap_cols(mat, 0, 4).todense()
matrix([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 1., 0., 0.],
[ 1., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0.]])
LILマトリックスを使用して、出力のタイプをどのように保持できるかを示しました。アプリケーションでは、既にCSCまたはCSR形式になっていて、変換を最小限に抑えるために、行または列を最初にスワップするかどうかを選択することをお勧めします。
私はこれを特定の実装に必要とします。しかし、同僚が私に指摘したように、一般的には、まばらな行列に対して順列をしません。まばらな行列「A」は、一般に線形マップ「y = Ax」として使用される。反復ソルバーで。したがって、この交換は、入力ベクトルx(これは 'A 'の列交換)または' y'のエントリ(これは行の交換)のエントリを入れ替えて、 'A'の周りにラッパーを書くことによってより良く実現されます。 – Jan