📜  NumPy Python中的数据类型对象(dtype)

📅  最后修改于: 2022-05-13 01:54:28.102000             🧑  作者: Mango

NumPy Python中的数据类型对象(dtype)

每个 ndarray 都有一个关联的数据类型 (dtype) 对象。这个数据类型对象(dtype)告诉我们数组的布局。这意味着它为我们提供了以下信息:

  • 数据类型(整数、浮点数、 Python对象等)
  • 数据大小(字节数)
  • 数据的字节顺序(小端或大端)
  • 如果数据类型是子数组,它的形状和数据类型是什么?

ndarray 的值存储在缓冲区中,可以将其视为连续的内存字节块。所以这些字节将如何被解释由 dtype 对象给出。

1.构造数据类型(dtype)对象:数据类型对象是NumPy.dtype类的一个实例,可以使用NumPy.dtype来创建。

参数:

  • obj:要转换为数据类型对象的对象。
  • 对齐:布尔值,可选
    向字段添加填充以匹配 C 编译器为类似 C 结构输出的内容。
  • 复制:布尔,可选
    制作数据类型对象的新副本。如果为 False,则结果可能只是对内置数据类型对象的引用。
Python
# Python Program to create a data type object
import numpy as np
 
# np.int16 is converted into a data type object.
print(np.dtype(np.int16))


Python
# Python Program to create a data type object
# containing a 32 bit big-endian integer
import numpy as np
 
# i4 represents integer of size 4 byte
# > represents big-endian byte ordering and < represents little-endian encoding.
# dt is a dtype object
dt = np.dtype('>i4')
 
print("Byte order is:",dt.byteorder)
 
print("Size is:",dt.itemsize)
 
print("Data type is:",dt.name)


Python
# Python program to differentiate
# between type and dtype.
import numpy as np
 
a = np.array([1])
 
print("type is: ",type(a))
print("dtype is: ",a.dtype)


Python
# Python program for demonstrating
# the use of fields
import numpy as np
 
# A structured data type containing a 16-character string (in field ‘name’) 
# and a sub-array of two 64-bit floating-point number (in field ‘grades’):
 
dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
 
# Data type of object with field grades
print(dt['grades'])
 
# Data type of object with field name 
print(dt['name'])


Python
# Python program to demonstrate 
# the use of data type object with structured array.
import numpy as np
 
dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
 
# x is a structured array with names and marks of students.
# Data type of name of the student is np.unicode_ and 
# data type of marks is np.float(64)
x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
 
print(x[1])
print("Grades of John are: ",x[1]['grades'])
print("Names are: ",x['name'])


输出:

int16

Python

# Python Program to create a data type object
# containing a 32 bit big-endian integer
import numpy as np
 
# i4 represents integer of size 4 byte
# > represents big-endian byte ordering and < represents little-endian encoding.
# dt is a dtype object
dt = np.dtype('>i4')
 
print("Byte order is:",dt.byteorder)
 
print("Size is:",dt.itemsize)
 
print("Data type is:",dt.name)

输出:

Byte order is: >
Size is: 4
Name of data type is: int32

类型说明符(在上述情况下为 i4 )可以采用不同的形式:

  • b1,i1,i2,i4,i8,u1,u2,u4,u8,f2,f4,f8,c8,c16,a
    (表示字节、整数、无符号整数、浮点数、复数和
    指定字节长度的固定长度字符串)
  • int8,…,uint8,…,float16, float32, float64, complex64, complex128
    (这次是大小)

笔记:

dtype is different from type. 

Python

# Python program to differentiate
# between type and dtype.
import numpy as np
 
a = np.array([1])
 
print("type is: ",type(a))
print("dtype is: ",a.dtype)

输出:

type is:    
dtype is:  int32

2. 具有结构化数组的数据类型对象:数据类型对象对于创建结构化数组很有用。结构化数组是包含不同类型数据的数组。可以借助字段访问结构化数组。
字段就像为对象指定名称。在结构化数组的情况下,dtype 对象也将是结构化的。

Python

# Python program for demonstrating
# the use of fields
import numpy as np
 
# A structured data type containing a 16-character string (in field ‘name’) 
# and a sub-array of two 64-bit floating-point number (in field ‘grades’):
 
dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
 
# Data type of object with field grades
print(dt['grades'])
 
# Data type of object with field name 
print(dt['name'])

输出:

('

Python

# Python program to demonstrate 
# the use of data type object with structured array.
import numpy as np
 
dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
 
# x is a structured array with names and marks of students.
# Data type of name of the student is np.unicode_ and 
# data type of marks is np.float(64)
x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
 
print(x[1])
print("Grades of John are: ",x[1]['grades'])
print("Names are: ",x['name'])

输出:

('John', [ 6.,  7.])
Grades of John are:  [ 6.  7.]
Names are:  ['Sarah' 'John']

参考 :

  • docs.scipy.org
  • 结构化数组