關(guān)于Numpy數(shù)據(jù)類型對象(dtype)使用詳解
更新時間:2019年11月27日 10:29:44 作者:EvanForEver
今天小編就為大家分享一篇關(guān)于Numpy數(shù)據(jù)類型對象(dtype)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
常用方法
#記住引入numpy時要是用別名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 屬性獲取完整字符串表示 # 第一個字符是字節(jié)序,< 表示小端,> 表示大端,| 表示平臺的字節(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的各個字段類型 >>> 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ù)組 # 否則它會把記錄拆開 >>> itemz = array([('Meaning of life DVD', 42, 3.14), ('Butter', 13,2.72)], dtype=t) >>> itemz[1] ('Butter', 13, 2.7200000286102295) #再舉個例* >>>adt = np.dtype("a3, 3u8, (3,4)a10") #3字節(jié)字符串、3個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)第一個大小不固定的參數(shù)類型,第二傳入大?。? >>> dt = np.dtype((void, 10)) #10位 >>> dt = np.dtype((str, 35)) # 35字符字符串 >>> dt = np.dtype(('U', 10)) # 10字符unicode string #(fixed_dtype, shape)第一個傳入固定大小的類型參數(shù),第二參數(shù)傳入個數(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個int8的子數(shù)組
以上這篇關(guān)于Numpy數(shù)據(jù)類型對象(dtype)使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python中的Numpy入門教程
- 在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
- python中numpy.zeros(np.zeros)的使用方法
- Python使用numpy產(chǎn)生正態(tài)分布隨機數(shù)的向量或矩陣操作示例
- Python numpy 提取矩陣的某一行或某一列的實例
- 詳解Numpy數(shù)組轉(zhuǎn)置的三種方法T、transpose、swapaxes
- Numpy數(shù)據(jù)類型轉(zhuǎn)換astype,dtype的方法
- numpy中的delete刪除數(shù)組整行和整列的實例
- 淺談numpy數(shù)組的幾種排序方式
- 簡單快捷:NumPy入門教程的環(huán)境設(shè)置
相關(guān)文章
Python基礎(chǔ)之pandas數(shù)據(jù)合并
這篇文章主要介紹了Python基礎(chǔ)之pandas數(shù)據(jù)合并,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04python學(xué)習(xí)VSCode使用技巧帶你進入高效開發(fā)模式
VsCode是一款開源的編輯器,擁有強大的功能,。由于擁有各種各樣的插件,這就使得VsCode可以做到的事情更多了。在使用的過程中,也是有很多技巧的,掌握一些技巧對于后期寫代碼也會輕松很多2021-09-09python可視化分析的實現(xiàn)(matplotlib、seaborn、ggplot2)
這篇文章主要介紹了python可視化分析的實現(xiàn)(matplotlib、seaborn、ggplot2),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python使用pydub庫對mp3與wav格式進行互轉(zhuǎn)的方法
今天小編就為大家分享一篇Python使用pydub庫對mp3與wav格式進行互轉(zhuǎn)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01