Numpy Matrix内存大小比Numpy Array低 [英] Numpy Matrix Memory size low compared to Numpy Array

查看:102
本文介绍了Numpy Matrix内存大小比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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