ndarray data types
Data type, i.e. DTYPE, is a special object that contains information ndarray memory blocks need to be a certain type of data affirmed (also called metadata, i.e., data representation)
dtype is able NumPy with his Qi system data and flexible interaction causes. Typically, the system provides a hard disk or other correspondence relationship memory and the data, such that reading and writing using the data underlying C or Fortran language becomes very easy.
name | description |
---|---|
bool_ | Boolean data type (True or False) |
int_ | The default integer type (similar to the C language long, int32 or Int64) |
intc | The same type C and int, int is generally 64 or int32 |
intp | For the integer type of the index (an ssize_t C-like, generally remains int32 or Int64) |
int8 | Character clause (-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_ | float64 type of shorthand |
float16 | Half-precision floating-point format, comprising: a sign bit, five-bit exponent, 10 mantissa bits |
float32 | Single-precision floating point number, comprising: a sign bit, eight exponent, 23 mantissa bits |
float64 | Double precision floating point, comprising: a sign bit, 11 exponent bits, 52 bits mantissa |
complex_ | complex128 shorthand type, i.e., a plurality of 128-bit |
complex64 | Complex, 32-bit floating-point number represents bis (real number part and imaginary number part) |
complex128 | Complex, 64-bit floating-point number represents bis (real number part and imaginary number part) |
Astype method using an explicit data type conversion array
arr = np.array([1,2,3,4,5])
print(arr.dtype)
print(arr)
float_arr = arr.astype('float32')#也可以写作 arr.astype(np.float32)
print(float_arr.dtype)
print(float_arr)
int32
[1 2 3 4 5]
float32
[1. 2. 3. 4. 5.]
Note: The contents of a digital array of strings can be converted to digital, and when the content is floating-point numbers only when converted to float, not int, is an integer only when it can be converted to int
Dtype with other arrays to convert the data type:
int_arr = np.arange(10)
calibers = np.array([.22, .270, .357], dtype=np.float64)
print(calibers)
arr_last = int_arr.astype(calibers.dtype)
print(arr_last.dtype)
print(arr_last)
[0.22 0.27 0.357]
float64
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]