English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
NumPy data type description and example
Data type, i.e., dtype, is also a special object that contains the memory block information required for an ndarray to declare a certain type of data (also known as metadata, representing the data of the data). Dtype is the reason why NumPy can interact flexibly with other systems. Usually, other systems provide a correspondence between a hard disk or memory and data, making it very convenient to read and write data using low-level languages such as C or Fortran.
The data types supported by numpy are more than Python built-in data typesThere are many more, basically corresponding to C language data types, among which some types correspond to Python built-in types. The following table lists commonly used NumPy basic types.
Name | Description |
int_ | Default integer type (similar to C language's long, int32 or int64) |
int8 | Byte (-128 to 127) |
int16 | Integer (-32768 to 32767) |
int32 | Integer (-2147483648 to 2147483647) |
int64 | Integer (-9223372036854775808 to 9223372036854775807) |
intc | The same as C's int type, it is usually int32 or int 64 |
intp | Integer type used for indexing (similar to C's ssize_t, it is usually still int32 or int64) |
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_ | float64 Abbreviation for type |
float16 | Half-precision floating-point numbers include:1 Number of sign bits,5 Number of exponent bits,10 Number of mantissa bits |
float32 | Single-precision floating-point numbers include:1 Number of sign bits,8 Number of exponent bits,23 Number of mantissa bits |
float64 | Double-precision floating-point numbers include:1 Number of sign bits,11 Number of exponent bits,52 Number of mantissa bits |
complex_ | complex128 Abbreviation for type, i.e. 128 Bit complex |
complex64 | Complex, representing double 32 Bit floating-point (real part and imaginary part) |
complex128 | Complex, representing double 64 Bit floating-point (real part and imaginary part) |
bool_ | Boolean data type (True or False) |
It's okay if you can't remember all these NumPy dtype names, especially for beginners. Usually, you just need to know the approximate type of the data you are dealing with is a floating-point number, complex number, integer, boolean value, string, or a regular Python object. When you need to control the storage method of data in memory and on disk (especially for large datasets), you need to understand how to control the storage type.
NumPy's numeric types are actually instances of dtype objects and correspond to unique characters, including np.bool_, np.int32, np.float32etc.
The data type object is used to describe how the memory area corresponding to the array is used, which depends on the following aspects:
Data type - For example, floating-point numbers, complex numbers, integers, boolean values, strings, or Python objects.Data size - For example, how many bytes an integer uses to store.Data byte order - Little-endian or big-endian
The 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 at the front). ">" means big-endian (the most significant byte is stored at the smallest address, i.e., the high-order group is placed at the front).
The dtype object is constructed using the following syntax:
numpy.dtype(object, align, copy)
object - The data type object to be converted toalign - If true, fill the fields to make it similar to C's structurecopy - Copy dtype object, if false, it is a reference to an intrinsic data type object
Next, we can understand through an example.
>>> import numpy as np >>> a = np.dtype(np.float32) >>> print(a) float32
# int8, int16, int32, int64 Four data types can be used as strings 'i1', 'i2','i4','i8'' instead of >>> import numpy as np >>> a = np.dtype('i8') >>> print(a) int64
The following example demonstrates the use of structured data types, creating type fields and corresponding actual types.
>>> import numpy as np >>> a = np.dtype([('number',np.int16)] # The data type is applied to the ndarray object >>> print(a) [('number', '<i2']) >>> arr = np.array(1,)2,)3[(( >>> print(arr) ,)1,)2,)3>>> print(arr['number']) # The type field name can be used to access the actual 'number' column The following example defines a structured data type 'animal', which includes a string field 'name', an integer field 'age', and a floating-point field 'marks', and applies this dtype to an ndarray object. [1 2 3]
>>> animal = np.dtype([('name','S
>>> import numpy as np 21'), ('marks', '4')]) >>> print(animal) 21'), ('marks', ' <f4']) >>> a = np.array([('cat', 5, 10),(b'dog', 4, 35),(b'lion',8,18]), dtype = animal) >>> print(a) [(b'cat', 5, 10).(b'dog', 4, 35).(b'lion', 8, 18])
Using the astype method, you can explicitly convert the data type of an array, as shown in the following example:
>>> import numpy as np >>> arr = np.array([1,2,3,4,5]) >>> print(arr.dtype) int64 >>> print(arr) [1 2 3 4 5] >>> float_arr = arr.astype('float32') # Also can be written as arr.astype(np.float32) >>> print(float_arr.dtype) float32 >>> print(float_arr) [1. 2. 3. 4. 5.]
You can also convert data types using the dtype of other arrays, for example:
>>> import numpy as np >>> int_arr = np.arange(10) >>> calibers = np.array([.22,.270.,357], dtype=np.float64) >>> print(calibers) [0.22 0.27 0.357] >>> arr_last = int_arr.astype(calibers.dtype) >>> print(arr_last.dtype) float64 >>> print(arr_last) [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
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 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 | Original Data (void) |