import numpy as np
np.double is np.float64 # returns True
As you can see they are virtually the same
You can see that in the docs here where it is mentioned that np.float64 is just an alias for np.double
failure to convert numpy.float64 into System.Double
Converting numpy float64 of python float to Decimal is not the same (get rounded in numpy case)
floating point - How to convert 64-bit to double decimal in Python? - Stack Overflow
floating point - convert double to float in Python - Stack Overflow
You can do something like this:
from numpy import * #for Double data type
double(int('0100000011111000011010100000000000000000000000000000000000000000',2))
int_data = int('0100000011111000011010100000000000000000000000000000000000000000',2)
double_data = struct.unpack('d', struct.pack('q', int_data))[0]
You can use the struct module to play with numerical representations:
import struct
>>> struct.unpack("f", struct.pack("f", 0.00582811585976))
(0.005828116089105606,)
It looks plausible:
>>> v1 = 0.00582811585976
>>> v2 = 0.00582811608911
>>> import numpy as np
>>> np.float32(v1)
0.0058281161
>>> float(np.float32(v1)) #convert to 32bit and then back to 64bit
0.005828116089105606 #This rounds to v2 if you're printing 14 places of precision ...
>>> '%.14f'%np.float32(v1)
'0.00582811608911'
>>> '%.14f'%np.float32(v1) == '%.14f'%v2
True
import numpy as np
np.double is np.float64 # returns True
In theory both should be same.
I felt my answer was misleading and I didn't notice the bug mentioned in other answers. So here is the update:
The benchmark difference wasn't caused by the used types - checkout other answers as it was a bug.
Regarding types: according to the documentation it's up to the used platform implementation to decide with number of bits used to represent double: Double-precision floating-point number type, compatible with Python float and C double. (numpy docs). Hence, in the case of x86_64 it will be just an alias to np.float64. With some other platforms it may not be true. If such choice depends on the system, you'll benefit from the native performance, but it may introduce bugs.
If you run the benchmark with the fix, calculated time values should be almost the same.
I think you're comparing apples with oranges.
The first bench is basically a * b but the second a * a.
I suspect much less cache misses for the latter.
Python's built-in float type has double precision (it's a C double in CPython, a Java double in Jython). If you need more precision, get NumPy and use its numpy.float128.
Decimal datatype
- Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem.
If you are pressed by performance issuses, have a look at GMPY
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
intrefers tonp.int_,boolmeansnp.bool_, thatfloatisnp.float_andcomplexisnp.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)
You can create an anonymous type float like this
>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>
Use val.item() to convert most NumPy values to a native Python type:
import numpy as np
# for example, numpy.float32 -> python float
val = np.float32(0)
pyval = val.item()
print(type(pyval)) # <class 'float'>
# and similar...
type(np.float64(0).item()) # <class 'float'>
type(np.uint32(0).item()) # <class 'int'>
type(np.int16(0).item()) # <class 'int'>
type(np.cfloat(0).item()) # <class 'complex'>
type(np.datetime64(0, 'D').item()) # <class 'datetime.date'>
type(np.datetime64('2001-01-01 00:00:00').item()) # <class 'datetime.datetime'>
type(np.timedelta64(0, 'D').item()) # <class 'datetime.timedelta'>
...
(A related method np.asscalar(val) was deprecated with 1.16, and removed with 1.23).
For the curious, to build a table of conversions of NumPy array scalars for your system:
for name in dir(np):
obj = getattr(np, name)
if hasattr(obj, 'dtype'):
try:
if 'time' in name:
npn = obj(0, 'D')
else:
npn = obj(0)
nat = npn.item()
print('{0} ({1!r}) -> {2}'.format(name, npn.dtype.char, type(nat)))
except:
pass
There are a few NumPy types that have no native Python equivalent on some systems, including: clongdouble, clongfloat, complex192, complex256, float128, longcomplex, longdouble and longfloat. These need to be converted to their nearest NumPy equivalent before using .item().
If you want to convert (numpy.array OR numpy scalar OR native type OR numpy.darray) TO native type you can simply do :
converted_value = getattr(value, "tolist", lambda: value)()
tolist will convert your scalar or array to python native type. The default lambda function takes care of the case where value is already native.