numpy.array_equal returns False, even though arrays have the same shape and values

别等时光非礼了梦想. 提交于 2020-01-24 15:16:13

问题


I have a very simple function, as shown below

def new_price(A, B, x):
    return np.linalg.inv(A @ B) @ x

These are the inputs I give it

A = np.array([
    [2, 0, 1, 0],
    [1, 1, 1, 1],
    [0, 0, 0, 10]
]) 

B = np.array([
    [3, 3, 3],
    [2, 0, 8],
    [0, 5, 3],
    [0, 0, 10] 
])

x = np.array([ 84, 149, 500])

This returns the array [ 1. 3. 5.]. But, when I make the following equality check, it returns False

v1 = new_price(A, B, x)
v2 = np.array([1.0, 3.0, 5.0])
np.array_equal(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]))

I checked and the shapes and the types of both the arrays are same. What am I missing here?


回答1:


The are not exactly equal:

>>> new_price(A, B, [ 84, 149, 500]) -  np.array([1, 3, 5])
array([  2.84217094e-14,  -1.42108547e-14,   0.00000000e+00])

Better use np.allclose():

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]))
True

Returns True if two arrays are element-wise equal within a tolerance.

You can adjust the relative and absolute tolerance.

Still true for really small values:

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]),
                atol=1e-13, rtol=1e-14)
True

Found the limit:

>>> np.allclose(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]),
                atol=1e-14, rtol=1e-14)
False


来源:https://stackoverflow.com/questions/48527910/numpy-array-equal-returns-false-even-though-arrays-have-the-same-shape-and-valu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!