Numpy Matrix Memory size low compared to Numpy Array

流过昼夜 提交于 2020-03-05 03:10:39

问题


I have a .npz file which I want to load into RAM . The compressed file size is 30MB . I am doing the following operation to load the data into RAM.

import numpy as np
from scipy import sparse
from sys import getsizeof

a = sparse.load_npz('compressed/CRS.npz').todense()
getsizeof(a)
# 136
type(a)
# numpy.matrixlib.defmatrix.matrix
b = np.array(a)
getsizeof(b)
# 64000112
type(b)
# numpy.ndarray

Why numpy.matrix object occupy very low memory size compared to numpy.arrray ? Both a and b have same dimension and data.


回答1:


Your a matrix is a view of another array, so the underlying data is not counted towards its getsizeof. You can see this by checking that a.base is not None, or by seeing that the OWNDATA flag is False in a.flags.

Your b array is not a view, so the underlying data is counted towards its getsizeof.

numpy.matrix doesn't provide any memory savings.



来源:https://stackoverflow.com/questions/52897457/numpy-matrix-memory-size-low-compared-to-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!