關(guān)于Numpy數(shù)據(jù)類型對(duì)象(dtype)使用詳解
常用方法
#記住引入numpy時(shí)要是用別名np,則所有的numpy字樣都要替換
#查詢數(shù)值類型
>>>type(float)
dtype('float64')
# 查詢字符代碼
>>> dtype('f')
dtype('float32')
>>> dtype('d')
dtype('float64')
# 查詢雙字符代碼
>>> dtype('f8')
dtype('float64')
# 獲取所有字符代碼
>>> sctypeDict.keys()
[0, … 'i2', 'int0']
# char 屬性用來獲取字符代碼
>>> t = dtype('Float64')
>>> t.char
'd'
# type 屬性用來獲取類型
>>> t.type
<type 'numpy.float64'>
# str 屬性獲取完整字符串表示
# 第一個(gè)字符是字節(jié)序,< 表示小端,> 表示大端,| 表示平臺(tái)的字節(jié)序
>>> t.str
'<f8'
# 獲取大小
>>> t.itemsize
8
# 許多函數(shù)擁有 dtype 參數(shù)
# 傳入數(shù)值類型、字符代碼和 dtype 都可以
>>> arange(7, dtype=uint16)
array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
類型參數(shù)及縮寫
| 類型 | 字符代碼 |
|---|---|
| bool | ?, b1 |
| int8 | b, i1 |
| uint8 | B, u1 |
| int16 | h, i2 |
| uint16 | H, u2 |
| int32 | i, i4 |
| uint32 | I, u4 |
| int64 | q, i8 |
| uint64 | Q, u8 |
| float16 | f2, e |
| float32 | f4, f |
| float64 | f8, d |
| complex64 | F4, F |
| complex128 | F8, D |
| str | a, S(可以在S后面添加數(shù)字,表示字符串長度,比如S3表示長度為三的字符串,不寫則為最大長度) |
| unicode | U |
| object | O |
| void | V |
自定義異構(gòu)數(shù)據(jù)類型
基本書寫格式
import numpy
#定義t的各個(gè)字段類型
>>> t = dtype([('name', str, 40), ('numitems', numpy.int32), ('price',numpy.float32)])
>>> t
dtype([('name', '|S40'), ('numitems', '<i4'), ('price','<f4')])
# 獲取字段類型
>>> t['name']
dtype('|S40')
# 使用記錄類型創(chuàng)建數(shù)組
# 否則它會(huì)把記錄拆開
>>> itemz = array([('Meaning of life DVD', 42, 3.14), ('Butter', 13,2.72)], dtype=t)
>>> itemz[1]
('Butter', 13, 2.7200000286102295)
#再舉個(gè)例*
>>>adt = np.dtype("a3, 3u8, (3,4)a10") #3字節(jié)字符串、3個(gè)64位整型子數(shù)組、3*4的10字節(jié)字符串?dāng)?shù)組,注意8為字節(jié)
>>>itemz = np.array([('Butter',[13,2,3],[['d','o','g','s'],['c','a','t','s'],['c','o','w','s']])],dtype=adt)
>>>itemz
(b'But', [13, 2, 3], [[b'd', b'o', b'g', b's'], [b'c', b'a', b't', b's'], [b'c', b'o', b'w', b's']])
其他書寫格式
#(flexible_dtype, itemsize)第一個(gè)大小不固定的參數(shù)類型,第二傳入大?。?
>>> dt = np.dtype((void, 10)) #10位
>>> dt = np.dtype((str, 35)) # 35字符字符串
>>> dt = np.dtype(('U', 10)) # 10字符unicode string
#(fixed_dtype, shape)第一個(gè)傳入固定大小的類型參數(shù),第二參數(shù)傳入個(gè)數(shù)
>>> dt = np.dtype((np.int32, (2,2))) # 2*2int子數(shù)組
舉例: >>>item = np.array([([12,12],[55,56])], dtype=dt)
array([[12, 12], [55, 56]])
>>> dt = np.dtype(('S10', 1)) # 10字符字符串
>>> dt = np.dtype(('i4, (2,3)f8, f4', (2,3))) # 2*3結(jié)構(gòu)子數(shù)組
#[(field_name, field_dtype, field_shape), …]
>>> dt = np.dtype([('big', '>i4'), ('little', '<i4')])
>>> dt = np.dtype([('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')])
#{‘names': …, ‘formats': …, ‘offsets': …, ‘titles': …, ‘itemsize': …}:
>>> dt= np.dtype({'names':('Date','Close'),'formats':('S10','f8')})
>>> dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})
#(base_dtype, new_dtype):
>>>dt = np.dtype((np.int32, (np.int8, 4))) //base_dtype被分成4個(gè)int8的子數(shù)組
以上這篇關(guān)于Numpy數(shù)據(jù)類型對(duì)象(dtype)使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python中的Numpy入門教程
- 在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
- python中numpy.zeros(np.zeros)的使用方法
- Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
- Python numpy 提取矩陣的某一行或某一列的實(shí)例
- 詳解Numpy數(shù)組轉(zhuǎn)置的三種方法T、transpose、swapaxes
- Numpy數(shù)據(jù)類型轉(zhuǎn)換astype,dtype的方法
- numpy中的delete刪除數(shù)組整行和整列的實(shí)例
- 淺談numpy數(shù)組的幾種排序方式
- 簡單快捷:NumPy入門教程的環(huán)境設(shè)置
相關(guān)文章
Python MongoDB 插入數(shù)據(jù)時(shí)已存在則不執(zhí)行,不存在則插入的解決方法
這篇文章主要介紹了Python MongoDB 插入數(shù)據(jù)時(shí)已存在則不執(zhí)行,不存在則插入的解決方法,結(jié)合實(shí)例形式分析了Python基于日志判斷數(shù)據(jù)是否已經(jīng)插入的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
Python守護(hù)進(jìn)程(daemon)代碼實(shí)例
這篇文章主要介紹了Python守護(hù)進(jìn)程(daemon)代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-03-03
python實(shí)現(xiàn)網(wǎng)頁自動(dòng)簽到功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)網(wǎng)頁自動(dòng)簽到功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
使用Python實(shí)現(xiàn)簡單的人臉識(shí)別功能(附源碼)
Python中實(shí)現(xiàn)人臉識(shí)別功能有多種方法,依賴于python膠水語言的特性,我們通過調(diào)用包可以快速準(zhǔn)確的達(dá)成這一目的,本文給大家分享使用Python實(shí)現(xiàn)簡單的人臉識(shí)別功能的操作步驟,感興趣的朋友一起看看吧2021-12-12
使用python庫xlsxwriter庫來輸出各種xlsx文件的示例
這篇文章主要介紹了使用python庫xlsxwriter庫來輸出各種xlsx文件的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
python模擬預(yù)測一下新型冠狀病毒肺炎的數(shù)據(jù)
這篇文章主要介紹了python模擬預(yù)測一下新型冠狀病毒肺炎的數(shù)據(jù) ,需要的朋友可以參考下2020-02-02
CentOS系統(tǒng)上安裝Conda的詳細(xì)指南
Conda 是一個(gè)開源的包管理系統(tǒng)和環(huán)境管理系統(tǒng),廣泛應(yīng)用于數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)領(lǐng)域,本文將詳細(xì)介紹如何在 CentOS 系統(tǒng)上安裝 Conda吧2025-03-03

