gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:59:53 29 4
gpt4 key购买 nike

我正在寻找一种方法来检查 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com