Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange implicit conversion of data type in numpy

Tags:

python

numpy

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

like image 523
kstn Avatar asked Nov 08 '21 09:11

kstn


People also ask

What is uint8 in NumPy?

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.

How do I change the Dtype of a NumPy array?

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.

What are NumPy data type?

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.

What is NP uint16?

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 .


Video Answer


1 Answers

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 of uint64 is float64 (Note! Not all uint64 values can be represented as float64 floats! Precision is lost! But it's better than int64 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
like image 167
juanpa.arrivillaga Avatar answered Oct 20 '22 05:10

juanpa.arrivillaga