복붙노트

[PYTHON] int 목록을 사용하는 희소 행렬 분할

PYTHON

int 목록을 사용하는 희소 행렬 분할

나는 거대한 & 희박한 데이터 (347, 5 416 812 801)의 매트릭스를 가지고 있지만, 데이터의 0.13 %만이 0이 아닌 매우 드문 드문 한 알고리즘을 배우는 기계를 작성하고있다.

내 희소 매트릭스의 크기는 105 000 바이트 (<1Mbytes)이며 csr 유형입니다.

각 열의 예제 인덱스 목록을 선택하여 열차 / 테스트 세트를 분리하려고합니다. 그래서 두 가지로 내 데이터 집합을 분할하고 싶습니다 :

training_set = matrix[train_indices]

모양의 (len (training_indices), 5 416 812 801), 아직도 드문 드문

testing_set = matrix[test_indices]

모양 (347-len (training_indices), 5 416 812 801)도 희소 함

training_indices와 testing_indices 두 int의 목록

하지만 training_set = matrix [train_indices]는 실패하고 Segmentation 오류를 반환합니다 (코어 덤프 됨)

64Gbytes의 RAM을 가진 서버에서이 코드를 실행하기 때문에 메모리의 문제는 아닐 수도 있습니다.

어떤 원인이 될 수 있는지에 대한 단서가 있습니까?

해결법

  1. ==============================

    1.Csr 행 색인 생성을 다시 작성한 것 같습니다.

    Csr 행 색인 생성을 다시 작성한 것 같습니다.

    def extractor(indices, N):
       indptr=np.arange(len(indices)+1)
       data=np.ones(len(indices))
       shape=(len(indices),N)
       return sparse.csr_matrix((data,indices,indptr), shape=shape)
    

    내가 교수형에 처한 csr 테스트 :

    In [185]: M
    Out[185]: 
    <30x40 sparse matrix of type '<class 'numpy.float64'>'
        with 76 stored elements in Compressed Sparse Row format>
    
    In [186]: indices=np.r_[0:20]
    
    In [187]: M[indices,:]
    Out[187]: 
    <20x40 sparse matrix of type '<class 'numpy.float64'>'
        with 57 stored elements in Compressed Sparse Row format>
    
    In [188]: extractor(indices, M.shape[0])*M
    Out[188]: 
    <20x40 sparse matrix of type '<class 'numpy.float64'>'
        with 57 stored elements in Compressed Sparse Row format>
    

    다른 많은 csr 메서드와 마찬가지로 행렬 곱셈을 사용하여 최종 값을 생성합니다. 이 경우 선택된 행에 1이있는 희소 행렬이 있습니다. 시간은 실제로 조금 더 좋습니다.

    In [189]: timeit M[indices,:]
    1000 loops, best of 3: 515 µs per loop
    In [190]: timeit extractor(indices, M.shape[0])*M
    1000 loops, best of 3: 399 µs per loop
    

    귀하의 경우 추출기 행렬은 (len (training_indices), 347) 모양이며 len (training_indices) 값만 있습니다. 그래서 그것은 크지 않습니다.

    그러나 행렬이 너무 커서 (또는 적어도 2 차원이 너무 커서) 행렬 곱셈 루틴에서 오류가 발생하면 파이썬 / numpy을 트래핑하지 않고 세그먼트 화 오류가 발생할 수 있습니다.

    matrix.sum (축 = 1)이 작동합니까? 그것도 1의 조밀 한 매트릭스와 함께 행렬 곱셈을 사용합니다. 또는 희박합니다. 예 (347) * M, 비슷한 크기의 행렬 곱셈입니까?

  2. from https://stackoverflow.com/questions/39500649/sparse-matrix-slicing-using-list-of-int by cc-by-sa and MIT license