numpy dtype object_c怎么判断数据类型

numpy dtype object_c怎么判断数据类型常用方法1#记住引入numpy时要是用别名np,则所有的numpy字样都要替换2#查询数值类型3>>>type(float)4dtype('float64')5#查询字符代码6>>>dtype('f')

大家好,欢迎来到IT知识分享网。

常用方法

 1 #记住引入numpy时要是用别名np,则所有的numpy字样都要替换
 2  #查询数值类型
 3 >>>type(float)
 4 dtype('float64')
 5 # 查询字符代码
 6 >>> dtype('f')
 7 dtype('float32')
 8 >>> dtype('d')
 9 dtype('float64')
10 # 查询双字符代码
11 >>> dtype('f8')
12 dtype('float64')
13 # 获取所有字符代码
14 >>> sctypeDict.keys()
15 [0, … 'i2', 'int0']
16  
17 # char 属性用来获取字符代码
18 >>> t = dtype('Float64')
19 >>> t.char
20 'd'
21 # type 属性用来获取类型
22 >>> t.type
23 <type 'numpy.float64'>
24  
25 # str 属性获取完整字符串表示
26 # 第一个字符是字节序,< 表示小端,> 表示大端,| 表示平台的字节序
27 >>> t.str
28 '<f8'
29  
30 # 获取大小
31 >>> t.itemsize
32 8
33  
34 # 许多函数拥有 dtype 参数
35 # 传入数值类型、字符代码和 dtype 都可以
36 >>> arange(7, dtype=uint16)
37 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
str a, S(可以在S后面添加数字,表示字符串长度,比如S3表示长度为三的字符串,不写则为最大长度)
unicode U
object O
void V

基本书写格式

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')
 
# 使用记录类型创建数组
# 否则它会把记录拆开
>>> 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字节字符串、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']])

其他书写格式

 1 #(flexible_dtype, itemsize)第一个大小不固定的参数类型,第二传入大小:
 2 >>> dt = np.dtype((void, 10)) #10位
 3 >>> dt = np.dtype((str, 35))  # 35字符字符串
 4 >>> dt = np.dtype(('U', 10))  # 10字符unicode string
 5  
 6 #(fixed_dtype, shape)第一个传入固定大小的类型参数,第二参数传入个数
 7 >>> dt = np.dtype((np.int32, (2,2)))     # 2*2int子数组
 8 举例: >>>item = np.array([([12,12],[55,56])], dtype=dt)
 9 array([[12, 12], [55, 56]])
10 >>> dt = np.dtype(('S10', 1))         # 10字符字符串
11 >>> dt = np.dtype(('i4, (2,3)f8, f4', (2,3))) # 2*3结构子数组
12  
13 #[(field_name, field_dtype, field_shape), …]
14 >>> dt = np.dtype([('big', '>i4'), ('little', '<i4')])
15 >>> dt = np.dtype([('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')])
16  
17 #{‘names': …, ‘formats': …, ‘offsets': …, ‘titles': …, ‘itemsize': …}:
18 >>> dt= np.dtype({'names':('Date','Close'),'formats':('S10','f8')})
19 >>> dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})
20  
21 #(base_dtype, new_dtype):
22 >>>dt = np.dtype((np.int32, (np.int8, 4))) //base_dtype被分成4个int8的子数组

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/28272.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信