I am looking for a way to check if a numpy array is np.float64
or np.float32
. This works fine for np.float64
:
a = np.random.rand(10)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # No exception is raised for np.float64
But fails for np.float32
:
a = np.random.rand(10).astype(np.float32)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # An exception is raised!
float is one of the available numeric data types in Go used to store decimal numbers. float32 is a version of float that stores decimal values composed of 32 bits of data.
What is the difference between NumPy float64 and float? 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.
Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.
Looks float32 has a resolution of 1e-6 and the abs value is valid down to as small as 1.2e-38 . The relative error is at the order of 1e-8 for values above 1e-38, lower than 1e-6 proposed by np.
One way you can check if a data type is a float is with issubdtype
:
In [1]: a = np.random.rand(10).astype(np.float64)
In [2]: b = np.random.rand(10).astype(np.float32)
In [3]: np.issubdtype(a.dtype,np.floating)
Out[3]: True
In [4]: np.issubdtype(b.dtype,np.floating)
Out[4]: True
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