首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【从矩阵到图像的类型转换2】:Numpy的类型、数据类型以及数据类型间的转换、dtype数据类型对象的实例化

  • 25-03-03 18:41
  • 3935
  • 8398
blog.csdn.net

一、numpy中的数据类型

一些更加详细的讲解可以参考如下链接

1.0 解释说明

numpy 的数值类型实际上是 dtype 对象的实例,并对应唯一的字符,包括 np.bool_,np.int32,np.float32,等等。

数据类型对象(numpy.dtype 类的实例)用来描述与数组对应的内存区域是如何使用,它描述了数据的以下几个方面:

  • 数据类型(整数、浮点或者 Python 对象)
  • 数据大小(例如, 整数使用多少个字节存储)
  • 字节序(小端或大端)
  • 在结构化类型的情况下,字段的名称,每个字段的数据类型,和每个字段占用的内存块部分。
  • 如果数据类型是子序列,它的形状和数据类型。

每个内建类型都有一个唯一定义它的字符代码:

1.1 常用数据类型总览

共19个

数据类型(初始化时,dtpye使用的类型)括号中为等价字符串内存大小字节数描述求解类型属性时候的返回值numpy_arr.dtype
np.bool_ (b1)1字节布尔值(真或假)bool
np.int_(i8或i4)4或8字节,通常为int32或int64默认的整数类型,相当于 C 的longint64
np.intc(i8或i4)4或8字节,通常为int32或int64相当于 C 的intint32
np.intp(i8)4或8字节,通常为int32或int64用于索引的整数,相当于 C 的size_tint64
np.int8(i1)1字节8位有符号整数,范围(-128 ~ 127)int8
np.int16(i2)2字节16位有符号整数,范围(-32768 ~ 32767)int16
np.int32(i4或i)4字节32位有符号整数,范围(-2147483648 ~ 2147483647)int32
np.int64(i8)8字节64位有符号整数,范围(-9223372036854775808 ~ 9223372036854775807)int64
np.uint8(u1)1字节8位无符号整数,范围(0 ~ 255)uint8
np.uint16(u2)2字节16位无符号整数,范围(0 ~ 65535)uint16
np.uint32(u3)4字节32位无符号整数,范围(0 ~ 4294967295)uint32
np.uint64(u4)8字节64位无符号整数,范围(0 ~ 18446744073709551615)uint64
np.float_(f8)8字节float64的简写float64
np.float16(f2)2字节半精度浮点:符号位,5 位指数,10 位尾数float16
np.float32(f或f4)4字节单精度浮点:符号位,8 位指数,23 位尾数float32
np.float64(f8)8字节双精度浮点:符号位,11 位指数,52 位尾数float64
np.complex_(c16)16字节complex128的简写complex128
np.complex64(c8)8字节复数,由两个32位浮点表示(实部和虚部)complex64
np.complex128(c16)16字节复数,由两个64位浮点表示(实部和虚部)complex128

举例:

针对如上所有的类型,都可以运行一下这个代码(把初始化类型换掉即可)

import numpy as np 
print("dtype: np.bool_")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.bool_)
print(numpy_arr)
print(numpy_arr.dtype)
  • 1
  • 2
  • 3
  • 4
  • 5

结果如下:

二、检查数组的数据类型,dtype对象的讲解

2.0 dtpye检查数组数据类型

使用print(type(numpy_arr))得到的结果都是

所求具体数据类型的方法:数组被初始化的时候会在内部有一个名为 dtype 对象,这个蕴含了所有数据类型的信息,直接打印可以得到numpy数组的数据类型。更加详细的信息可以打印dtpye对象的一些其他属性

举例

import numpy as np
numpy_arr =np.zeros((1,2))
print(numpy_arr)
print("数据类型对象名称:",numpy_arr.dtype)
print("实例化此数据类对象的标准类型对象:",numpy_arr.dtype.type)
print("识别一般数据类型对象的字符码:",numpy_arr.dtype.kind)
print("此数据类型对象的唯一字符码:",numpy_arr.dtype.char)
print("此数据类型对象的唯一编号:",numpy_arr.dtype.num)
print("此数据类型对象的协议类型字符串:",numpy_arr.dtype.str)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.1 dtype对象的描述

