Numpy Matrix内存大小比Numpy Array低 [英] Numpy Matrix Memory size low compared to Numpy Array
问题描述
我有一个.npz文件,我想将其加载到RAM中.压缩文件大小为30MB.我正在执行以下操作以将数据加载到RAM.
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
与numpy.arrray
相比,为什么numpy.matrix
对象占用的内存很小? a和b都具有相同的维度和数据.
Why numpy.matrix
object occupy very low memory size compared to numpy.arrray
? Both a and b have same dimension and data.
推荐答案
您的a
矩阵是另一个数组的视图,因此基础数据不计入其getsizeof
中.您可以通过检查a.base is not None
或查看OWNDATA
标志在a.flags
中是False
来看到此情况.
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
.
您的b
数组不是视图,因此基础数据计入其getsizeof
.
Your b
array is not a view, so the underlying data is counted towards its getsizeof
.
numpy.matrix
不能节省任何内存.
这篇关于Numpy Matrix内存大小比Numpy Array低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!