Numpy資料型別物件(dtype)

2021-08-21 15:17:22 字數 2953 閱讀 8259

#記住引入numpy時要是用別名np,則所有的numpy字樣都要替換

#查詢數值型別

>>>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

# str 屬性獲取完整字串表示

# 第乙個字元是位元組序,< 表示小端,> 表示大端,| 表示平台的位元組序

>>> t.str

'>> t.itemsize

8# 許多函式擁有 dtype 引數

# 傳入數值型別、字元**和 dtype 都可以

>>> arange(7, dtype=uint16)

array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)

型別

字元**

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

stra, s(可以在s後面新增數字,表示字串長度,比如s3表示長度為三的字串,不寫則為最大長度)

unicode

uobject

ovoid

v基本書寫格式

import numpy

#定義t的各個字段型別

>>> t = dtype([('name', str, 40), ('numitems', numpy.int32), ('price',numpy.float32)])

>>> t

dtype([('name', '|s40'), ('numitems', '>> t['name']

dtype('|s40')

# 使用記錄型別建立陣列

# 否則它會把記錄拆開

>>> itemz = array([('meaning of life ***', 42, 3.14), ('butter', 13,2.72)], dtype=t)

>>> itemz[1]

('butter', 13, 2.7200000286102295)

#再舉個例*

>>>adt = np.dtype("a3, 3u8, (3,4)a10") #3位元組字串、3個64位整型子陣列、3*4的10位元組字串陣列,注意8為位元組

>>>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)第乙個大小不固定的引數型別,第二傳入大小:

>>> 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子陣列

舉例: >>>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結構子陣列

#[(field_name, field_dtype, field_shape), …]

>>> dt = np.dtype([('big', '>i4'), ('little', '>> dt = np.dtype([('r','u1'), ('g','u1'), ('b','u1'), ('a','u1')])

#:>>> dt= np.dtype()

>>> dt = np.dtype()

#(base_dtype, new_dtype):

>>>dt = np.dtype((np.int32, (np.int8, 4))) //base_dtype被分成4個int8的子陣列

NumPy 資料型別

numpy 支援比 python 更多種類的數值型別。下表顯示了 numpy 中定義的不同標量資料型別。序號資料型別及描述 1.bool 儲存為乙個位元組的布林值 真或假 2.int 預設整數,相當於 c 的long,通常為int32或int64 3.intc相當於 c 的int,通常為int32或...

NumPy 資料型別

numpy提供的數值型別,數值範圍比python提供的數值型別更大。numpy的數值型別,如下表所示 sn資料型別描述1 bool 布林值,取值ture false,占用乙個位元組 2int 是integer的預設型別。與c語言中的long型別相同,有可能是64位或32位。3intc 類似於c語言中...

Numpy資料型別

numpy是python的一種開源的數值計算擴充套件,是一些針對矩陣進行運算的模組。1.numpy介紹 2.numpy 學習筆記 3.python中的list和array的不同之處 4.python列表 numpy陣列與矩陣的區別 1.python中的list和np.array的不同之處 numpy...