'scipy_sparse_csr_matrix'a '와 scipy_sparse_csr_matrix (boolean)'mask '가 두 개 있는데'a '요소를 0으로 설정하려면 마스크 요소 사실이다. scipy 스파 스 매트릭스에서 mask가 True이면 요소를 0으로 설정하는 효율적인 방법
>>>a
<3x3 sparse matrix of type '<type 'numpy.int32'>'
with 4 stored elements in Compressed Sparse Row format>
>>>a.todense()
matrix([[0, 0, 3],
[0, 1, 5],
[7, 0, 0]])
>>>mask
<3x3 sparse matrix of type '<type 'numpy.bool_'>'
with 4 stored elements in Compressed Sparse Row format>
>>>mask.todense()
matrix([[ True, False, True],
[False, False, True],
[False, True, False]], dtype=bool)
예를
에 대한은 그 때 나는 다음과 같은 결과를 얻을합니다.
>>>result
<3x3 sparse matrix of type '<type 'numpy.int32'>'
with 2 stored elements in Compressed Sparse Row format>
>>>result.todense()
matrix([[0, 0, 0],
[0, 1, 0],
[7, 0, 0]])
나는
result = a - a.multiply(mask)
또는
a -= a.multiply(mask) #I don't care either in-place or copy.
같은 조작으로 그것을 할 수 있습니다하지만 위의 작업이 비효율적 생각합니다. 'a'와 'mask'의 실제 모양이 67,108,864 × 2,000,000이기 때문에 이러한 작업은 고사양 서버 (64 코어 Xeon CPU, 512GB 메모리)에서 몇 초가 걸립니다. 예를 들어, 'a'는 약 30,000,000 개가 아닌 요소이고 'mask'는 약 1,800,000 개가 아닌 0 개 요소이며, 위의 연산은 약 2 초 걸립니다.
더 효율적인 방법이 있습니까?
조건은 다음과 같습니다.
- a.getnnz()! = mask.getnnz()
- a.shape = mask.shape
감사합니다!
다른 방법은
a.data*=~np.array(mask[a.astype(np.bool)]).flatten();a.eliminate_zeros() #This takes twice the time longer than above method.
'a'와'마스크 '의'nnz'는 어떻게 비교됩니까? 게다가 같지 않아. 둘 다 동등하게 희박합니까? – hpaulj
답변 해 주셔서 감사합니다. @hpaulj! 너를 혼동해서 미안해. 조건 1은 '마스크의 0이 아닌 (거짓) 요소의 수는 a의 요소 수와 다릅니다.'만을 의미합니다. – hiroto1228