NumPy之:标量scalars
文章目錄
- 簡介
- scalar類型的層次結(jié)構(gòu)
- 內(nèi)置Scalar類型
- boolean
- Integers
- Unsigned integers
- Floating-point numbers
- Complex floating-point numbers
- Python 對(duì)象
- 可變長度數(shù)據(jù)類型
簡介
Python語言中只定義了特定數(shù)據(jù)類的一種類型(比如只有一種整數(shù)類型,一種浮點(diǎn)類型等)。在不需要關(guān)注計(jì)算機(jī)中數(shù)據(jù)表示方式的普通應(yīng)用程序中,這樣做很方便。但是,對(duì)于科學(xué)計(jì)算來說,我們需要更加精確的控制類型。
在NumPy中,引入了24種新的Python scalar類型用于更加準(zhǔn)確的描述數(shù)據(jù)。這些類型都是可以直接在NumPy中的數(shù)組中使用的,所以也叫Array scalar類型。
本文將會(huì)詳細(xì)講解這24種scalar類型。
scalar類型的層次結(jié)構(gòu)
先看一個(gè)張圖,看下scalar類型的層次結(jié)構(gòu):
上面實(shí)線方框括起來的,就是scalar類型。 這些標(biāo)量類型,都可以通過 np.type來訪問,比如:
In [130]: np.intc Out[130]: numpy.int32細(xì)心的小伙伴可能要問了,這不對(duì)呀,實(shí)線方框括起來的只有22中類型,還有兩個(gè)類型是什么?
還有兩個(gè)是代表整數(shù)指針的 intp 和 uintp 。
注意,array scalars 類型是不可變的。
我們可以isinstance來對(duì)這些數(shù)組標(biāo)量來進(jìn)行層次結(jié)構(gòu)的檢測(cè)。
例如,如果val是數(shù)組標(biāo)量對(duì)象,則isinstance(val,np.generic)將返回True。如果val是復(fù)數(shù)值類型,則isinstance(val,np.complexfloating)將返回True。
內(nèi)置Scalar類型
我們用下面的表來展示內(nèi)置的Scalar類型和與他們相對(duì)應(yīng)的C類型或者Python類型。最后一列的字符代碼是類型的字符表示,在有些情況比如構(gòu)建dtype中會(huì)使用到。
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 對(duì)象
| object_ | any Python object | 'O' |
對(duì)于數(shù)組中的對(duì)象類型object_來說,存儲(chǔ)的數(shù)據(jù)其實(shí)是Python對(duì)象的引用,所以說他們的對(duì)象類型必須一致。
雖然存儲(chǔ)的是引用,但是在取值訪問的時(shí)候,返回的就是對(duì)象本身。
可以看到對(duì)于數(shù)字類型來說,int,uint,float,complex,后面可以跟上具體的數(shù)組,表示特定的長度。
intp 和 uintp 是兩個(gè)指向整數(shù)的指針。
有些類型和Python自帶的類型基本上是等價(jià)的,事實(shí)上這些類型就是繼承自Python自帶的類型:
| int_ | IntType (Python 2 only) |
| float_ | FloatType |
| complex_ | ComplexType |
| bytes_ | BytesType |
| unicode_ | UnicodeType |
有一個(gè)特例就是bool_ ,它和Python的 BooleanType 非常類似,但并不是繼承自BooleanType。因?yàn)镻ython的BooleanType 是不允許被繼承的。并且兩者底層的數(shù)據(jù)存儲(chǔ)長度也是不一樣的。
雖然在Python中bool是int的子類。但是在NumPy中 bool_ 并不是 int_ 的子類,bool_ 甚至不是一個(gè)number 類型。
在Python 3 中, int_ 不再繼承 Python3 中的int了,因?yàn)閕nt不再是一個(gè)固定長度的整數(shù)。
NumPy 默認(rèn)的數(shù)據(jù)類型是 float_。
可變長度數(shù)據(jù)類型
下面的三種數(shù)據(jù)類型長度是可變的,
| bytes_ | compatible: Python bytes | 'S#' |
| unicode_ | compatible: Python unicode/str | 'U#' |
| void | 'V#' |
字符代碼中的 # 表示的是數(shù)字。
上面描述的字符代碼,為了和Python的其他模塊進(jìn)行兼容,比如struct ,需要進(jìn)行下面適當(dāng)?shù)男拚?#xff1a;
c -> S1, b -> B, 1 -> b, s -> h, w -> H, 和 u -> I.
本文已收錄于 http://www.flydean.com/03-python-numpy-scalar/
最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發(fā)現(xiàn)!
歡迎關(guān)注我的公眾號(hào):「程序那些事」,懂技術(shù),更懂你!
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的NumPy之:标量scalars的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NumPy之:数据类型
- 下一篇: ECMAScript 2019(ES10