用戶: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
參見: