>>> numpy.float64(5.9975).hex()
'0x1.7fd70a3d70a3dp+2'
>>> (5.9975).hex()
'0x1.7fd70a3d70a3dp+2'
They are the same number. What differs is the textual representation obtained via by their __repr__ method; the native Python type outputs the minimal digits needed to uniquely distinguish values, while NumPy code before version 1.14.0, released in 2018 didn't try to minimise the number of digits output.
Numpy float64 vs Python float - Stack Overflow
BUG: Shouldn't numpy's floating types (e.g. `np.float_`) and python's native `float` type be compatible?
Preventing numpy from converting float type to numpy.int64 type
Prevent numpy array from turning my floats into strings
Videos
>>> numpy.float64(5.9975).hex()
'0x1.7fd70a3d70a3dp+2'
>>> (5.9975).hex()
'0x1.7fd70a3d70a3dp+2'
They are the same number. What differs is the textual representation obtained via by their __repr__ method; the native Python type outputs the minimal digits needed to uniquely distinguish values, while NumPy code before version 1.14.0, released in 2018 didn't try to minimise the number of digits output.
Numpy float64 dtype inherits from Python float, which implements C double internally. You can verify that as follows:
isinstance(np.float64(5.9975), float) # True
So even if their string representation is different, the values they store are the same.
On the other hand, np.float32 implements C float (which has no analog in pure Python) and no numpy int dtype (np.int32, np.int64 etc.) inherits from Python int because in Python 3 int is unbounded:
isinstance(np.float32(5.9975), float) # False
isinstance(np.int32(1), int) # False
So why define np.float64 at all?
np.float64 defines most of the attributes and methods in np.ndarray. From the following code, you can see that np.float64 implements all but 4 methods of np.array:
[m for m in set(dir(np.array([]))) - set(dir(np.float64())) if not m.startswith("_")]
# ['argpartition', 'ctypes', 'partition', 'dot']
So if you have a function that expects to use ndarray methods, you can pass np.float64 to it while float doesn't give you the same.
For example:
def my_cool_function(x):
return x.sum()
my_cool_function(np.array([1.5, 2])) # <--- OK
my_cool_function(np.float64(5.9975)) # <--- OK
my_cool_function(5.9975) # <--- AttributeError
Let's say I have 5 lists, list1, list2, list3, list4, list5.
The first 3 are lists of strings e.g. while the last 2 are lists of floats e.g.
list1 = ['1','2','3'...] list2 = list3 = list1 list4 = [1.0,2.0,3.0...] list5 = list4
All lists are the same length.
I'd like to turn them into a numpy array, while preserving their original datatypes. I say
intermediateList = zip(list1,list2,list3,list4,list5)
which gives me a list (length=5) of lists, with the original data types preserved. Perfect.
However, when I try to convert to numpy.array
myArray=asarray(intermediateList)
numPy converts everything into numpy_string type, which is very irritating. I can now no longer perform numerical operations on list4 and list5 without re-converting the values to float.
(as a sidenote, if I were to try to fix this by saying
list5=list5.astype(float)
it will convert to float in the console, however when I call list5 again it is back to numpy_string! anyone know why?)
Any suggestions?