How do I convert a numpy
array
from type 'float64'
to type 'float'
? Specifically, how do I convert an entire array
with dtype
'float64'
to have dtype
'float'
? Is this possible? The answer for scalars in the thought-to-be duplicate question above does not address my question.
Consider this:
>>> type(my_array[0])
<type 'numpy.float64'>
>>> # Let me try to convert this to 'float':
>>> new_array = my_array.astype(float)
>>> type(new_array[0])
<type 'numpy.float64'>
>>> # No luck. What about this:
>>> new_array = my_array.astype('float')
>>> type(new_array[0])
<type 'numpy.float64'>
>>> # OK, last try:
>>> type(np.inf)
<type 'float'>
>>> # Yeah, that's what I want.
>>> new_array = my_array.astype(type(np.inf))
>>> type(new_array[0])
<type 'numpy.float64'>
If you're unsure why I might want to do this, see this question and its answers.
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.
float64 are numpy specific 32 and 64-bit float types. Thus, when you do isinstance(2.0, np. float) , it is equivalent to isinstance(2.0, float) as 2.0 is a plain python built-in float type... and not the numpy type. isinstance(np.
We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number.
Use numpy. ndarray. astype() to convert a NumPy array of strings to an array of floats.
Yes, actually when you use Python's native float
to specify the dtype for an array , numpy converts it to float64
. As given in documentation -
Note that, above, we use the Python float object as a dtype. NumPy knows that
int
refers tonp.int_
,bool
meansnp.bool_
, thatfloat
isnp.float_
andcomplex
isnp.complex_
. The other data-types do not have Python equivalents.
And -
float_ - Shorthand for float64.
This is why even though you use float
to convert the whole array to float , it still uses np.float64
.
According to the requirement from the other question , the best solution would be converting to normal float object after taking each scalar value as -
float(new_array[0])
A solution that I could think of is to create a subclass for float
and use that for casting (though to me it looks bad). But I would prefer the previous solution over this if possible. Example -
In [20]: import numpy as np
In [21]: na = np.array([1., 2., 3.])
In [22]: na = np.array([1., 2., 3., np.inf, np.inf])
In [23]: type(na[-1])
Out[23]: numpy.float64
In [24]: na[-1] - na[-2]
C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars
if __name__ == '__main__':
Out[24]: nan
In [25]: class x(float):
....: pass
....:
In [26]: na_new = na.astype(x)
In [28]: type(na_new[-1])
Out[28]: float #No idea why its showing float, I would have thought it would show '__main__.x' .
In [29]: na_new[-1] - na_new[-2]
Out[29]: nan
In [30]: na_new
Out[30]: array([1.0, 2.0, 3.0, inf, inf], dtype=object)
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