Introduction to the
We know that there are four numeric types in Python: int, float, bool, and complex. As NumPy for scientific computing, its data types are much richer.
Today I’ll take a closer look at the data types in NumPy.
The data type in an array
NumPy is implemented in C, and we can compare the data types in NumPy with those in C:
Types in Numpy | Types in C | instructions |
---|---|---|
np.bool_ | bool |
Boolean (True or False) stored as a byte |
np.byte | signed char |
Platform-defined |
np.ubyte | unsigned char |
Platform-defined |
np.short | short |
Platform-defined |
np.ushort | unsigned short |
Platform-defined |
np.intc | int |
Platform-defined |
np.uintc | unsigned int |
Platform-defined |
np.int_ | long |
Platform-defined |
np.uint | unsigned long |
Platform-defined |
np.longlong | long long |
Platform-defined |
np.ulonglong | unsigned long long |
Platform-defined |
np.half / np.float16 | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa | |
np.single | float |
Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa |
np.double | double |
Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa. |
np.longdouble | long double |
Platform-defined extended-precision float |
np.csingle | float complex |
Complex number, represented by two single-precision floats (real and imaginary components) |
np.cdouble | double complex |
Complex number, represented by two double-precision floats (real and imaginary components). |
np.clongdouble | long double complex |
Complex number, represented by two extended-precision floats (real and imaginary components). |
Let’s take a random look at what the above types are in the Ipython environment:
import numpy as np
In [26]: np.byte
Out[26]: numpy.int8
In [27]: np.bool_
Out[27]: numpy.bool_
In [28]: np.ubyte
Out[28]: numpy.uint8
In [29]: np.short
Out[29]: numpy.int16
In [30]: np.ushort
Out[30]: numpy.uint16
Copy the code
So the above data type, the underlying data type is still fixed length, let’s see what they are:
Numpy type | C type | instructions |
---|---|---|
np.int8 | int8_t |
Byte (-128 to 127) |
np.int16 | int16_t |
Integer (-32768 to 32767) |
np.int32 | int32_t |
Integer (-2147483648 to 2147483647) |
np.int64 | int64_t |
Integer (-9223372036854775808 to 9223372036854775807) |
np.uint8 | uint8_t |
Unsigned integer (0 to 255) |
np.uint16 | uint16_t |
Unsigned integer (0 to 65535) |
np.uint32 | uint32_t |
Unsigned integer (0 to 4294967295) |
np.uint64 | uint64_t |
Unsigned integer (0 to 18446744073709551615) |
np.intp | intptr_t |
Integer used for indexing, typically the same as ssize_t |
np.uintp | uintptr_t |
Integer large enough to hold a pointer |
np.float32 | float |
|
np.float64 / np.float_ | double |
Note that this matches the precision of the builtin python float. |
np.complex64 | float complex |
Complex number, represented by two 32-bit floats (real and imaginary components) |
np.complex128 / np.complex_ | double complex |
Note that this matches the precision of the builtin python complex. |
All of these types are instances of dType objects. There are five basic types commonly used: bool, int, uint, float, and complex.
The number following the type indicates the number of bytes occupied by the type.
There are some platform-defined data types in the table above. These types are platform-specific and should be used with special care.
These dType types can be specified manually when creating arrays:
>>> import numpy as np
>>> x = np.float32(1.0)
>>> x
1.0
>>> y = np.int_([1.2.4])
>>> y
array([1.2.4])
>>> z = np.arange(3, dtype=np.uint8)
>>> z
array([0.1.2], dtype=uint8)
Copy the code
For historical reasons, we can also specify a character format dTYPE when creating an array for backward compatibility.
>>> np.array([1.2.3], dtype='f')
array([ 1..2..3.], dtype=float32)
Copy the code
The f above represents the float type.
Type conversion
If you want to cast an existing array type, you can either use the astype method that comes with the array or call NP’s cast method:
In [33]: z = np.arange(3, dtype=np.uint8)
In [34]: z
Out[34]: array([0.1.2], dtype=uint8)
In [35]: z.astype(float)
Out[35]: array([0..1..2.])
In [36]: np.int8(z)
Out[36]: array([0.1.2], dtype=int8)
Copy the code
Note that we used float above, and Python will automatically replace float with NP.float_, as well as int == Np.int_, bool == NP.BOOL_, complex == np.plex_. Simplified versions cannot be used for other data types.
Check the type
To check the data type of an array, use the built-in dtype property:
In [37]: z.dtype
Out[37]: dtype('uint8')
Copy the code
Dtype, as an object, can also perform some type judgments:
>>> d = np.dtype(int)
>>> d
dtype('int32')
>>> np.issubdtype(d, np.integer)
True
>>> np.issubdtype(d, np.floating)
False
Copy the code
Data overflow
In general, an exception will be reported if the data is out of range. Let’s say we have a very long int:
In [38]: a= 1000000000000000000000000000000000000000000000000000000000000000000000000000000
In [39]: a
Out[39] :1000000000000000000000000000000000000000000000000000000000000000000000000000000
In [40]: np.int(1000000000000000000000000000000000000000000000000000000)
Out[40] :1000000000000000000000000000000000000000000000000000000
In [41]: np.int32(1000000000000000000000000000000000000000000000000000000)
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-41-71feb4433730> in <module>()
----> 1 np.int32(1000000000000000000000000000000000000000000000000000000)
Copy the code
The number above is too long, outside the int32 range, and an exception will be thrown.
NumPy does not raise an exception if it is out of range.
In [43]: np.power(100.8, dtype=np.int32)
Out[43] :1874919424
In [44]: np.power(100.8, dtype=np.int64)
Out[44] :10000000000000000
Copy the code
NumPy provides two methods to measure the range of int and float, numpy.iinfo and numpy.finfo:
In [45]: np.iinfo(int)
Out[45]: iinfo(min= -9223372036854775808.max=9223372036854775807, dtype=int64)
In [46]: np.iinfo(np.int32)
Out[46]: iinfo(min= -2147483648.max=2147483647, dtype=int32)
In [47]: np.iinfo(np.int64)
Out[47]: iinfo(min= -9223372036854775808.max=9223372036854775807, dtype=int64)
Copy the code
If a 64-bit int is still too small, use NP. float64. Float64 uses scientific notation, so a wider range of results can be obtained, but its accuracy may be reduced.
In [48]: np.power(100.100, dtype=np.int64)
Out[48] :0
In [49]: np.power(100.100, dtype=np.float64)
Out[49] :1e+200
Copy the code
This article is available at www.flydean.com/02-python-n…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!