使用NumPy的数据类型的大小 [英] Size of data type using NumPy

查看:221
本文介绍了使用NumPy的数据类型的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NumPy中,我可以通过以下方式获取特定数据类型的大小(以字节为单位):

In NumPy, I can get the size (in bytes) of a particular data type by:

datatype(...).itemsize

或:

datatype(...).nbytes

例如:

np.float32(5).itemsize #4
np.float32(5).nbytes   #4

我有两个问题.首先,有没有一种方法可以在不创建数据类型实例的情况下获取此信息?其次,itemsizenbytes有什么区别?

I have two questions. First, is there a way to get this information without creating an instance of the datatype? Second, what's the difference between itemsize and nbytes?

推荐答案

您需要dtype的实例来获取项目大小,但是您不需要ndarray的实例. (很快就会知道,nbytes是数组的属性,而不是dtype.)

You need an instance of the dtype to get the itemsize, but you shouldn't need an instance of the ndarray. (As will become clear in a second, nbytes is a property of the array, not the dtype.)

例如

print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize

itemsizenbytes之间的差异而言,nbytes只是x.itemsize * x.size.

As far as the difference between itemsize and nbytes, nbytes is just x.itemsize * x.size.

例如

In [16]: print np.arange(100).itemsize
8

In [17]: print np.arange(100).nbytes
800

这篇关于使用NumPy的数据类型的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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