NumPy Data Types
numpy supports far more data types than Python's built-in types, and basically corresponds to the data types of the C language, with some types corresponding to Python's 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 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 the 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_ | Shorthand for the complex128 type, i.e. a 128-bit complex number |
| complex64 | Complex number, representing two 32-bit floating point numbers (real part and imaginary part) |
| complex128 | Complex number, representing two 64-bit floating point numbers (real part and imaginary part) |
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.
Data type object (dtype)
A data type object (an instance of the numpy.dtype class) is used to describe how the memory region corresponding to an array is used. It describes the data in the following aspects::
- The type of the 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 a structured type, the names of the fields, the data type of each field, and the portion of the memory block taken by each field
- If the data type is a sub-array, what its shape and data type are.
Byte order is determined by pre-setting the data type to < or >. < 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 group is placed first).
A dtype object is 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 them similar to a C struct.
- copy - copy the dtype object; if false, it is a reference to the built-in data type object
Example
Next we can understand this through examples.
Example 1
import
numpy
as
np
Using scalar types
dt
=
np
.
dtype
(
np
.
int32
)
(
dt
)
The output is:
int32
Example 2
import
numpy
as
np
The four data types int8, int16, int32, int64 can be replaced by the strings 'i1', 'i2', 'i4', 'i8'
dt
=
np
.
dtype
(
'
i4
'
)
(
dt
)
The output is:
int32
Example 3
import
numpy
as
np
Byte order marking
dt
=
np
.
dtype
(
'
' ) print ( dt ) The output is: The following example shows the use of structured data types; the type fields and corresponding actual types will be created. import numpy as np dt = np . dtype ( [ ( ' age ' , np . int8 ) ] ) print ( dt ) The output is: import numpy as np dt = np . dtype ( [ ( ' age ' , np . int8 ) ] ) a = np . array ( [ ( 10 , ) , ( 20 , ) , ( 30 , ) ] , dtype = dt ) print ( a ) The output is: import numpy as np dt = np . dtype ( [ ( ' age ' , np . int8 ) ] ) a = np . array ( [ ( 10 , ) , ( 20 , ) , ( 30 , ) ] , dtype = dt ) print ( a [ ' age ' ] ) The output is: The following example defines a structured data type student, containing the string field name, the integer field age, and the floating point field marks, and applies this dtype to the ndarray object. import numpy as np student = np . dtype ( [ ( ' name ' , ' S20 ' ) , ( ' age ' , ' i1 ' ) , ( ' marks ' , ' f4 ' ) ] ) print ( student ) The output is: 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: Each built-in type has a unique character code that defines it, as follows:int32
Example 4
First create the structured data type
[('age', 'i1')]
Example 5
Apply the data type to the ndarray object
[(10,) (20,) (30,)]
Example 6
Type field names can be used to access the actual age column
[10 20 30]
Example 7
[('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]
Example 8
[(b'abc', 21, 50.) (b'xyz', 18, 75.)]
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)