数据的类型由以下 dtype 属性描述:

方法描述
dtype.type用于实例化此数据类型的标量的类型对象。
dtype.kind识别一般数据类型的字符码( “biufcmMOSUV” 之一)。
dtype.char21种不同内置类型中的每一种都有唯一的字符码。
dtype.num21种不同内置类型中每种类型的唯一编号。
dtype.str此数据类型对象的数组协议类型字符串。

数据的大小依次由以下内容描述:

方法描述
dtype.name此数据类型的位宽名称。
dtype.itemsize此数据类型对象的元素大小。

此数据的字符顺序:

方法描述
dtype.byteorder指示此数据类型对象的字节顺序的字符。

有关结构化数据类型中的子数据类型的信息:

方法描述
dtype.fields为此数据类型定义的命名字段的字典,或 None。
dtype.names字段名称的有序列表,如果没有字段,则为 None。

对于描述子数组的数据类型:

方法描述
dtype.subdtype元组 (item_dtype,shape)。如果此dtype描述子数组,则无其他。
dtype.shape如果此数据类型描述子数组,则为子数组的Shape元组,否则为 ()。

提供附加信息的属性:

方法描述
dtype.hasobject指示此数据类型在任何字段或子数据类型中是否包含任何引用计数对象的布尔值。
dtype.flags描述如何解释此数据类型的位标志。
dtype.isbuiltin指示此数据类型如何与内置数据类型相关的整数。
dtype.isnative指示此dtype的字节顺序是否为平台固有的布尔值。
dtype.descrarray_interface 数据类型说明。
dtype.alignment根据编译器,此数据类型所需的对齐(字节)。
dtype.base返回子数组的基本元素的dtype,而不考虑其尺寸或形状。

数据类型具有以下更改字节顺序的方法:

方法描述
dtype.newbyteorder([new_order])返回具有不同字节顺序的新dtype。

以下方法实现腌制(pickle)协议:

方法描述
dtype.reduce()帮助腌制(pickle)
dtype.setstate()

dtype的赋值可以使用数据类型的全名类似于np.bool_,也可以使用等价字符串类似于b1。

举例

import numpy as np 
print("dtype: np.bool_")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.bool_)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.int_")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.int_)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.intc")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.intc)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.intp")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.intp)
print(numpy_arr)
print(numpy_arr.dtype)



print("dtype: np.int8")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.int8)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.int16")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.int16)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.int32")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.int32)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.int64")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.int64)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.uint8")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.uint8)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.uint16")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.uint16)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.uint32")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.uint32)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.uint64")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.uint64)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.float_")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.float_)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.float16")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.float16)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.float32")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.float32)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.float64")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.float64)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.complex_")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.complex_)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.complex64")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.complex64)
print(numpy_arr)
print(numpy_arr.dtype)

print("dtype: np.complex128")
numpy_arr = np.array([1, 2, 3, 4], dtype=np.complex128)
print(numpy_arr)
print(numpy_arr.dtype)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

三、数据类型之间的转换

3.1 语法

使用numpy array内置的astype函数进行数据类型之间的转换

astype可由一下语法构造:

numpy_array.astype(target_type)
  • 1

3.2 举例1

import numpy as np 
numpy_arr = np.array([1, 2, 3, 4], dtype='c8')
print(numpy_arr)
print(numpy_arr.dtype)
numpy_arr = numpy_arr.astype(np.bool_)
print(numpy_arr)
print(numpy_arr.dtype)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.3 举例2

此处写的是float 而不是np.float64, Numpy很聪明,会将python类型映射到等价的dtype上

