np.float 不匹配 np.float32 和 np.float64

sla*_*law 5 python numpy stumpy

我正在寻找一种方法来检查 numpy 数组是否为np.float64或np.float32。这适用于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
Run Code Online (Sandbox Code Playgroud)

但失败了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!
Run Code Online (Sandbox Code Playgroud)

use*_*424 4

检查数据类型是否为浮点数的一种方法是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
Run Code Online (Sandbox Code Playgroud)