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

1.4k Views Asked by At

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!
1

There are 1 best solutions below

3
On BEST ANSWER

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