关于Numpy数据类型对象(dtype)使用详解

yipeiwu_com6年前Python基础

常用方法

#记住引入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
<type 'numpy.float64'>
 
# str 属性获取完整字符串表示
# 第一个字符是字节序,< 表示小端,> 表示大端,| 表示平台的字节序
>>> t.str
'<f8'
 
# 获取大小
>>> 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
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']])

其他书写格式

#(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', '<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的子数组

以上这篇关于Numpy数据类型对象(dtype)使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现定时压缩指定文件夹发送邮件

工作中每天需要收集部门内的FR文件,发送给外部部门的同事帮忙上传,这么发了有大半年,昨天亮光一闪,为什么不做成自动化呢,于是用python实现了整个流程,今天体验了一下真是美滋滋。 代码...

Python检测字符串中是否包含某字符集合中的字符

目的   检测字符串中是否包含某字符集合中的字符 方法   最简洁的方法如下,清晰,通用,快速,适用于任何序列和容器 复制代码 代码如下: def containAny(seq,ase...

通过Python模块filecmp 对文件比较的实现方法

filecmp定义了两个函数,用于方便地比较文件与文件夹:     filecmp.cmp(f1, f2[, shallow]):  比较两个文件...

详细分析python3的reduce函数

详细分析python3的reduce函数

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。 官方文档是这样介绍的 reduce(...) reduce(fu...

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

今天在网上copy的一段代码,代码很简单,每行看起来该缩进的都缩进了,运行的时候出现了如下错误:  【解决过程】  1.对于此错误,最常见的原因是,的确没有缩进。...