0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

numpy.zeros()でndarrayを初期化した際はメモリが確保されない

Posted at

題の通り、ndarrayをnumpy.zeros([行数,列数])で初期化した際、**[行数,列数]サイズのndarrayのデータを入れるためのメモリ領域は確保されない。**内部的には0以外のデータに対してだけ実際にメモリを割り当てている模様。
このため、例えば100万×100万のような巨大なndarrayであっても、numpy.zeros()で初期化した場合は、メモリ32GBや64GBのPCでも軽快に扱うことが出来る。スパースな行列をメモリに制限のある環境で扱う際に利用できるかもしれない。(素直にscipy.sparseを使えば良い気もするが)

import numpy as np
import sys

# 100万×100万,型はfloat64のndarrayを生成
zero_array = np.zeros([1000000,1000000])

# メモリサイズを確認
sys.getsizeof(zero_array)
# 8000000000112(約8TB)

# メモリ32GBのPCでも軽快に動作する
print(zero_array)
#[[0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# ...
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]]

zero_array[0,0] = 1
print(zero_array)
#[[1. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# ...
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]
# [0. 0. 0. ... 0. 0. 0.]]

numpy.ones()で初期化しようとすると、メモリが足りずプロセスが落ちる。

one_array = np.ones([1000000,1000000])
# プロセス異常終了
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?