一、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 的long |
int64 |
np.intc (i8 或i4 ) |
4或8字节,通常为int32或int64 | 相当于 C 的int |
int32 |
np.intp (i8 ) |
4或8字节,通常为int32或int64 | 用于索引的整数,相当于 C 的size_t |
int64 |
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)
结果如下:
二、检查数组的数据类型,dtype对象的讲解
2.0 dtpye检查数组数据类型
使用print(type(numpy_arr))
得到的结果都是<class 'numpy.ndarray'>
所求具体数据类型的方法:数组被初始化的时候会在内部有一个名为 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)
2.1 dtype对象的描述
数据的类型由以下 dtype
属性描述:
方法 | 描述 |
---|---|
dtype.type | 用于实例化此数据类型的标量的类型对象。 |
dtype.kind | 识别一般数据类型的字符码( “biufcmMOSUV” 之一)。 |
dtype.char | 21种不同内置类型中的每一种都有唯一的字符码。 |
dtype.num | 21种不同内置类型中每种类型的唯一编号。 |
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.descr | array_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)
三、数据类型之间的转换
3.1 语法
使用numpy array内置的astype
函数进行数据类型之间的转换
astype
可由一下语法构造:
numpy_array.astype(target_type)
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)
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)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JBqjR1fd-1616055606864)(http://qiniu.daidaixiang.xyz/image-20210318155056480.png)]
四、新建自己的数据类型
numpy 的数据类型实际上是numpy.dtype对象
的实例,并对应唯一的字符,包括np.bool_
等等。
数据类型对象用来描述与数组对应的内存区域是如何使用,它描述了数据的以下几个方面:
- 数据类型(整数、浮点或者 Python 对象)
- 数据大小(例如, 整数使用多少个字节存储)
- 字节序(小端或大端)字节顺序是通过对数据类型预先设定 < 或 > 来决定的。 < 意味着小端法(最小值存储在最小的地址,即低位组放在最前面)。> 意味着大端法(最重要的字节存储在最小的地址,即高位组放在最前面)。
- 在结构化类型的情况下,字段的名称,每个字段的数据类型,和每个字段占用的内存块部分。
- 如果数据类型是子序列,它的形状和数据类型。
4.1 语法
dtype对象
是使用以下语法构造的:
numpy.dtype(object, align, copy)
- object - 要转换为的数据类型对象
- align - 如果为 true,填充字段使其类似 C 的结构体。
- copy - 复制 dtype 对象 ,如果为 false,则是对内置数据类型对象的引用
4.2 举例
示例 1
# 使用数组标量类型
import numpy as np
dt = np.dtype(np.int32)
print(dt)
输出如下:
int32
示例 2
#int8,int16,int32,int64 可替换为等价的字符串 'i1','i2','i4',以及其他。
import numpy as np
dt = np.dtype('i4')
print(dt)
输出如下:
int32
示例 3
# 使用端记号
import numpy as np
dt = np.dtype('>i4')
print(dt)
输出如下:
>i4
下面的例子展示了结构化数据类型的使用。 这里声明了字段名称和相应的标量数据类型。
示例 4
# 首先创建结构化数据类型。
import numpy as np
dt = np.dtype([('age',np.int8)])
print(dt)
输出如下:
[('age', 'i1')]
示例 5
# 现在将其应用于 ndarray 对象
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a)
输出如下:
[(10,) (20,) (30,)]
示例 6
# 文件名称可用于访问 age 列的内容
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a['age'])
输出如下:
[10 20 30]
示例 7
以下示例定义名为 student 的结构化数据类型,其中包含字符串字段name
,整数字段age
和浮点字段marks
。 此dtype
应用于ndarray
对象。
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)
输出如下:
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
示例 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)
输出如下:
[('abc', 21, 50.0), ('xyz', 18, 75.0)]
LAST、参考文献
numpy创建空数组_lsh呵呵-CSDN博客_空array
Numpy数据类型转换astype,dtype_Aurora Silent-CSDN博客