【发布时间】:2020-02-28 17:02:31
【问题描述】:
我试图了解 numpy 掩码数组和带有 nans 的普通数组之间的大小差异。
import numpy as np
g = np.random.random((5000,5000))
indx = np.random.randint(0,4999,(500,2))
mask = np.full((5000,5000),False,dtype=bool)
mask[indx] = True
g_mask = np.ma.array(g,mask=mask)
我使用以下answer 来计算对象的大小:
import sys
from types import ModuleType, FunctionType
from gc import get_referents
# Custom objects know their class.
# Function objects seem to know way too much, including modules.
# Exclude modules as well.
BLACKLIST = type, ModuleType, FunctionType
def getsize(obj):
"""sum size of object & members."""
if isinstance(obj, BLACKLIST):
raise TypeError('getsize() does not take argument of type: '+ str(type(obj)))
seen_ids = set()
size = 0
objects = [obj]
while objects:
need_referents = []
for obj in objects:
if not isinstance(obj, BLACKLIST) and id(obj) not in seen_ids:
seen_ids.add(id(obj))
size += sys.getsizeof(obj)
need_referents.append(obj)
objects = get_referents(*need_referents)
return size
这给了我以下结果:
getsize(g)
>>>200000112
getsize(g_mask)
>>>25000924
为什么未屏蔽数组比屏蔽数组大?如何估计掩码数组与未掩码数组的实际大小?
【问题讨论】:
标签: python arrays numpy size mask