我想知道一个蒙面的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
为什么非屏蔽阵列比屏蔽阵列大?如何估计屏蔽数组与未屏蔽数组的实际大小?你知道吗
比较
g
数组和g_mask
的_data
属性,我们发现后者只是前者的view
:它们有相同的数据缓冲区,但它们的
id
不同:面罩也一样:
实际上,通过这种构造,
_mask
是相同的数组:掩蔽数组的
__array_interface__
是._data
属性的__array_interface__
:nbytes
是数组的数据缓冲区大小:布尔数组的每个元素有1个字节,浮点数为64,8个字节。你知道吗
numpy.ndarray
没有^{getsize
函数不兼容。GC系统看不到掩码数组的ndarray
部分所拥有的引用。尤其是,base
的g_mask
没有包含在输出中。你知道吗相关问题 更多 >
编程相关推荐