2016-11-26 2 views
-1

20000 * 20000 개의 행과 열이있는 0.4 - 1 범위의 값으로 대칭 배열을 만들려고합니다. 그러나이 큰 배열을 만들 때 메모리 오류가 발생합니다. 아래 코드를 찾으십시오. NP대형 탁아한 배열 만들기

def random_symmetric_matrix(n): 
    _R = np.random.uniform(0.4,1,n*(n-1)/2) 
    P = np.zeros((n,n)) 
    P[np.triu_indices(n, 1)] = _R 
    P[np.tril_indices(n, -1)] = P.T[np.tril_indices(n, -1)] 
    print P 
    np.savetxt("b.txt",P, delimiter=' ') 
    return P 

random_symmetric_matrix(6000) 
+0

추적은 어디에 있습니까? – hpaulj

+0

@hpaulj : 메모리 오류 – Praveen

+0

(20000 * 20000)/2 부동 소수점 숫자 *로 구성된 배열은 실제 실제로는 큰 개체이며 기가 바이트 단위입니다 – Rojan

답변

1

수입 NumPy와 나는 함수를 복사하고 인쇄 및 savetxt 제거 : 그것은 N = 4000 수렁 시작 오래 된 작은 시스템에서

In [574]: def random_symmetric_matrix(n): 
    ...:  _R = np.random.uniform(0.4,1,n*(n-1)//2) 
    ...:  P = np.zeros((n,n)) 
    ...:  print('...') 
    ...:  P[np.triu_indices(n, 1)] = _R 
    ...:  print(',,,') 
    ...:  P[np.tril_indices(n, -1)] = P.T[np.tril_indices(n, -1)] 
    ...:  return P 

합니다.

In [573]: random_symmetric_matrix(14000).shape 
... 
--------------------------------------------------------------------------- 
MemoryError        Traceback (most recent call last) 
<ipython-input-573-32a007267a79> in <module>() 
----> 1 random_symmetric_matrix(14000).shape 

<ipython-input-565-9f171b601d49> in random_symmetric_matrix(n) 
     3  P = np.zeros((n,n)) 
     4  print('...') 
----> 5  P[np.triu_indices(n, 1)] = _R 
     6  print(',,,') 
     7  P[np.tril_indices(n, -1)] = P.T[np.tril_indices(n, -1)] 

/usr/lib/python3/dist-packages/numpy/lib/twodim_base.py in triu_indices(n, k, m) 
    973 
    974  """ 
--> 975  return where(~tri(n, m, k=k-1, dtype=bool)) 
    976 
    977 

MemoryError: 

문제 문에 초점 :

여기 내 첫 번째 메모리 오류의 그것은 tri를 구성하는 문제가 발생하지 않는

In [576]: np.triu_indices(4,1) 
Out[576]: 
(array([0, 0, 0, 1, 1, 2], dtype=int32), 
array([1, 2, 3, 2, 3, 3], dtype=int32)) 
In [577]: np.triu_indices(4,1)[0].shape 
Out[577]: (6,) 
In [578]: np.triu_indices(400,1)[0].shape 
Out[578]: (79800,) 
In [579]: np.triu_indices(4000,1)[0].shape 
Out[579]: (7998000,) 

; 그러나 where 색인을 수집하면 메모리가 소모됩니다. 그것은 P 배열에 대해 충분한 메모리가 나타나는 동안

In [593]: T=np.tri(10000, 10000, k=-1, dtype=bool) 
In [594]: T.shape 
Out[594]: (10000, 10000) 
In [595]: np.where(T) 
--------------------------------------------------------------------------- 
MemoryError        Traceback (most recent call last) 
<ipython-input-595-33094d2967ea> in <module>() 
----> 1 np.where(T) 

MemoryError: 

그래서, 그 길을 따라 인덱싱 너무 많은 메모리를 사용합니다. 이 문제를 해결할 방법을 모르지만 적어도 검색 할 위치는 이제 알았습니다.

+1

충분한 메모리가 없다면 전통적인 방식으로 루프를 돌리고 (조금 속도가 올라가면 청크로) 인덱스를 설정해야합니다. – sirfz