import numpy as np 
numeric_arr = np.array(['1.2','2.3','3.2141'], dtype=np.string_)
print(numeric_arr.dtype)
numeric_arr = numeric_arr.astype(float)
print(numeric_arr.dtype)
  • 1
  • 2
  • 3
  • 4
  • 5

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JBqjR1fd-1616055606864)(http://qiniu.daidaixiang.xyz/image-20210318155056480.png)]

四、新建自己的数据类型

numpy 的数据类型实际上是numpy.dtype对象的实例,并对应唯一的字符,包括np.bool_等等。

数据类型对象用来描述与数组对应的内存区域是如何使用,它描述了数据的以下几个方面:

  • 数据类型(整数、浮点或者 Python 对象)
  • 数据大小(例如, 整数使用多少个字节存储)
  • 字节序(小端或大端)字节顺序是通过对数据类型预先设定 < 或 > 来决定的。 < 意味着小端法(最小值存储在最小的地址,即低位组放在最前面)。> 意味着大端法(最重要的字节存储在最小的地址,即高位组放在最前面)。
  • 在结构化类型的情况下,字段的名称,每个字段的数据类型,和每个字段占用的内存块部分。
  • 如果数据类型是子序列,它的形状和数据类型。

4.1 语法

dtype对象是使用以下语法构造的:

numpy.dtype(object, align, copy)
  • 1
  • object - 要转换为的数据类型对象
  • align - 如果为 true,填充字段使其类似 C 的结构体。
  • copy - 复制 dtype 对象 ,如果为 false,则是对内置数据类型对象的引用

4.2 举例

示例 1

# 使用数组标量类型  
import numpy as np 
dt = np.dtype(np.int32)  
print(dt)
  • 1
  • 2
  • 3
  • 4

输出如下:

int32
  • 1

示例 2

#int8,int16,int32,int64 可替换为等价的字符串 'i1','i2','i4',以及其他。  
import numpy as np 
dt = np.dtype('i4')  
print(dt)
  • 1
  • 2
  • 3
  • 4

输出如下:

int32
  • 1

示例 3

# 使用端记号  
import numpy as np 
dt = np.dtype('>i4')  
print(dt)
  • 1
  • 2
  • 3
  • 4

输出如下:

>i4
  • 1

下面的例子展示了结构化数据类型的使用。 这里声明了字段名称和相应的标量数据类型。

示例 4

# 首先创建结构化数据类型。  
import numpy as np 
dt = np.dtype([('age',np.int8)])  
print(dt)
  • 1
  • 2
  • 3
  • 4

输出如下:

[('age', 'i1')]
  • 1

示例 5

# 现在将其应用于 ndarray 对象  
import numpy as np 
dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt)  
print(a)
  • 1
  • 2
  • 3
  • 4
  • 5

输出如下:

[(10,) (20,) (30,)]
  • 1

示例 6

# 文件名称可用于访问 age 列的内容  
import numpy as np 
dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt)  
print(a['age'])
  • 1
  • 2
  • 3
  • 4
  • 5

输出如下:

[10 20 30]
  • 1

示例 7

以下示例定义名为 student 的结构化数据类型,其中包含字符串字段name,整数字段age和浮点字段marks。 此dtype应用于ndarray对象。

import numpy as np 
student = np.dtype([('name','S20'),  ('age',  'i1'),  ('marks',  'f4')]) 
print(student)
  • 1
  • 2
  • 3

输出如下:

[('name', 'S20'), ('age', 'i1'), ('marks', ')])
  • 1

示例 8

import numpy as np 
student = np.dtype([('name','S20'),  ('age',  'i1'),  ('marks',  'f4')]) 
a = np.array([('abc',  21,  50),('xyz',  18,  75)], dtype = student)  
print(a)
  • 1
  • 2
  • 3
  • 4

输出如下:

[('abc', 21, 50.0), ('xyz', 18, 75.0)]
  • 1

LAST、参考文献

numpy创建空数组_lsh呵呵-CSDN博客_空array

Numpy数据类型转换astype,dtype_Aurora Silent-CSDN博客

NumPy - 数据类型 · TutorialsPoint NumPy 教程

数据类型对象(dtype) | NumPy 中文

NumPy 数据类型 | 菜鸟教程

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树科学计算基础软件包NumPyNumPy概述416632 人正在系统学习中
注:本文转载自blog.csdn.net的呆呆象呆呆的文章"https://blog.csdn.net/qq_41554005/article/details/114983092"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top