Numpy 将 float32 转换为 float64 [英] Numpy casting float32 to float64

查看:316
本文介绍了Numpy 将 float32 转换为 float64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 python 3 中的 numpy float32 数组执行一些标准操作,但是在使用 numpy sum() 时我看到了一些奇怪的行为.这是一个示例会话:

I want to perform some standard operations on numpy float32 arrays in python 3, however I'm seeing some strange behavior when working with numpy sum(). Here's an example session:

Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin

import numpy as np
np.__version__
Out[3]: '1.12.1'
a = np.ones(10).astype(np.float32)
np.sum(a).dtype
Out[5]: dtype('float32')
(np.sum(a)+1).dtype
Out[6]: dtype('float64')
(np.sum(a)+1.).dtype
Out[7]: dtype('float64')
(a+1).dtype
Out[8]: dtype('float32')

将标量添加到总和的结果(似乎具有 float32dtype)的任何原因都会将其转换回 float64?需要明确的是,我知道我可以将标量显式转换为 float32,但是正如最后一行所示,numpy 在向数组添加标量时仍然尊重 float32.任何解释或建议如何在没有显式转换的情况下保持 float32 的内容?

Any reason why adding a scalar to the result of a sum (which seems to have dtype of float32) would cast it back to float64? To be clear, I know that I can explicitly cast the scalar to float32, however as the last line shows, numpy still respects float32 when adding a scalar to an array. Any explanations or suggestions how to keep things as float32 without explicit casting?

推荐答案

np.sum(a) 的结果是一个 NumPy 标量,而不是一个数组.仅涉及标量的操作使用与涉​​及(正维)NumPy 数组的操作不同的转换规则,在 numpy.result_type.

The result of np.sum(a) is a NumPy scalar, rather than an array. Operations involving only scalars use different casting rules from operations involving (positive-dimensional) NumPy arrays, described in the docs for numpy.result_type.

当操作仅涉及标量(包括 0 维数组)时,结果数据类型完全由输入数据类型决定.仅涉及(正维)数组的操作也是如此.

When an operation involves only scalars (including 0-dimensional arrays), the result dtype is determined purely by the input dtypes. The same is true for operations involving only (positive-dimensional) arrays.

然而,当混合标量和(正维)数组时,NumPy 不使用标量的实际 dtype,而是检查标量的值以查看较小"的 dtype 是否可以容纳它们,然后使用该 dtype对于类型提升.(数组不会经过这个过程,即使它们的值适合更小的 dtype.)

However, when scalars and (positive-dimensional) arrays are mixed, instead of using the actual dtypes of the scalars, NumPy examines the values of the scalars to see if a "smaller" dtype can hold them, then uses that dtype for the type promotion. (The arrays do not go through this process, even if their values would fit in a smaller dtype.)

因此,

np.sum(a)+1

是标量操作,将 1 转换为 dtype int_(int32 或 int64 取决于 C long 的大小),然后基于 dtypes float32 和 int32/int64 执行提升,但是

is a scalar operation, converting 1 to a NumPy scalar of dtype int_ (either int32 or int64 depending on the size of a C long) and then performing promotion based on dtypes float32 and int32/int64, but

a+1

涉及一个数组,所以为了提升的目的,1的dtype被当作int8处理.

involves an array, so the dtype of 1 is treated as int8 for the purposes of promotion.

由于 float32 无法容纳 dtype int32(或 int64)的所有值,因此 NumPy 将在第一次升级时升级为 float64.(float64 不能保存 dtype int64 的所有值,但 NumPy 不会为此提升到 numpy.longdouble.)由于 float32 可以 保存 dtype int8 的所有值,NumPy 坚持使用 float32 作为第二次推广.

Since a float32 can't hold all values of dtype int32 (or int64), NumPy upgrades to float64 for the first promotion. (float64 can't hold all values of dtype int64, but NumPy won't promote to numpy.longdouble for this.) Since a float32 can hold all values of dtype int8, NumPy sticks with float32 for the second promotion.

如果您使用大于 1 的数字,则它不适合 int8:

If you use a number bigger than 1, so it doesn't fit in an int8:

In [16]: (a+1).dtype
Out[16]: dtype('float32')

In [17]: (a+1000000000).dtype
Out[17]: dtype('float64')

您可以看到不同的促销行为.

you can see different promotion behavior.

这篇关于Numpy 将 float32 转换为 float64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