numpy数组中的整数溢出 [英] Integer overflow in numpy arrays
问题描述
import numpy as np
a = np.arange(1000000).reshape(1000,1000)
print(a**2)
使用此代码,我得到了这个答案.为什么我会得到负值?
With this code I get this answer. Why do I get negative values?
[[ 0 1 4 ..., 994009 996004 998001]
[ 1000000 1002001 1004004 ..., 3988009 3992004 3996001]
[ 4000000 4004001 4008004 ..., 8982009 8988004 8994001]
...,
[1871554624 1873548625 1875542628 ..., -434400663 -432404668 -430408671]
[-428412672 -426416671 -424420668 ..., 1562593337 1564591332 1566589329]
[1568587328 1570585329 1572583332 ..., -733379959 -731379964 -729379967]]
推荐答案
在您的平台上,np.arange返回dtype'int32'的数组:
On your platform, np.arange returns an array of dtype 'int32' :
In [1]: np.arange(1000000).dtype
Out[1]: dtype('int32')
数组的每个元素都是32位整数.平方导致结果不适合32位.结果被裁剪为32位,但仍解释为32位整数,这就是为什么看到负数的原因.
Each element of the array is a 32-bit integer. Squaring leads to a result which does not fit in 32-bits. The result is cropped to 32-bits and still interpreted as a 32-bit integer, however, which is why you see negative numbers.
在这种情况下,可以通过在平方之前构造dtype'int64'数组来避免整数溢出:
In this case, you can avoid the integer overflow by constructing an array of dtype 'int64' before squaring:
a=np.arange(1000000,dtype='int64').reshape(1000,1000)
请注意,使用numpy时,发现的问题是固有的危险.您必须谨慎选择dtype,并事先知道您的代码不会导致算术溢出.出于速度考虑,numpy不能也不会在出现这种情况时警告您.
Note that the problem you've discovered is an inherent danger when working with numpy. You have to choose your dtypes with care and know before-hand that your code will not lead to arithmetic overflows. For the sake of speed, numpy can not and will not warn you when this occurs.
请参见 http://mail.scipy.org/pipermail /numpy-discussion/2009-April/041691.html 在numpy邮件列表上对此进行了讨论.
See http://mail.scipy.org/pipermail/numpy-discussion/2009-April/041691.html for a discussion of this on the numpy mailing list.
这篇关于numpy数组中的整数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!