📜  NumPy数据类型

📅  最后修改于: 2020-10-27 02:14:38             🧑  作者: Mango

NumPy数据类型

与Python相比,NumPy提供了更大范围的数字数据类型。下表列出了数字数据类型。

SN Data type Description
1 bool_ It represents the boolean value indicating true or false. It is stored as a byte.
2 int_ It is the default type of integer. It is identical to long type in C that contains 64 bit or 32-bit integer.
3 intc It is similar to the C integer (c int) as it represents 32 or 64-bit int.
4 intp It represents the integers which are used for indexing.
5 int8 It is the 8-bit integer identical to a byte. The range of the value is -128 to 127.
6 int16 It is the 2-byte (16-bit) integer. The range is -32768 to 32767.
7 int32 It is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647.
8 int64 It is the 8-byte (64-bit) integer. The range is -9223372036854775808 to 9223372036854775807.
9 uint8 It is the 1-byte (8-bit) unsigned integer.
10 uint16 It is the 2-byte (16-bit) unsigned integer.
11 uint32 It is the 4-byte (32-bit) unsigned integer.
12 uint64 It is the 8 bytes (64-bit) unsigned integer.
13 float_ It is identical to float64.
14 float16 It is the half-precision float. 5 bits are reserved for the exponent. 10 bits are reserved for mantissa, and 1 bit is reserved for the sign.
15 float32 It is a single precision float. 8 bits are reserved for the exponent, 23 bits are reserved for mantissa, and 1 bit is reserved for the sign.
16 float64 It is the double precision float. 11 bits are reserved for the exponent, 52 bits are reserved for mantissa, 1 bit is used for the sign.
17 complex_ It is identical to complex128.
18 complex64 It is used to represent the complex number where real and imaginary part shares 32 bits each.
19 complex128 It is used to represent the complex number where real and imaginary part shares 64 bits each.

NumPy类型

numpy数组的所有项目都是数据类型对象,也称为numpy dtypes。数据类型对象实现与数组相对应的固定大小的内存。

我们可以使用以下语法创建dtype对象。

numpy.dtype(object, align, copy)

构造函数接受以下对象。

对象:它表示要转换为数据类型的对象。

对齐:可以将其设置为任何布尔值。如果为true,则它将添加额外的填充以使其等效于C结构。

复制:它将创建dtype对象的另一个副本。

例子1

import numpy as np
d = np.dtype(np.int32)
print(d)

输出:

int32

例子2

import numpy as np 
d = np.int32(i4)
print(d)

输出:

int32

创建结构化数据类型

我们可以创建一个类似于地图的(字典)数据类型,其中包含值之间的映射。例如,它可以包含员工与薪水或学生与年龄之间的映射等。

考虑以下示例。

例子1

import numpy as np
d = np.dtype([('salary',np.float)])
print(d)

输出:

[('salary', '

例子2

import numpy as np
d=np.dtype([('salary',np.float)])
arr = np.array([(10000.12,),(20000.50,)],dtype=d)
print(arr['salary'])

输出:

[(10000.12,) (20000.5 ,)]