slaw
slaw

Reputation: 6869

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

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!

Upvotes: 5

Views: 1640

Answers (1)

user545424
user545424

Reputation: 16179

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

Upvotes: 4

Related Questions