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

相关文章

基于python3 的百度图片下载器的实现代码

自己写了玩的一个小脚本,百度图片下载 import re import os import requests import hashlib def dowmloadPic(h...

python读取html中指定元素生成excle文件示例

Python2.7编写的读取html中指定元素,并生成excle文件 复制代码 代码如下:#coding=gbkimport stringimport codecsimport os,t...

python3 shelve模块的详解

python3 shelve模块的详解 一、简介   在python3中我们使用json或者pickle持久化数据,能dump多次,但只能load一次,因为先前的数据已经被后面dump的...

Python 含参构造函数实例详解

本篇博文主要介绍在Python3中如何构造含参构造函数 样例如下: class MyOdlHttp: username = '' password = '' def _...

Python3常用内置方法代码实例

这篇文章主要介绍了Python3常用内置方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 max()/min() 传...