NumPy 支持比 Python 更多種類的數(shù)值類型。 下表顯示了 NumPy 中定義的不同標(biāo)量數(shù)據(jù)類型。
| 序號 | 數(shù)據(jù)類型及描述 |
|---|---|
| 1. | bool_存儲為一個字節(jié)的布爾值(真或假) |
| 2. | int_默認(rèn)整數(shù),相當(dāng)于 C 的long,通常為int32或int64 |
| 3. | intc相當(dāng)于 C 的int,通常為int32或int64 |
| 4. | intp用于索引的整數(shù),相當(dāng)于 C 的size_t,通常為int32或int64 |
| 5. | int8字節(jié)(-128 ~ 127) |
| 6. | int1616 位整數(shù)(-32768 ~ 32767) |
| 7. | int3232 位整數(shù)(-2147483648 ~ 2147483647) |
| 8. | int6464 位整數(shù)(-9223372036854775808 ~ 9223372036854775807) |
| 9. | uint88 位無符號整數(shù)(0 ~ 255) |
| 10. | uint1616 位無符號整數(shù)(0 ~ 65535) |
| 11. | uint3232 位無符號整數(shù)(0 ~ 4294967295) |
| 12. | uint6464 位無符號整數(shù)(0 ~ 18446744073709551615) |
| 13. | float_float64的簡寫 |
| 14. | float16半精度浮點:符號位,5 位指數(shù),10 位尾數(shù) |
| 15. | float32單精度浮點:符號位,8 位指數(shù),23 位尾數(shù) |
| 16. | float64雙精度浮點:符號位,11 位指數(shù),52 位尾數(shù) |
| 17. | complex_complex128的簡寫 |
| 18. | complex64復(fù)數(shù),由兩個 32 位浮點表示(實部和虛部) |
| 19. | complex128復(fù)數(shù),由兩個 64 位浮點表示(實部和虛部) |
NumPy 數(shù)字類型是dtype(數(shù)據(jù)類型)對象的實例,每個對象具有唯一的特征。 這些類型可以是np.bool_,np.float32等。
數(shù)據(jù)類型對象描述了對應(yīng)于數(shù)組的固定內(nèi)存塊的解釋,取決于以下方面:
數(shù)據(jù)類型(整數(shù)、浮點或者 Python 對象)
數(shù)據(jù)大小
字節(jié)序(小端或大端)
在結(jié)構(gòu)化類型的情況下,字段的名稱,每個字段的數(shù)據(jù)類型,和每個字段占用的內(nèi)存塊部分。
如果數(shù)據(jù)類型是子序列,它的形狀和數(shù)據(jù)類型。
字節(jié)順序取決于數(shù)據(jù)類型的前綴<或>。<意味著編碼是小端(最小有效字節(jié)存儲在最小地址中)。>意味著編碼是大端(最大有效字節(jié)存儲在最小地址中)。
dtype可由一下語法構(gòu)造:
numpy.dtype(object, align, copy)
參數(shù)為:
Object:被轉(zhuǎn)換為數(shù)據(jù)類型的對象。
Align:如果為true,則向字段添加間隔,使其類似 C 的結(jié)構(gòu)體。
Copy? 生成dtype對象的新副本,如果為flase,結(jié)果是內(nèi)建數(shù)據(jù)類型對象的引用。
# 使用數(shù)組標(biāo)量類型 import numpy as np dt = np.dtype(np.int32) print dt
輸出如下:
int32
#int8,int16,int32,int64 可替換為等價的字符串 'i1','i2','i4',以及其他。
import numpy as np
dt = np.dtype('i4')
print dt
輸出如下:
int32
# 使用端記號
import numpy as np
dt = np.dtype('>i4')
print dt
輸出如下:
>i4
下面的例子展示了結(jié)構(gòu)化數(shù)據(jù)類型的使用。 這里聲明了字段名稱和相應(yīng)的標(biāo)量數(shù)據(jù)類型。
# 首先創(chuàng)建結(jié)構(gòu)化數(shù)據(jù)類型。
import numpy as np
dt = np.dtype([('age',np.int8)])
print dt
輸出如下:
[('age', 'i1')]
# 現(xiàn)在將其應(yīng)用于 ndarray 對象
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a
輸出如下:
[(10,) (20,) (30,)]
# 文件名稱可用于訪問 age 列的內(nèi)容
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a['age']
輸出如下:
[10 20 30]
以下示例定義名為 student 的結(jié)構(gòu)化數(shù)據(jù)類型,其中包含字符串字段name,整數(shù)字段age和浮點字段marks。 此dtype應(yīng)用于ndarray對象。
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print student
輸出如下:
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print a
輸出如下:
[('abc', 21, 50.0), ('xyz', 18, 75.0)]
每個內(nèi)建類型都有一個唯一定義它的字符代碼:
'b':布爾值
'i':符號整數(shù)
'u':無符號整數(shù)
'f':浮點
'c':復(fù)數(shù)浮點
'm':時間間隔
'M':日期時間
'O':Python 對象
'S', 'a':字節(jié)串
'U':Unicode
'V':原始數(shù)據(jù)(void)