Numpy 數(shù)據(jù)類型
本小節(jié)將詳述 Numpy 內(nèi)置的數(shù)據(jù)類型,以及如何在創(chuàng)建數(shù)組對(duì)象時(shí)進(jìn)行靈活指定、如何查看創(chuàng)建好的數(shù)組類型、以及如何更改數(shù)據(jù)類型。
1. 常見數(shù)據(jù)類型
Numpy 支持的數(shù)據(jù)類型比 Python 內(nèi)置的要多很多,而這也是 Numpy 如此靈活和強(qiáng)大的原因之一。
例如對(duì)于整數(shù),在 Numpy 中,根據(jù)整數(shù)的位數(shù)不同所需要占據(jù)的空間大小不同,又對(duì)整數(shù)類型進(jìn)行類細(xì)分,常見地可以分為 int8、int16、int32、int64 等等。
Numpy 支持的常用數(shù)據(jù)類型如下:
類型 | 說明 |
---|---|
int8 、uint8 |
分別表示有符號(hào)和無符號(hào)的8位整型,可表示的整數(shù)范圍為-128 ~ 127、0 ~ 255 |
int16 、uint16 |
分別表示有符號(hào)和無符號(hào)的16位整型,可表示的整數(shù)范圍為-32768 ~ 32767、0 ~ 65535 |
int32 、uint32 |
分別表示有符號(hào)和無符號(hào)的32位整型,可表示的整數(shù)范圍為-2147483648 ~ 2147483647、0 ~ 4294967295 |
int64 、uint64 |
分別表示有符號(hào)和無符號(hào)的64位整型,可表示的整數(shù)范圍為-9223372036854775808 ~ 9223372036854775807、0 ~ 18446744073709551615 |
float16 、float32 、float64 、float128 |
分別表示半精度浮點(diǎn)數(shù)、單精度浮點(diǎn)、雙精度浮點(diǎn)、擴(kuò)展精度浮點(diǎn)數(shù) |
complex64 、complex128 、complex256 |
分別用兩個(gè)32位、64位、128位的浮點(diǎn)數(shù)表示的復(fù)數(shù) |
bool |
存儲(chǔ)True和False值的布爾類型 |
Object |
Python對(duì)象類型 |
string_ |
類型代號(hào)S,固定長(zhǎng)度的字符串類型,每個(gè)字符1個(gè)字節(jié)。例如,如果需要?jiǎng)?chuàng)建一個(gè)長(zhǎng)度為10的字符串,應(yīng)使用S10 。 |
unicode_ |
類型代號(hào)U,固定長(zhǎng)度的unicode類型,跟字符串的定義方式一樣,例如(U8 ) |
2. 定義數(shù)據(jù)類型
dtype 是一個(gè)特殊的對(duì)象,在該對(duì)象中定義了 ndarray 的數(shù)據(jù)類型與數(shù)據(jù)大小。通常我們?cè)趧?chuàng)建 ndarray 的時(shí)候,可以顯示地利用 dtype 定義數(shù)組的細(xì)節(jié)信息。
案例
在創(chuàng)建數(shù)組對(duì)象的時(shí)候,通過給 dtype 賦值,顯式地定義一個(gè) int8 類型的數(shù)組。
import numpy as np
arr0 = np.array([[1,2,3], [4,5,6]], dtype=np.int8)
arr0
Out:
array([[1, 2, 3],
[4, 5, 6]], dtype=int8)
在創(chuàng)建數(shù)組對(duì)象的時(shí)候,通過給 dtype 賦值,顯式地定義一個(gè) float16 類型的數(shù)組
arr1 = np.array([[1.2, -2.4, 3.1], [-4, 5.9, -6.7]], dtype=np.float16)
arr1
Out:
array([[ 1.2, -2.4, 3.1],
[-4. , 5.9, -6.7]], dtype=float16)
在創(chuàng)建數(shù)組對(duì)象的時(shí)候,通過給 dtype 賦值,顯式地定義一個(gè) S2 字符串類型的數(shù)組
arr2 = np.array(list('ABCD'), ndmin=2, dtype='S2')
arr2
Out:
array([[b'A', b'B', b'C', b'D']], dtype='|S2')
在創(chuàng)建數(shù)組對(duì)象的時(shí)候,通過給 dtype 賦值,顯式地定義布爾類型的數(shù)組
arr3 = np.array([[-1,0,-3], [4,-5,6]], dtype=np.bool)
arr3
Out:
array([[ True, False, True],
[ True, True, True]])
3. 查詢數(shù)據(jù)類型
同樣的,對(duì)于已經(jīng)定義好數(shù)組,也可以通過調(diào)用 dtype 對(duì)象,查看其數(shù)據(jù)類型。
案例
逐個(gè)查看上面定義的數(shù)組的數(shù)據(jù)類型:
arr0.dtype
Out:
dtype('int8')
arr1.dtype
Out:
dtype('float16')
arr2.dtype
Out:
dtype('S2')
arr3.dtype
Out:
dtype('bool')
4. 更改數(shù)據(jù)類型
對(duì)于已經(jīng)定義好的數(shù)組,也可以通過 ndarray 的 astype 方法對(duì) dtype 對(duì)象進(jìn)行修改。
案例
將整數(shù)類型的數(shù)組轉(zhuǎn)換成浮點(diǎn)數(shù)類型,并查看轉(zhuǎn)換之后的結(jié)果:
float_arr0 = arr0.astype(np.float)
float_arr0.dtype
Out:
dtype('float64')
可以看到,在我們不指定具體的浮點(diǎn)數(shù)精度的時(shí)候,Numpy 也會(huì)自動(dòng)去推斷一個(gè)合理的類型(當(dāng)然,很多時(shí)候并不是最優(yōu)的類型,因此在處理大數(shù)據(jù)集時(shí),建議定義完整)。
如果把浮點(diǎn)數(shù)轉(zhuǎn)換成整數(shù),則小數(shù)部分將會(huì)被截?cái)嗌釛墶?/p>
int_arr1 = arr1.astype(np.int8)
int_arr1
array([[ 1, -2, 3],
[-4, 5, -6]], dtype=int8)
對(duì)于字符串類型的數(shù)組,在某些情況是可以與浮點(diǎn)數(shù)進(jìn)行轉(zhuǎn)換的,例如:
string_arr4 = np.array([['1.2', '2.8'], ['4.3', '5.7']], dtype=np.string_)
string_arr4.astype(np.float16)
Out:
array([[1.2, 2.8],
[4.3, 5.7]], dtype=float16)
需要注意的是,astype
并非全能,在某些情況下,如果轉(zhuǎn)換過程失敗,則會(huì)引發(fā)一個(gè)ValueError
。例如:
string_arr4.astype(np.int16)
ValueError Traceback (most recent call last)
<ipython-input-25-787558dcde3c> in <module>
----> 1 string_arr4.astype(np.int16)
ValueError: invalid literal for int() with base 10: '1.2'
通過觀察上面的案例,我們會(huì)發(fā)現(xiàn),利用 astype 方法會(huì)創(chuàng)造出一個(gè)新的數(shù)組。確實(shí) astype 方法并不能在原有的數(shù)組上進(jìn)行修改,因此如果你想保存改變后的數(shù)組,務(wù)必記得給它賦個(gè)值。
5. 小結(jié)
本小節(jié)詳細(xì)講述了 Numpy 內(nèi)置的數(shù)據(jù)類型,以及如何定義、查詢、更改數(shù)組的數(shù)據(jù)類型。需要注意的是,在利用 astype 方法更改數(shù)據(jù)類型的時(shí)候,并不是 100% 成功的。