Python NumPy数组_equal函数未按预期工作

Python NumPy数组_equal函数未按预期工作,python,arrays,numpy,Python,Arrays,Numpy,我从NumPy中观察到函数array_equal的异常行为。我肯定我遗漏了一个非常简单的细节 我试图完成的是一个单元测试 事实是,我有数组1: In[21]: bounds['lowerBound'] Out[21]: array([ 1. , 1.2, 1.8]) 我有数组2: In[22]: res['lowerBound'] Out[22]: array([ 1. , 1.2, 1.8]) 为了以防万一,我检查了它们的形状: In[26]: bounds['lowerBou

我从
NumPy
中观察到函数
array_equal
的异常行为。我肯定我遗漏了一个非常简单的细节

我试图完成的是一个单元测试

事实是,我有数组1:

In[21]: bounds['lowerBound']

Out[21]: array([ 1. ,  1.2,  1.8])
我有数组2:

In[22]: res['lowerBound']

Out[22]: array([ 1. ,  1.2,  1.8])
为了以防万一,我检查了它们的形状:

In[26]: bounds['lowerBound'].shape
Out[26]: (3,)
In[28]: res['lowerBound'].shape
Out[28]: (3,)
还包括数据类型:

In[30]: res['lowerBound'].dtype
Out[30]: dtype('float64')
In[31]: bounds['lowerBound'].dtype
Out[31]: dtype('float64')
当我试图验证它们是否相同时:

In[29]:  np.array_equal(bounds['lowerBound'],res['lowerBound'])
Out[29]: False
这怎么可能

提前谢谢

编辑: 用于生成数据的代码为:

bounds={'lowerBound':np.array([1.,1.2,1.8]), 'upperBound':np.array([10.,12.,18.])}
通过以下函数生成
res
字典:

def generateAdjustedBounds(self,universeMktCap,lowerBound,upperBound):
    lb=np.zeros(len(universeMktCap))
    ub=np.zeros(len(universeMktCap))
    lb[0]=lowerBound
    ub[0]=upperBound

    for dat in range(1,len(lb)):
        lb[dat]=lb[dat-1]*universeMktCap[dat]/universeMktCap[dat-1]
        ub[dat]=ub[dat-1]*universeMktCap[dat]/universeMktCap[dat-1]

    Bounds={'lowerBound':np.array(lb),'upperBound':np.array(ub)}

    return Bounds

因为您的元素是浮动的,所以您可能应该改用allclose()


这里的数据类型是什么,您可以发布代码来创建阵列吗?它非常广泛。但我会检查数据类型。我对python很陌生。。。等一下!谢谢使用
np.设置打印选项(精度=16)
查看值之间的差异。这很有效!谢谢但何时建议使用数组_equal?当数组包含整数时,请使用数组_equal。这是因为当数学等效计算中引入小舍入误差时,应表示相同数字的任何两个浮点数通常都不表示整数。