NumPy之:標量scalars
簡介
Python語言中只定義了特定數據類的一種類型(比如只有一種整數類型,一種浮點類型等)。在不需要關注電腦中數據表示方式的普通應用程式中,這樣做很方便。但是,對於科學計算來說,我們需要更加精確的控制類型。
在NumPy中,引入了24種新的Python scalar類型用於更加準確的描述數據。這些類型都是可以直接在NumPy中的數組中使用的,所以也叫Array scalar類型。
本文將會詳細講解這24種scalar類型。
scalar類型的層次結構
先看一個張圖,看下scalar類型的層次結構:
上面實線方框括起來的,就是scalar類型。 這些標量類型,都可以通過 np.type
來訪問,比如:
In [130]: np.intc
Out[130]: numpy.int32
細心的小夥伴可能要問了,這不對呀,實線方框括起來的只有22中類型,還有兩個類型是什麼?
還有兩個是代表整數指針的 intp
和 uintp
。
注意,array scalars 類型是不可變的。
我們可以isinstance來對這些數組標量來進行層次結構的檢測。
例如,如果val是數組標量對象,則isinstance(val,np.generic)將返回True。如果val是複數值類型,則isinstance(val,np.complexfloating)將返回True。
內置Scalar類型
我們用下面的表來展示內置的Scalar類型和與他們相對應的C類型或者Python類型。最後一列的字元程式碼是類型的字元表示,在有些情況比如構建dtype中會使用到。
boolean
類型 | 描述 | 字元程式碼 |
---|---|---|
bool_ |
compatible: Python bool | '?' |
bool8 |
8 bits |
Integers
類型 | 描述 | 字元程式碼 |
---|---|---|
byte |
compatible: C char | 'b' |
short |
compatible: C short | 'h' |
intc |
compatible: C int | 'i' |
int_ |
compatible: Python int | 'l' |
longlong |
compatible: C long long | 'q' |
intp |
large enough to fit a pointer | 'p' |
int8 |
8 bits | |
int16 |
16 bits | |
int32 |
32 bits | |
int64 |
64 bits |
Unsigned integers
類型 | 描述 | 字元程式碼 |
---|---|---|
ubyte |
compatible: C unsigned char | 'B' |
ushort |
compatible: C unsigned short | 'H' |
uintc |
compatible: C unsigned int | 'I' |
uint |
compatible: Python int | 'L' |
ulonglong |
compatible: C long long | 'Q' |
uintp |
large enough to fit a pointer | 'P' |
uint8 |
8 bits | |
uint16 |
16 bits | |
uint32 |
32 bits | |
uint64 |
64 bits |
Floating-point numbers
類型 | 描述 | 字元程式碼 |
---|---|---|
half |
'e' |
|
single |
compatible: C float | 'f' |
double |
compatible: C double | |
float_ |
compatible: Python float | 'd' |
longfloat |
compatible: C long float | 'g' |
float16 |
16 bits | |
float32 |
32 bits | |
float64 |
64 bits | |
float96 |
96 bits, platform? | |
float128 |
128 bits, platform? |
Complex floating-point numbers
類型 | 描述 | 字元程式碼 |
---|---|---|
csingle |
'F' |
|
complex_ |
compatible: Python complex | 'D' |
clongfloat |
'G' |
|
complex64 |
two 32-bit floats | |
complex128 |
two 64-bit floats | |
complex192 |
two 96-bit floats, platform? | |
complex256 |
two 128-bit floats, platform? |
Python 對象
類型 | 描述 | 字元程式碼 |
---|---|---|
object_ |
any Python object | 'O' |
對於數組中的對象類型
object_
來說,存儲的數據其實是Python對象的引用,所以說他們的對象類型必須一致。雖然存儲的是引用,但是在取值訪問的時候,返回的就是對象本身。
可以看到對於數字類型來說,int,uint,float,complex,後面可以跟上具體的數組,表示特定的長度。
intp 和 uintp 是兩個指向整數的指針。
有些類型和Python自帶的類型基本上是等價的,事實上這些類型就是繼承自Python自帶的類型:
Array scalar type | Related Python type |
---|---|
int_ |
IntType (Python 2 only) |
float_ |
FloatType |
complex_ |
ComplexType |
bytes_ |
BytesType |
unicode_ |
UnicodeType |
有一個特例就是bool_ ,它和Python的 BooleanType 非常類似,但並不是繼承自BooleanType。因為Python的BooleanType 是不允許被繼承的。並且兩者底層的數據存儲長度也是不一樣的。
雖然在Python中bool是int的子類。但是在NumPy中 bool_ 並不是
int_
的子類,bool_ 甚至不是一個number 類型。
在Python 3 中,
int_
不再繼承 Python3 中的int
了,因為int
不再是一個固定長度的整數。
NumPy 默認的數據類型是 float_。
可變長度數據類型
下面的三種數據類型長度是可變的,
類型 | 描述 | 字元程式碼 |
---|---|---|
bytes_ |
compatible: Python bytes | 'S#' |
unicode_ |
compatible: Python unicode/str | 'U#' |
void |
'V#' |
字元程式碼中的 # 表示的是數字。
上面描述的字元程式碼,為了和Python的其他模組進行兼容,比如struct ,需要進行下面適當的修正:
c -> S1
,b -> B
,1 -> b
,s -> h
,w -> H
, 和u -> I
.
本文已收錄於 //www.flydean.com/03-python-numpy-scalar/
最通俗的解讀,最深刻的乾貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!
歡迎關注我的公眾號:「程式那些事」,懂技術,更懂你!