02NumPy learning - data types

  The data types supported by numpy are much more than the built-in types in Python, which can basically correspond to the data types in the C language, some of which correspond to the built-in types in Python. The following table lists commonly used NumPy primitive types.

type of data describe
bool_ Boolean data type (True or False)
int_ Default integer type (similar to long, int32 or int64 in C)
intc Like C's int type, generally int32 or int 64
intp Integer type for indexing (similar to C's ssize_t, still int32 or int64 in general)
int8 bytes (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 unsigned integer (0 to 255)
uint16 unsigned integer (0 to 65535)
uint32 unsigned integer (0 to 4294967295)
uint64 unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64 type
float16 Half-precision floating point number, including: 1 sign bit, 5 exponent bits, 10 mantissa bits
float32 Single-precision floating-point number, including: 1 sign bit, 8 exponent bits, 23 mantissa bits
float64 Double-precision floating-point number, including: 1 sign bit, 11 exponent bits, 52 mantissa bits
complex_ Shorthand for type complex128, which is a 128-bit complex number
complex64 Complex number, representing a double 32-bit floating point number (real and imaginary parts)
complex128 Complex number, representing a double 64-bit floating point number (real and imaginary parts)

Numpy's numeric types are actually instances of dtype objects, and correspond to unique characters, including np.bool_, np.int32, np.float32, and so on.

NumPy data type object (dtype)

The data type object is used to describe how the memory area corresponding to the array is used, which depends on the following aspects:

  • the type of data (integer, float or Python object)
  • The size of the data (for example, how many bytes are used for integer storage)
  • Byte order of the data (little endian or big endian)
  • In the case of structured types, the name of the field, the data type of each field, and the portion of the memory block each field takes
  • If the data type is a subarray, its shape and data type

The byte order is determined by presetting "<" or ">" to the data type. "<" means little endian (the smallest value is stored at the smallest address, i.e. the low-order group is placed first). ">" means big endian (the most significant byte is stored at the smallest address, i.e. the high order byte is first).

dtype objects are constructed using the following syntax:

numpy.dtype(object, align, copy)

  • object - the data type object to convert to
  • align - If true, pads the fields to make them look like C structs.
  • copy - copy the dtype object, if false, a reference to the built-in dtype object

Next, we can understand it through examples.

Example 1

import numpy as np
# 使用标量类型
dt = np.dtype(np.int32)
print(dt)

The output is: int32

Example 2

import numpy as np
# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
dt = np.dtype('i4')
print(dt)

The output is: int32

Example 3

import numpy as np
# 字节顺序标注
dt = np.dtype('<i4')
print(dt)

The output is: int32

The following example shows the use of structured data types, the type field and the corresponding actual type will be created.

Example 4

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

The output is: [('age', 'i1')]

Example 5

# 将数据类型应用于 ndarray 对象
import numpy as np
dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt) 
print(a)

The output is: [(10,) (20,) (30,)]

Example 6

# 将数据类型应用于 ndarray 对象
# 类型字段名可以用于存取实际的 age 列
import numpy as np
dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt) 
print(a['age'])

The output is: [10 20 30]

The following example defines a structured data type student with a string field name, an integer field age, and a floating point field marks, and applies this dtype to an ndarray object.

Example 7

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

The output is: [('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]

Example 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)

The output is: [(b'abc', 21, 50.) (b'xyz', 18, 75.)]

Each built-in type has a character code that uniquely defines it, as follows:

character Corresponding type
b boolean
i (signed) integer
u unsigned integer integer
f floating point
c complex float
m timedelta (time interval)
M datetime (datetime)
O (Python) object
S, a (byte-) string skewer
U Unicode
V raw data (void)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326041221&siteId=291194637