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

查看:4027
本文介绍了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')

为什么要在总和的结果(似乎是dtypefloat32)上添加标量,将其转换回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数组的操作不同的转换规则,有关文档在

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维数组)时,结果dtype仅由输入dtype确定.仅涉及(正维)数组的操作也是如此.

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_ (取决于C long的大小,为int32或int64),然后基于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天全站免登陆