NumPy Data Types
NumPy supports far more data types than Python's built-in types, and they basically correspond to C language data types, with some of them corresponding to Python built-in types. The following table lists the commonly used NumPy basic types.
| Name | Description |
|---|---|
| bool_ | Boolean data type (True or False) |
| int_ | Default integer type (similar to long in C, int32 or int64) |
| intc | Same as C's int type, generally int32 or int64 |
| intp | Integer type used for indexing (similar to ssize_t in C, generally still int32 or int64) |
| int8 | Byte (-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_ | Abbreviation of float64 type |
| float16 | Half-precision floating point, including: 1 sign bit, 5 exponent bits, 10 mantissa bits |
| float32 | Single-precision floating point, including: 1 sign bit, 8 exponent bits, 23 mantissa bits |
| float64 | Double-precision floating point, including: 1 sign bit, 11 exponent bits, 52 mantissa bits |
| complex_ | Abbreviation of complex128 type, i.e., 128-bit complex number |
| complex64 | Complex number, represented by two 32-bit floating point numbers (real and imaginary parts) |
| complex128 | Complex number, represented by two 64-bit floating point numbers (real and imaginary parts) |
NumPy's numeric types are actually instances of dtype objects, each corresponding to a unique character, including np.bool_, np.int32, np.float32, and so on.
Data Type Object (dtype)
Data type objects (instances of the numpy.dtype class) are used to describe how the memory region corresponding to an array is used. They describe the following aspects of the data:
- The type of data (integer, floating point, or Python object)
- The size of the data (e.g., how many bytes an integer uses for storage)
- The byte order of the data (little-endian or big-endian)
- In the case of structured types, the names of fields, the data type of each field, and the part of the memory block each field takes
- If the data type is a sub-array, then what its shape and data type are.
Byte order is determined by pre-setting the data type<or>to decide.<Means little-endian (the least significant byte is stored at the smallest address, i.e., the low-order byte group is placed first).>Means big-endian (the most significant byte is stored at the smallest address, i.e., the high-order byte group is placed 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, pad the fields to make it similar to a C struct.
- copy - copy the dtype object; if false, it is a reference to the built-in data type object.
Examples
Next, we can understand through examples.
Example 1
The output result is:
int32
Example 2
The output result is:
int32
Example 3
The output result is:
int32
The following example demonstrates the use of structured data types; the type fields and the corresponding actual types will be created.
Example 4
The output result is:
[('age', 'i1')]
Example 5
The output result is:
[(10,) (20,) (30,)]
Example 6
The output result is:
[10 20 30]
The following example defines a structured data type student, containing a string field name, an integer field age, and a floating-point field marks, and applies this dtype to an ndarray object.
Example 7
The output result is:
[('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]
Example 8
The output result is:
[(b'abc', 21, 50.) (b'xyz', 18, 75.)]
Each built-in type has a unique character code that defines it, as follows:
| Character | Corresponding type |
|---|---|
| b | Boolean |
| i | (Signed) integer |
| u | Unsigned integer |
| f | Floating point |
| c | Complex floating point |
| m | timedelta (time interval) |
| M | datetime (date and time) |
| O | (Python) object |
| S, a | (byte-)string |
| U | Unicode |
| V | Raw data (void) |