Python Numpy data-type dtype 自定義資料型別
阿新 • • 發佈:2019-01-07
一、例項
>>> dt = np.dtype('>i4') 定義一個big-endian int 4*8=32位的資料型別
>>> dt
dtype('>i4')
>>> dt.byteorder //位元組順序:>為big-edian <為little-endian
'>'
>>> dt.itemsize //位元組大小
4
>>> dt.name //dt型別
'int32'
>>> dt.type is np.int32
True
自定義資料型別:
定義dt:
>>> dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))]) //定義一個數據型別,其中name為16為字串,grades為2個float64的子陣列
>>> dt['name']
dtype('<U16')
>>> dt['grades']
dtype(('<f8',(2,)))
使用:
>>> x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
>>> x[1]
('John', [6.0, 7.0])
>>> x[1 ]['grades']
array([ 6., 7.])
>>> type(x[1])
<type 'numpy.void'>
>>> type(x[1]['grades'])
<type 'numpy.ndarray'>
二、dtype引數
在宣告資料型別時dtype能夠自動將引數轉為相應型別。預設資料型別為float_。
24個內建引數:
24內建Array-scalar types
>>> dt = np.dtype(np.int32) # 32位int,注意32為位
>>> dt = np.dtype (np.complex128) # 128位複數
numpy.sctypeDict.keys()引數:
存在於numpy.sctypeDict.keys()中的字串引數:
>>> dt = np.dtype('uint32') # 32位uint,注意32為位
>>> dt = np.dtype('Float64') # 64位float
python型別引數:
Tables | Are |
---|---|
int | int_ |
bool | bool_ |
float | float_ |
complex | cfloat |
str | string |
unicode | unicode_ |
buffer | void |
(all others) | object_ |
>>> dt = np.dtype(float) # Python的浮點
>>> dt = np.dtype(int) # Python的整型
>>> dt = np.dtype(object) # Python的物件
簡略字元引數:
'b' boolean
'i' (signed) integer
'u' unsigned integer
'f' floating-point
'c' complex-floating point
'm' timedelta
'M' datetime
'O' (Python) objects
'S', 'a' (byte-)string
'U' Unicode
'V' raw data (void)
>>> dt = np.dtype('f8') # 64位浮點,注意8為位元組
>>> dt = np.dtype('c16') # 128位複數
帶逗號字串引數:
>>> dt = np.dtype("a3, 3u8, (3,4)a10") //3位元組字串、3個64位整型子陣列、3*4的10位元組字串陣列,注意8為位元組
其他:
(flexible_dtype, itemsize)第一個引數型別引數大小不固定,第二傳入大小:
>>> dt = np.dtype((void, 10)) #10位
>>> dt = np.dtype((str, 35)) # 35字元字串
>>> dt = np.dtype(('U', 10)) # 10字元unicode string
(fixed_dtype, shape)第一個引數傳入固定大小引數,第二引數傳入個數
>>> dt = np.dtype((np.int32, (2,2))) # 2*2int子陣列
>>> dt = np.dtype(('S10', 1)) # 10字元字串
>>> dt = np.dtype(('i4, (2,3)f8, f4', (2,3))) # 2x3結構子陣列
[(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': ['r','g','b','a'],'formats': [uint8, uint8, uint8, uint8]})
>>> dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})
{‘field1’: …, ‘field2’: …, …}:
不推薦使用,可能會產生衝突
>>> dt = np.dtype({'col1': ('S10', 0), 'col2': (float32, 10),'col3': (int, 14)}) //col1在位元組0處,col2在位元組10處,col3在位元組14處
(base_dtype, new_dtype):
>>> dt = np.dtype((np.int32,{'real':(np.int16, 0),'imag':(np.int16, 2)}) //base_dtype前兩個位元組放置real,後兩個位元組放置imag
>>>dt = np.dtype((np.int32, (np.int8, 4))) //base_dtype被分成4個int8的子陣列
三、切換型別
使用astype,不可直接更改物件的dtype值
>>> b = np.array([1., 2., 3., 4.])
>>> b.dtype
dtype(‘float64‘)
>>> c = b.astype(int)
>>> c
array([1, 2, 3, 4])
>>> c.shape
(8,)
>>> c.dtype
dtype(‘int32‘)
>>> b
array([ 1., 2., 3., 4.])
>>> b.dtype = ‘int‘
>>> b.dtype
dtype(‘int32‘)
>>> b
array([0, 1072693248,0, 1073741824,0,1074266112, 0, 1074790400]) //陣列長度加倍
>>> b.shape
(8,)
檢視型別取值範圍:
np.finfo(np.float32)