关于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设计】。

相关文章

flask-socketio实现WebSocket的方法

【flask-socektio】 之前不知道在哪个场合下提到过如何从web后台向前台推送消息。听闻了反向ajax技术这种模式之后,大呼神奇,试了一下之后发现也确实可以用。不过,反向aj...

Python isinstance函数介绍

isinstance(object, classinfo) 判断实例是否是这个类或者object object是变量   classinfo 是类型(tuple,d...

使用虚拟环境打包python为exe 文件的方法

使用过anaconda环境下打包py文件的一点感悟,使用的是pyinstaller+anaconda环境下打包py文件 打包: pyinstaller -F -w -i logo.ico...

用实例分析Python中method的参数传递过程

什么是method? function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值。所有的参数都是明确的传递过去的。 method是function与对象的结合。我...

python过滤字符串中不属于指定集合中字符的类实例

本文实例讲述了python过滤字符串中不属于指定集合中字符的类。分享给大家供大家参考。具体如下: # -*- coding: utf-8 -*- import sets class...