Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.float not matching both np.float32 and np.float64

Tags:

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!
like image 825
slaw Avatar asked May 17 '19 14:05

slaw


People also ask

Is float and float32 same?

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 float and numpy float64?

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.

Is python float 32 or 64?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.

How precise is float32?

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.


1 Answers

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
like image 131
user545424 Avatar answered Oct 05 '22 01:10

user545424