本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

Numpy中使用astype(np.float32)后结果却为float64的原因是什么?

网络教程 app 1℃

Numpy中使用astype(np.float32)后结果却为float64的原因是什么?

numpy 指定 astype 为 float32 结果却是 float64 的原因

在图像预处理函数中,你使用 image = image.astype(np.float32) 将图像数组转换为 float32 类型。然而,打印结果显示为 float64。

这是因为后续的操作 image = (image – mean) / std 中,mean 和 std 都是 float64 类型。浮点运算遵循精度最高的输入类型,因此结果也变为 float64。

示例代码

image = np.array([1, 2, 3])image = image.astype(np.float32)mean = np.array([0.5, 1.0, 1.5]).astype(np.float64)std = np.array([0.2, 0.3, 0.4]).astype(np.float64)# 结果为 float64result = (image – mean) / stdprint(result.dtype) # 输出:float64

以上就是Numpy 中使用 astype(np.float32) 后结果却为 float64 的原因是什么?的详细内容,更多请关注范的资源库其它相关文章!

转载请注明:范的资源库 » Numpy中使用astype(np.float32)后结果却为float64的原因是什么?

喜欢 (0)