>>type(float) dtype( float64 ) # 查詢字符代碼 >>> dtype( f ) dtype(..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

關于Numpy數(shù)據(jù)類型對象(dtype)使用詳解

發(fā)布時間:2020-08-20 07:20:07 來源:腳本之家 閱讀:327 作者:EvanForEver 欄目:開發(fā)技術

常用方法

#記住引入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

自定義異構數(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é)字符串數(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結構子數(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ù)組

以上這篇關于Numpy數(shù)據(jù)類型對象(dtype)使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI