原文地址:

https://www.cnblogs.com/flydean/p/14720858.html

=====================================================

简介

之前讲到了NumPy中有多种数据类型,每种数据类型都是一个dtype(numpy.dtype )对象。今天我们来详细讲解一下dtype对象。

dtype的定义

先看下dtype方法的定义:

class numpy.dtype(obj, align=False, copy=False)

其作用就是将对象obj转成dtype类型的对象。

它带了两个可选的参数:

  • align - 是否按照C编译器的结构体输出格式对齐对象。

  • Copy - 是拷贝对象,还是对对象的引用。

dtype可以用来描述数据的类型(int,float,Python对象等),描述数据的大小,数据的字节顺序(小端或大端)等。

可转换为dtype的对象

可转换的obj对象可以有很多种类型,我们一一来进行讲解

dtype对象

如果obj对象本身就是一个dtype对象,那么可以进行无缝转换。

None

不传的话,默认就是float_,这也是为什么我们创建数组默认都是float类型的原因。

数组标量类型

内置的数组标量可以被转换成为相关的data-type对象。

前面一篇文章我们讲到了什么是数组标量类型。数组标量类型是可以通过np.type来访问的数据类型。 比如: np.int32np.complex128等。

我们看下数组标量的转换:

In [85]: np.dtype(np.int32)
Out[85]: dtype('int32') In [86]: np.dtype(np.complex128)
Out[86]: dtype('complex128')

这些以np开头的内置数组标量类型可以参考我之前写的文章 “NumPy之:数据类型” 。

注意,数组标量并不是dtype对象,虽然很多情况下,可以在需要使用dtype对象的时候都可以使用数组标量。

通用类型

一些通用类型对象,可以被转换成为相应的dtype类型:

通用类型对象 dtype类型
numberinexactfloating float
complexfloating cfloat
integersignedinteger int_
unsignedinteger uint
character string
genericflexible void

内置Python类型

一些Python内置的类型和数组标量类型是等价的,也可以被转换成为dtype:

Python类型 dtype类型
int int_
bool bool_
float float_
complex cfloat
bytes bytes_
str str_
buffer void
(all others) object_

看下内置Python类型转换的例子:

In [82]: np.dtype(float)
Out[82]: dtype('float64') In [83]: np.dtype(int)
Out[83]: dtype('int64') In [84]: np.dtype(object)
Out[84]: dtype('O')

带有.dtype属性的对象

任何type对象只要包含dtype属性,并且这个属性属于可以转换的范围的话,都可以被转换成为dtype。

一个字符的string对象

对于每个内置的数据类型来说都有一个和它对应的字符编码,我们也可以使用这些字符编码来进行转换:

In [134]: np.dtype('b')  # byte, native byte order
Out[134]: dtype('int8') In [135]: np.dtype('>H') # big-endian unsigned short
Out[135]: dtype('>u2') In [136]: np.dtype('<f') # little-endian single-precision float
Out[136]: dtype('float32') In [137]: np.dtype('d') # double-precision floating-point number
Out[137]: dtype('float64')

数组类型的String

Numpy中数组类型的对象有一个属性叫做typestr

typestr描述了这个数组中存放的数据类型和长度。

typestr由三部分组成,第一部分是描述数据字节顺序: < 小端 > 大端。

第二部分是数组里面元素的基本类型:

类型 描述
t Bit field (following integer gives the number of bits in the bit field).
b Boolean (integer type where all values are only True or False)
i Integer
u Unsigned integer
f Floating point
c Complex floating point
m Timedelta
M Datetime
O Object (i.e. the memory contains a pointer to PyObject)
S String (fixed-length sequence of char)
U Unicode (fixed-length sequence of Py_UNICODE)
V Other (void * – each item is a fixed-size chunk of memory)

最后一部分就是数据的长度。

dtype支持下面几种类型的转换:

类型 描述
'?' boolean
'b' (signed) byte
'B' unsigned byte
'i' (signed) integer
'u' unsigned integer
'f' floating-point
'c' complex-floating point
'm' timedelta
'M' datetime
'O' (Python) objects
'S''a' zero-terminated bytes (not recommended)
'U' Unicode string
'V' raw data (void)

我们看几个例子:

In [137]: np.dtype('d')
Out[137]: dtype('float64') In [138]: np.dtype('i4')
Out[138]: dtype('int32') In [139]: np.dtype('f8')
Out[139]: dtype('float64') In [140]: np.dtype('c16')
Out[140]: dtype('complex128') In [141]: np.dtype('a25')
Out[141]: dtype('S25') In [142]: np.dtype('U25')
Out[142]: dtype('<U25')

逗号分割的字符串

逗号分割的字符串可以用来表示结构化的数据类型。

对于这种结构化的数据类型也可以转换成为dtpye格式,转换后的dtype,将会以f1,f2, … fn-1作为名字来保存对应的格式数据。我们举个例子:

In [143]: np.dtype("i4, (2,3)f8, f4")
Out[143]: dtype([('f0', '<i4'), ('f1', '<f8', (2, 3)), ('f2', '<f4')])

上面的例子中,f0保存的是32位的整数,f1保存的是 2 x 3 数组的64-bit 浮点数。f2是一个32-bit 的浮点数。

再看另外一个例子:

In [144]: np.dtype("a3, 3u8, (3,4)a10")
Out[144]: dtype([('f0', 'S3'), ('f1', '<u8', (3,)), ('f2', 'S10', (3, 4))])

类型字符串

所有在numpy.sctypeDict.keys()中的字符,都可以被转换为dtype:

In [146]: np.sctypeDict.keys()
Out[146]: dict_keys(['?', 0, 'byte', 'b', 1, 'ubyte', 'B', 2, 'short', 'h', 3, 'ushort', 'H', 4, 'i', 5, 'uint', 'I', 6, 'intp', 'p', 7, 'uintp', 'P', 8, 'long', 'l', 'L', 'longlong', 'q', 9, 'ulonglong', 'Q', 10, 'half', 'e', 23, 'f', 11, 'double', 'd', 12, 'longdouble', 'g', 13, 'cfloat', 'F', 14, 'cdouble', 'D', 15, 'clongdouble', 'G', 16, 'O', 17, 'S', 18, 'unicode', 'U', 19, 'void', 'V', 20, 'M', 21, 'm', 22, 'bool8', 'Bool', 'b1', 'float16', 'Float16', 'f2', 'float32', 'Float32', 'f4', 'float64', 'Float64', 'f8', 'float128', 'Float128', 'f16', 'complex64', 'Complex32', 'c8', 'complex128', 'Complex64', 'c16', 'complex256', 'Complex128', 'c32', 'object0', 'Object0', 'bytes0', 'Bytes0', 'str0', 'Str0', 'void0', 'Void0', 'datetime64', 'Datetime64', 'M8', 'timedelta64', 'Timedelta64', 'm8', 'int64', 'uint64', 'Int64', 'UInt64', 'i8', 'u8', 'int32', 'uint32', 'Int32', 'UInt32', 'i4', 'u4', 'int16', 'uint16', 'Int16', 'UInt16', 'i2', 'u2', 'int8', 'uint8', 'Int8', 'UInt8', 'i1', 'u1', 'complex_', 'int0', 'uint0', 'single', 'csingle', 'singlecomplex', 'float_', 'intc', 'uintc', 'int_', 'longfloat', 'clongfloat', 'longcomplex', 'bool_', 'unicode_', 'object_', 'bytes_', 'str_', 'string_', 'int', 'float', 'complex', 'bool', 'object', 'str', 'bytes', 'a'])

使用的例子:

In [147]: np.dtype('uint32')
Out[147]: dtype('uint32') In [148]: np.dtype('float64')
Out[148]: dtype('float64')

元组

通过使用dtype构成的元组,我们可以生成新的dtype。

元组也有很多种方式。

(flexible_dtype, itemsize)

对于不固定长度的dtype,可以指定size:

In [149]: np.dtype((np.void, 10))
Out[149]: dtype('V10') In [150]: np.dtype(('U', 10))
Out[150]: dtype('<U10')

(fixed_dtype, shape)

对于固定长度的dtype,可以指定shape:

In [151]:  np.dtype((np.int32, (2,2)))
Out[151]: dtype(('<i4', (2, 2))) In [152]: np.dtype(('i4, (2,3)f8, f4', (2,3)))
Out[152]: dtype(([('f0', '<i4'), ('f1', '<f8', (2, 3)), ('f2', '<f4')], (2, 3)))

[(field_name, field_dtype, field_shape), ...]

list中的元素是一个个的field,每个field都是由2-3个部分组成的,分别是field名字,field类型,field的shape。

field_name如果是 ’ ‘的话,就会使用默认的f1,f2 ….作为名字。field_name 也可以是一个2元组,由title 和 name 组成。

field_dtype 就是field的dtype类型。

shape是一个可选字段,如果field_dtype是一个数组的话,就需要指定shape。

In [153]: np.dtype([('big', '>i4'), ('little', '<i4')])
Out[153]: dtype([('big', '>i4'), ('little', '<i4')])

上面是两个字段,一个是大端的32位的int,一个是小端的32位的int。

In [154]: np.dtype([('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')])
Out[154]: dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')])

四个字段,每个都是无符号整形。

{'names': ..., 'formats': ..., 'offsets': ..., 'titles': ..., 'itemsize': ...}

这种写法可以指定name列表和formats列表:

In [157]: np.dtype({'names': ['r','g','b','a'], 'formats': [np.uint8, np.uint8, np.uint8, np.uint8]})
Out[157]: dtype([('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')])

offsets 指的是每个字段的byte offsets。titles 是字段的title,itemsize 是整个dtype的size。

In [158]: np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'],
...: 'offsets': [0, 2],
...: 'titles': ['Red pixel', 'Blue pixel']})
...:
Out[158]: dtype({'names':['r','b'], 'formats':['u1','u1'], 'offsets':[0,2], 'titles':['Red pixel','Blue pixel'], 'itemsize':3})

(base_dtype, new_dtype)

可以将基本的dtype类型转换为结构化的dtype类型:

In [159]: np.dtype((np.int32,{'real':(np.int16, 0),'imag':(np.int16, 2)}))
Out[159]: dtype([('real', '<i2'), ('imag', '<i2')])

32位的int转换成两个16位的int。

In [161]: np.dtype(('i4', [('r','u1'),('g','u1'),('b','u1'),('a','u1')]))
Out[161]: dtype([('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')])

32位的int,转换成4个unsigned integers。

==============================================

import numpy as np

dt = np.dtype((np.int32,{'real':(np.int16, 0),'imag':(np.int16, 2)}))

x = np.array([2**31-1], dt)

print(x.tobytes(), '\t'*2, x)
print(x['real'].tobytes(), '\t'*3, x['real'])
print(x['imag'].tobytes(), '\t'*3, x['imag'])

【转载】 NumPy之:数据类型对象dtype的更多相关文章

  1. NumPy之:数据类型对象dtype

    目录 简介 dtype的定义 可转换为dtype的对象 dtype对象 None 数组标量类型 通用类型 内置Python类型 带有.dtype属性的对象 一个字符的string对象 数组类型的Str ...

  2. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  3. Numpy | 03 数据类型

    numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型. 下表列举了常用 NumPy 基本类型: 名称 描 ...

  4. 3.1Python数据处理篇之Numpy系列(一)---ndarray对象的属性与numpy的数据类型

    目录 目录 (一)简单的数组创建 1.numpy的介绍: 2.numpy的数组对象ndarray: 3.np.array(list/tuple)创建数组: (二)ndarray对象的属性 1.五个常用 ...

  5. Numpy | 02 Ndarray 对象

    NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引. ndarray 对象是用于存放同类型元素的多维数组. ndarr ...

  6. 【MongoDB】递归获取字段更新表达式,更新复杂数据类型对象

    在实际更新Mongo对象时发现,原有的更新代码无法更新复杂的数据类型对象.恰好看到张占岭老师有对该方法做相关的改进,因此全抄了下来. 总的核心思想就是运用反射与递归,对对象属性一层一层挖掘下去,循环创 ...

  7. String类、 StringBuffer、基本数据类型对象包装类

    一.概述 Sting s1 = "abc";    //s1是一个类类型变量,"abc"是一个对象. String s2 = new String(" ...

  8. 黑马程序员_Java基本数据类型对象包装类

    基本数据类型对象包装类 byte Byte short Short int Integer long Long boolean Boolean float Float double Double ch ...

  9. Java面向对象 String 基本数据类型对象包装类

      Java面向对象  String 知识概要:              (1)String的用法详解 (2)基本数据类型对象包装类 String          顾名思义,该类主要是对字符串 ...

  10. 基本数据类型对象包装(Integer等)

    基本数据类型 包装类 byte Byte short             Short int   Integer long Long boolean Boolean float          ...

随机推荐

  1. javascript 生成器和迭代器

    前置知识 生成器函数会返回一种称为Generator的迭代器 迭代器是一个对象,定义一个序列,并在终止时返回一个返回值 Symbol.iterator为每一个对象定义了默认的迭代器,可以被for..o ...

  2. 父类和子类对象的获取值的方式验证,通过父类属性的方式获取不到值,需要使用get方法

    父类和子类对象的获取值的方式验证,通过父类属性的方式获取不到值,需要使用get方法 静态属性通过类.属性的方式获取,对象获取使用get方法获取 package com.example.core.myd ...

  3. Externalizable接口实现序列化与反序列化

    Externalizable接口实现序列化与反序列化 package com.example.core.mydemo.java; import com.example.core.mydemo.json ...

  4. mongodb插入文档,更新文档和检索文档

    import com.mongodb.client.*; import com.mongodb.client.MongoClient; import com.mongodb.client.model. ...

  5. 【UnityTips】如何自定义脚本模版

    [UnityTips]如何自定义脚本模版 通常我们创建新脚本时大家看到的是这个样子: using System.Collections; using System.Collections.Generi ...

  6. Linux连接wifi,亲测成功

    环境: 装有CentOS-7的物理机 步骤: 搜索日志,查看是否有安装固件的请求: 1.dmesg | grep firmware #查看是否需要安装wifi固件 如果需要安装固件:(可以先跳过此步骤 ...

  7. 在Java中如何通过优化代码来节省内存

    Java 程序的一个常见问题是高内存使用率,这会导致性能问题甚至崩溃.因此,需要使用内存节省技术来优化 Java 代码并减少内存使用非常重要. 选择正确的数据类型: 使用适当大小的数据类型可以避免不必 ...

  8. DASCTF X GFCTF 2024|四月开启第一局 [PWN]详解

    DASCTF X GFCTF 2024|四月开启第一局[PWN] wp(详解) 1.dynamic_but_static 题目保护情况 64位程序,没有开canary和pie保护,got表可改 64位 ...

  9. Linux服务器从头配置

    安装配置jdk 下载 jdk jdk-8u171-linux-x64.tar.gz 将该压缩包放到/usr/local/jdk目录下然后解压(jdk目录需要自己手动创建) tar zxvf jdk-8 ...

  10. 谈谈你对 keep-alive 的了解?

    在做电商有关的项目中,当我们第一次进入列表页需要请求一下数据,当我从列表页进入详情页,详情页不缓存也需要请求下数据,然后返回列表页,这时候我们使用keep-alive来缓存组件,防止二次渲染,这样会大 ...