Numpy Dtype
NumPy supports many more data types than Python's built-in types, and they can basically correspond to C language data types, with some types corresponding to Python's built-in types. The following table lists common NumPy basic types.
| Name | Description |
| --- | --- |
| bool_ | Boolean data type (True or False) |
| int_ | Default integer type (similar to C's long, int32 or int64) |
| intc | Same as C's int type, typically int32 or int64 |
| intp | Integer type used for indexing (similar to C's ssize_t, 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_ | Shorthand for float64 |
| float16 | Half-precision float, includes: 1 sign bit, 5 exponent bits, 10 mantissa bits |
| float32 | Single-precision float, includes: 1 sign bit, 8 exponent bits, 23 mantissa bits |
| float64 | Double-precision float, includes: 1 sign bit, 11 exponent bits, 52 mantissa bits |
| complex_ | Shorthand for complex128, i.e., 128-bit complex number |
| complex64 | Complex number, representing two 32-bit floats (real and imaginary parts) |
| complex128 | Complex number, representing two 64-bit floats (real and imaginary parts) |
NumPy's numerical types are actually instances of dtype objects and correspond to unique character codes, including np.bool_, np.int32, np.float32, etc.
* * *
## Data Type Object (dtype)
A data type object (an instance of the numpy.dtype class) describes how the memory block corresponding to an array is used. It describes several aspects of the data:
* The type of data (integer, float, or Python object)
* The size of the data (e.g., how many bytes are used to store an integer)
* The byte order of the data (little-endian or big-endian)
* In the case of structured types, the names of the fields, the data type of each field, and the part of the memory block each field occupies
* If the data type is a sub-array, what its shape and data type are.
Byte order is determined by pre-setting in the data type. means big-endian (the most significant byte is stored at the smallest address, i.e., the high-order group is placed first).
The dtype object is constructed using the following syntax:
numpy.dtype(object, align, copy)
* object - The data type object to convert to
* align - If true, adds padding to make it similar to a C struct.
* copy - Copies 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
import numpy as np dt = np.dtype(np.int32)print(dt)
Output:
int32
## Example 2
import numpy as np dt = np.dtype('i4')print(dt)
Output:
int32
## Example 3
import numpy as np dt = np.dtype('<i4')print(dt)
Output:
int32
The following example demonstrates the use of structured data types, where type fields and corresponding actual types will be created.
## Example 4
import numpy as np dt = np.dtype([('age',np.int8)])print(dt)
Output:
[('age', 'i1')]
## Example 5
import numpy as np dt = np.dtype([('age',np.int8)])a = np.array([(10,),(20,),(30,)], dtype = dt)print(a)
Output:
[(10,) (20,) (30,)]
## Example 6
import numpy as np dt = np.dtype([('age',np.int8)])a = np.array([(10,),(20,),(30,)], dtype = dt)print(a['age'])
Output:
The following example defines a structured data type `student`, containing a string field `name`, an integer field `age`, and a float 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)
Output:
[('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)
Output:
[(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) |
YouTip