我正在寻找一种方法来检查 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
但是 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 的一种方法是使用 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
关于python - np.float 不匹配 np.float32 和 np.float64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188691/