咱們知道Python中有4種數字類型,分別是int,float,bool和complex。做爲科學計算的NumPy,其數據類型更加的豐富。python
今天給你們詳細講解一下NumPy中的數據類型。數組
NumPy是用C語言來實現的,咱們能夠對標一下NumPy中數組中的數據類型跟C語言中的數據類型:ui
Numpy 中的類型 | C 中的類型 | 說明 |
---|---|---|
np.bool_ | bool |
Boolean (True or False) stored as a byte |
np.byte | signed char |
Platform-defined |
np.ubyte | unsigned char |
Platform-defined |
np.short | short |
Platform-defined |
np.ushort | unsigned short |
Platform-defined |
np.intc | int |
Platform-defined |
np.uintc | unsigned int |
Platform-defined |
np.int_ | long |
Platform-defined |
np.uint | unsigned long |
Platform-defined |
np.longlong | long long |
Platform-defined |
np.ulonglong | unsigned long long |
Platform-defined |
np.half / np.float16 | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa | |
np.single | float |
Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa |
np.double | double |
Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa. |
np.longdouble | long double |
Platform-defined extended-precision float |
np.csingle | float complex |
Complex number, represented by two single-precision floats (real and imaginary components) |
np.cdouble | double complex |
Complex number, represented by two double-precision floats (real and imaginary components). |
np.clongdouble | long double complex |
Complex number, represented by two extended-precision floats (real and imaginary components). |
咱們在Ipython環境中隨機查看一下上面的類型究竟是什麼:this
import numpy as np In [26]: np.byte Out[26]: numpy.int8 In [27]: np.bool_ Out[27]: numpy.bool_ In [28]: np.ubyte Out[28]: numpy.uint8 In [29]: np.short Out[29]: numpy.int16 In [30]: np.ushort Out[30]: numpy.uint16
因此上面的數據類型,其底層仍是固定長度的數據類型,咱們看下到底有哪些:code
Numpy 類型 | C 類型 | 說明 |
---|---|---|
np.int8 | int8_t |
Byte (-128 to 127) |
np.int16 | int16_t |
Integer (-32768 to 32767) |
np.int32 | int32_t |
Integer (-2147483648 to 2147483647) |
np.int64 | int64_t |
Integer (-9223372036854775808 to 9223372036854775807) |
np.uint8 | uint8_t |
Unsigned integer (0 to 255) |
np.uint16 | uint16_t |
Unsigned integer (0 to 65535) |
np.uint32 | uint32_t |
Unsigned integer (0 to 4294967295) |
np.uint64 | uint64_t |
Unsigned integer (0 to 18446744073709551615) |
np.intp | intptr_t |
Integer used for indexing, typically the same as ssize_t |
np.uintp | uintptr_t |
Integer large enough to hold a pointer |
np.float32 | float |
|
np.float64 / np.float_ | double |
Note that this matches the precision of the builtin python float. |
np.complex64 | float complex |
Complex number, represented by two 32-bit floats (real and imaginary components) |
np.complex128 / np.complex_ | double complex |
Note that this matches the precision of the builtin python complex. |
全部這些類型都是 dtype 對象的實例。經常使用的有5種基本類型,分別是bool,int,uint,float和complex。component
類型後面帶的數字表示的是該類型所佔的字節數。orm
上面表格中有一些 Platform-defined的數據類型,這些類型是跟平臺相關的,在使用的時候要特別注意。對象
這些dtype類型能夠在建立數組的時候手動指定:教程
>>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8)
因爲歷史緣由,爲了向下兼容,咱們也能夠在建立數組的時候指定字符格式的dtype。ip
>>> np.array([1, 2, 3], dtype='f') array([ 1., 2., 3.], dtype=float32)
上面的 f 表示的是float類型。
若是想要轉換一個現有的數組類型,可使用數組自帶的astype方法,也能夠調用np的強制轉換方法:
In [33]: z = np.arange(3, dtype=np.uint8) In [34]: z Out[34]: array([0, 1, 2], dtype=uint8) In [35]: z.astype(float) Out[35]: array([0., 1., 2.]) In [36]: np.int8(z) Out[36]: array([0, 1, 2], dtype=int8)
注意,上面咱們使用了 float , Python將會把float 自動替換成爲 np.float_,一樣的簡化格式還有int
==np.int_
,bool
==np.bool_
,complex
==np.complex_
. 其餘的數據類型不能使用簡化版本。
查看一個數組的數據類型可使用自帶的dtype屬性:
In [37]: z.dtype Out[37]: dtype('uint8')
dtype做爲一個對象,自己也能夠進行一些類型判斷操做:
>>> d = np.dtype(int) >>> d dtype('int32') >>> np.issubdtype(d, np.integer) True >>> np.issubdtype(d, np.floating) False
通常來講,若是超出了數據的範圍是會報異常的。好比咱們有一個很是長的int值:
In [38]: a= 1000000000000000000000000000000000000000000000000000000000000000000000000000000 In [39]: a Out[39]: 1000000000000000000000000000000000000000000000000000000000000000000000000000000 In [40]: np.int(1000000000000000000000000000000000000000000000000000000) Out[40]: 1000000000000000000000000000000000000000000000000000000 In [41]: np.int32(1000000000000000000000000000000000000000000000000000000) --------------------------------------------------------------------------- OverflowError Traceback (most recent call last) <ipython-input-41-71feb4433730> in <module>() ----> 1 np.int32(1000000000000000000000000000000000000000000000000000000)
上面的數字太長了,超出了int32的範圍,就會拋出異常。
可是NumPy的有些操做,若是超出範圍以後,並不會報異常,而是正常範圍,這時候咱們就須要注意了:
In [43]: np.power(100, 8, dtype=np.int32) Out[43]: 1874919424 In [44]: np.power(100, 8, dtype=np.int64) Out[44]: 10000000000000000
NumPy提供了兩個方法來測量int和float的範圍,numpy.iinfo 和 numpy.finfo :
In [45]: np.iinfo(int) Out[45]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) In [46]: np.iinfo(np.int32) Out[46]: iinfo(min=-2147483648, max=2147483647, dtype=int32) In [47]: np.iinfo(np.int64) Out[47]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
若是64位的int仍是過小的話,可使用np.float64,float64可使用科學計數法,因此可以獲得更大範圍的結果,可是其精度可能會縮小。
In [48]: np.power(100, 100, dtype=np.int64) Out[48]: 0 In [49]: np.power(100, 100, dtype=np.float64) Out[49]: 1e+200
本文已收錄於 http://www.flydean.com/02-python-numpy-datatype/
最通俗的解讀,最深入的乾貨,最簡潔的教程,衆多你不知道的小技巧等你來發現!
歡迎關注個人公衆號:「程序那些事」,懂技術,更懂你!