2016-11-26 15 views
-1

私は、値が0.4〜1で20000 * 20000の行と列の値を持つ対称のnumpy配列を作成しようとしています。しかし、この大きな配列を作成するとメモリエラーが発生します。 下記の私のコードを見つけてください。 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: 

問題の記述を中心に:

は、ここに私の最初のメモリエラーのITが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

関連する問題