User:Xyy23330121/Python/科学计算/数组的数据类型
在创建数组时,使用 dtype 参数,可以让数组中的数字使用特定的数据类型。比如:
import numpy as np
x = np.ones(2, dtype=float) #形状为(2, )的全一数组,使用 Python 的 float 类型。
一切 Python 的数据类型都可以用于数组中,如果读者希望,也可以 from decimal import Decimal 然后设置数据类型为 Decimal。对于 Python 其它模块的数据类型不再赘述。以下将详细讲述 numpy 的数据类型。若无特殊说明,之后所有代码均以下面代码为开头。
import numpy as np
numpy 内置了许多数据类型。本节所述的类型都可以作为数据存放在数组中。
numpy 中的一切数字类型都以 np.number 为抽象基类。若需要判断数据是否是 numpy 的数字类型,而不关注数字具体的存储方式,可以使用 np.number 来进行判断。
| 占用的 字节数 |
有符号整数 | 无符号整数 | ||
|---|---|---|---|---|
| 类型名称 | 类型别名 | 类型名称 | 类型别名 | |
| 1 | np.byte | np.int8 | np.ubyte | np.uint8 |
| 2 | np.short | np.int16 | np.ushort | np.uint16 |
| 4 | np.intc | np.int32 | np.uintc | np.uint32 |
| 8 | np.long | np.int64 np.intp |
np.ulong | np.uint64 np.uintp |
| 16 | np.longlong | np.ulonglong | ||
| 特殊 | np.int_ 在32位系统占用4字节, 在64位系统占用8字节。 |
np.uint 在32位系统占用4字节, 在64位系统占用8字节。 | ||
numpy 的整数类型与 Python 内置整数类型不同,它有字节限制,因此它所存储的数值具有上下限。所有整数类型有一个抽象基类 np.integer。有符号整数额外有抽象基类 np.signedinteger,而无符号整数则具有 np.unsignedinteger 作为抽象基类。
特别的,numpy 的数组中是无法使用 Python 内置的整数类型的。numpy 的默认整数类型为 np.int64,当设置 dtype=int 时,numpy 会擅自将其类型设置为 int64。
| 占用的 字节数 |
浮点数 | 复数 | ||
|---|---|---|---|---|
| 类型名称 | 类型别名 | 类型名称 | 类型别名 | |
| 2 | np.half | np.float16 | ||
| 4 | np.single | np.float32 | ||
| 8 | np.double | np.float64 | np.csingle | np.complex64 |
| 16 | np.longdouble | np.float128 | np.cdouble | np.complex128 |
| 32 | np.clongdouble | np.complex256 | ||
numpy 还内置了许多浮点数类型。与整数相似,numpy 也给浮点数设置了一些抽象基类:
- np.inexact 一切可能存在误差的数字类型基类
- np.floating 一切浮点数的数字类型基类
- np.complexfloating 一切复数(由两个浮点数组成)的数字类型基类
特别的,np.float64 和 Python 内置的 float 类型是同一个类型,这也是 numpy 在遇到浮点数时的默认类型。np.complex128 和 Python 内置的 complex 类型是同一个类型。
注:对于一些系统(比如Windows系统), np.longdouble 类型可能并不会具有更高精度——而是和 np.float64 具有相同的精度。而 np.float128 高精度浮点数可能不可用。具体参见 Stack Overflow 上面的 这个讨论。
numpy 使用 np.bool 类型(别名 np.bool_)来存储布尔值。与 Python 中 bool 是 int 的子类型不同;np.bool 是 np.int_ 的子类型。
对于 numpy 提供的其它数据类型,可以参见:
- https://github.com/numpy/numpy/blob/v2.0.0/numpy/dtypes.pyi
- https://github.com/numpy/numpy/blob/v2.0.0/numpy/__init__.pyi#L158
- https://numpy.org/doc/stable/reference/routines.dtypes.html
类似 Python 的 datetime 模块,numpy 也有表示时间的数据类型 np.datetime64 和 np.timedelta64。参见: https://numpy.org/doc/stable/reference/arrays.datetime.html#arrays-datetime
numpy 使用np.str_ 作为字符串类型,使用 np.bytes_ 作为字节串类型。这两个类型分别拥有 Python 内置类型 str 和 bytes 的一切方法。除此之外,这两个类型有共同的抽象基类:
np.flexible不定长数据的基类。np.ndarray也属于该基类。np.characterAbstract base class of all character string scalar types.
除了用 isinstance() 函数判断数据类型之外,numpy 还为数组提供了一个属性 ndarray.dtype,用于判断数组内数据的类型。如下所示:
a = np.array([[1.1,2,3,4],[5,6,7,8]])
print(a.dtype) #输出:float64
print(type(a.dtype)) #输出:<class 'numpy.dtypes.Float64DType'>
b = np.array([[1.1,2,3,4],[5,6,7,8]],dtype=int)
print(b.dtype) #输出:int64
from decimal import Decimal as dec
c = np.array([[1.1,2,3,4],[5,6,7,8]],dtype=dec)
print(c.dtype) #输出:object
print(type(c.dtype)) #输出:<class 'numpy.dtypes.ObjectDType'>
特别的,对于非 Python 内置类型,dtype 总会输出 object。
| 属性 | 备注 |
|---|---|
ndarray.flags
|
Information about the memory layout of the array. |
ndarray.shape
|
数组的形状。 |
ndarray.strides
|
遍历数组时,在每个维度需要步进的字节数。 |
ndarray.ndim
|
数组的维数。 |
ndarray.data
|
指向数组数据起点的 Python 缓冲对象。 |
ndarray.size
|
数组的数据个数。 |
ndarray.itemsize
|
数组中每个数据占用的内存(单位:字节)。 |
ndarray.nbytes
|
数组所占用的总内存(单位:字节)。 |
ndarray.base
|
Base object if memory is from some other object. |
numpy 提供了一些属性用于查看数组的内存占用。这些属性由右表所示。
其中,关于 ndarray.strides 我们有:
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.strides) #输出:(32, 8)
print(a[0].nbytes) #输出:32
print(a.itemsize) #输出:8
https://numpy.org/doc/stable/reference/arrays.scalars.html#attributes
https://numpy.org/doc/stable/reference/arrays.scalars.html#methods
读者也可以通过 numpy.dtype() 函数来创建自己的数据类型,参见:
- https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype
- https://numpy.org/doc/stable/reference/arrays.dtypes.html#specifying-and-constructing-data-types
参见: