당신은 sparse.hstack
와 밀도 계산을 복제 할 수 있습니다 :
In [1]: A = np.arange(9).reshape(3,3)
In [2]: from scipy import sparse
In [3]: M = sparse.csr_matrix(A)
In [4]: M
Out[4]:
<3x3 sparse matrix of type '<class 'numpy.int32'>'
with 8 stored elements in Compressed Sparse Row format>
In [5]: M.A
Out[5]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]], dtype=int32)
In [6]: [A[i:1+3+i-2,:] for i in range(2)]
Out[6]:
[array([[0, 1, 2],
[3, 4, 5]]), array([[3, 4, 5],
[6, 7, 8]])]
In [7]: [M[i:1+3+i-2,:] for i in range(2)]
Out[7]:
[<2x3 sparse matrix of type '<class 'numpy.int32'>'
with 5 stored elements in Compressed Sparse Row format>,
<2x3 sparse matrix of type '<class 'numpy.int32'>'
with 6 stored elements in Compressed Sparse Row format>]
In [8]: sparse.hstack([M[i:1+3+i-2,:] for i in range(2)])
Out[8]:
<2x6 sparse matrix of type '<class 'numpy.int32'>'
with 11 stored elements in COOrdinate format>
In [9]: _.A
Out[9]:
array([[0, 1, 2, 3, 4, 5],
[3, 4, 5, 6, 7, 8]], dtype=int32)
나는 어떤 속도 약속을하지 않습니다.
희소 행렬 곱셈은 잘 발달되어 있습니다 (행렬이 희박한 경우,이 예제는 그렇지 않습니다). 사실 행/열 합계, 심지어는 슬라이스와 같은 연산은 행렬 곱셈으로 수행됩니다. 즉, dotted
이 값을 합산하거나 선택할 때 행렬 또는 벡터를 생성합니다.
sparse.hstack
은 스파 스 블록 행렬 연산을 사용합니다. 이렇게하면 입력이 coo
형식으로 변환되고 각각 row
, col
, data
개의 오프셋이있는 배열이 결합되어 새로운 coo
행렬이 만들어집니다.
나는이 단계들을 결합하는 것을 상상할 수 있지만, 상당한 노력이 필요합니다.
오케이. 문자 그대로 바닥의 마지막 줄 이었기 때문에 나는 그것을 실제로 보지 못했습니다. 다음 번에 더 명확하게 해주세요 ;-) –
@ cᴏʟᴅsᴘᴇᴇᴅ 제목에도 나와 있습니다. 그러나 나는 당신의 요점을 생각합니다. 그러나 나는 당신의 열등한 트릭을 좋아했습니다. – eleanora