4
scipy.sparse.csr_matrix
에서 대각선을 제거하고 싶습니다. 그렇게 효율적인 방법이 있습니까? 나는 sparsetools
모듈에 대각선을 반환하는 함수가 C
인 것을 보았습니다. 다른 바탕으로scipy에서 희소 행렬의 0이 아닌 대각선 요소 제거/설정
그래서 내 현재의 접근 방식은 다음과 같다 herehere에 응답하고 :
def csr_setdiag_val(csr, value=0):
"""Set all diagonal nonzero elements
(elements currently in the sparsity pattern)
to the given value. Useful to set to 0 mostly.
"""
if csr.format != "csr":
raise ValueError('Matrix given must be of CSR format.')
csr.sort_indices()
pointer = csr.indptr
indices = csr.indices
data = csr.data
for i in range(min(csr.shape)):
ind = indices[pointer[i]: pointer[i + 1]]
j = ind.searchsorted(i)
# matrix has only elements up until diagonal (in row i)
if j == len(ind):
continue
j += pointer[i]
# in case matrix has only elements after diagonal (in row i)
if indices[j] == i:
data[j] = value
그때 작성하지 않고 내가 할 수있는 최선의 것을
csr.eliminate_zeros()
에 따라 어떤 내 자신의 Cython
코드?
가되어'충분하지 scr_matrix.setdiag'? –
'setdiag'는 배열을 취하고 이전에 행렬에 없었던 요소들을 설정합니다. 따라서 매트릭스에 새로운 원소를 추가하는 것은 값 비싸지 만 나는 그것들을 비교하지 않았습니다. – Midnighter
제거하고자하는 대각선으로 새 스파 스 행렬을 생성 한 다음 빼기는 어떻습니까? 항목을 완전히 제거하려면 '압축'기능을 실행해야 할 수도 있습니다. – hpaulj