I create a simple numpy of data type uint as below:
import numpy as np
a = np.array([1,2,3], dtype=np.uint)
When I compute
a[0] + 1
I expect a result of 2, but it gives
2.0
Why there is such an implicit conversion to float for np.uint? Note that it does not happen with int or np.int64
NumPy data types uint8 In Python uint8 datatype indicates unsigned integer and it consists of 8 bits with positive range values from 0 to 255. This datatype store information about the type byte order and bit-width with 'C'unsigned character.
We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.
NumPy dtypeA data type object implements the fixed size of memory corresponding to an array. We can create a dtype object by using the following syntax. The constructor accepts the following object. Object: It represents the object which is to be converted to the data type.
np. uint16 is a type object representing the type of array scalars of uint16 dtype. x. dtype is an actual dtype object, and dtype objects implement == in a weird way that's non-transitive and inconsistent with hash .
I was able to track down this closely related github issue on the numpy repo. According to Robert Kern, a major contributor to numpy
and scipy
, the decision about the result type is made based on the input types. numpy
relies on underlying routines that have type-specific implementations where both arguments are of the same type, so it has to promote to some common type. In this case, the issue is that one type is unsigned and the other is signed:
...it's a confluence of several factors. The implementation of the
numpy.add()
ufunc that underlies these routines only has type-specific implementations where the two arguments are of the same type. So the ufunc system needs to cast the arguments to a common type. One of the arguments is signed, so both arguments need to be casted to a signed type. The smallest signed type that can represent values up through the maximum range ofuint64
isfloat64
(Note! Not alluint64
values can be represented asfloat64
floats! Precision is lost! But it's better thanint64
where the whole upper half of the values are lost.).
Note, a similar thing occurs with unsigned and signed numpy types, np.uint
and np.int
:
>>> import numpy as np
>>> np.uint(0) + np.int64(1)
1.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With