使用“ np.testing.assert_array_equal”的形状不匹配的形状错误在相同的功能和输入上
我想检查两个函数是否得到相同的结果。这种相似性检查无法通过列表比较来完成。结果包含嵌套形式的列表和 NumPy 数组,因此我尝试使用循环将它们更改为 ndarrays,然后尝试 np.testing.assert_array_equal
,但它出现不匹配的形状错误:
值错误:
断言期间出错:回溯(最近一次调用最后一次):
文件“/usr/local/lib/python3.7/dist-packages/numpy/testing/_private/utils.py”,第 817 行,位于assert_array_compare
max_abs_error = max(误差)
文件“<array_function 内部结构>”,第 6 行,位于 amax
文件“/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py”,第 2755 行,在 amax 中 keepdims=keepdims,initial=初始,where=哪里)
文件“/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py”,第 86 行,位于 _wrapreduction
返回 ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError:操作数无法与形状 (27,2,1) (26,2,1) 一起广播数组不相等
x: 数组([数组([[[0.21456296],
[0.66358573]],
...
y: 数组([数组([[[0.21456296],
[0.66358573]],
...
我不知道为什么会发生这种情况,以及它是否与“形状不匹配或冲突值引发异常。”在其文档中;两个功能相同,数据输入相同。
这个问题可以通过以下代码重现:
import numpy as np
cv = np.array([[1, 0], [2, 1], [3, 2], [4, 3], [5, 4]], dtype=np.int32)
vc = np.array([[0.73901752, 0.29121358], [0.43416323, 0.09588229], [0.66348142, 0.31610006],
[0.82838408, 0.91985854], [0.80829238, 0.38857108], [0.95869708, 0.96481968],
[0.02026681, 0.43935289], [0.95321295, 0.30348123], [0.7182516 , 0.85782094],
[0.89697646, 0.99626489]])
change = 0.05
iters = np.arange(3, 8)
mi = np.array([[0.24122257, 0.56981744], [0.24122257, 0.14708398], [0.54651088, 0.14708398],
[0.67472099, 0.20946646], [0.33991152, 0.36013738]])
ma = np.array([[0.66782044, 0.77456042], [0.54651088, 0.77456042], [0.69503927, 0.20946646],
[0.69503927, 0.7711305 ], [0.67472099, 0.7711305 ]])
def ex(cv, vc, iters, change, mi, ma):
rng = np.random.default_rng(85)
Rand_ = []
for i, j in enumerate(cv):
rand_lim = []
for m in range(iters[i]):
rand_x = rng.uniform(mi[i, 0] - change, ma[i, 0] + change, 1)
rand_y = rng.uniform(mi[i, 1] - change, ma[i, 1] + change, 1)
rand_lim.append([rand_x, rand_y])
Rand_.append(rand_lim)
return Rand_
def new(cv, vc, iters, change, mi, ma):
rng = np.random.default_rng(85)
Rand_ = []
for i, j in enumerate(cv):
rand_lim = []
for m in range(iters[i]):
rand_x = rng.uniform(mi[i, 0] - change, ma[i, 0] + change, 1)
rand_y = rng.uniform(mi[i, 1] - change, ma[i, 1] + change, 1)
rand_lim.append([rand_x, rand_y])
Rand_.append(rand_lim)
return Rand_
ex_ = ex(cv, vc, iters, change, mi, ma)
new_ = new(cv, vc, iters, change, mi, ma)
ex_ = np.array([np.array(xi) for xi in ex_])
new_ = np.array([np.array(xi) for xi in new_])
# ex_ = np.array([np.array(xi, dtype=float) for xi in ex_], dtype=object)
# new_ = np.array([np.array(xi, dtype=float) for xi in new_], dtype=object)
np.testing.assert_array_equal(ex_, new_)
为什么会发生这个错误?
I want to check if two functions get the same result. This similarity check couldn't be done by list comparisons. The results contains both lists and NumPy arrays in nested form, so I tried to change them to ndarrays using loops and then try np.testing.assert_array_equal
, but it get an unmatched shapes error:
ValueError:
error during assertion:Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/numpy/testing/_private/utils.py", line 817, in assert_array_compare
max_abs_error = max(error)
File "<array_function internals>", line 6, in amax
File "/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py", line 2755, in amax
keepdims=keepdims, initial=initial, where=where)
File "/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py", line 86, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: operands could not be broadcast together with shapes (27,2,1) (26,2,1)Arrays are not equal
x: array([array([[[0.21456296],
[0.66358573]],
...
y: array([array([[[0.21456296],
[0.66358573]],
...
I don't know why this happened and if it is related to "An exception is raised at shape mismatch or conflicting values." in its documentation; Both the functions are the same with same data inputs.
This problem can be reproduced by the following codes:
import numpy as np
cv = np.array([[1, 0], [2, 1], [3, 2], [4, 3], [5, 4]], dtype=np.int32)
vc = np.array([[0.73901752, 0.29121358], [0.43416323, 0.09588229], [0.66348142, 0.31610006],
[0.82838408, 0.91985854], [0.80829238, 0.38857108], [0.95869708, 0.96481968],
[0.02026681, 0.43935289], [0.95321295, 0.30348123], [0.7182516 , 0.85782094],
[0.89697646, 0.99626489]])
change = 0.05
iters = np.arange(3, 8)
mi = np.array([[0.24122257, 0.56981744], [0.24122257, 0.14708398], [0.54651088, 0.14708398],
[0.67472099, 0.20946646], [0.33991152, 0.36013738]])
ma = np.array([[0.66782044, 0.77456042], [0.54651088, 0.77456042], [0.69503927, 0.20946646],
[0.69503927, 0.7711305 ], [0.67472099, 0.7711305 ]])
def ex(cv, vc, iters, change, mi, ma):
rng = np.random.default_rng(85)
Rand_ = []
for i, j in enumerate(cv):
rand_lim = []
for m in range(iters[i]):
rand_x = rng.uniform(mi[i, 0] - change, ma[i, 0] + change, 1)
rand_y = rng.uniform(mi[i, 1] - change, ma[i, 1] + change, 1)
rand_lim.append([rand_x, rand_y])
Rand_.append(rand_lim)
return Rand_
def new(cv, vc, iters, change, mi, ma):
rng = np.random.default_rng(85)
Rand_ = []
for i, j in enumerate(cv):
rand_lim = []
for m in range(iters[i]):
rand_x = rng.uniform(mi[i, 0] - change, ma[i, 0] + change, 1)
rand_y = rng.uniform(mi[i, 1] - change, ma[i, 1] + change, 1)
rand_lim.append([rand_x, rand_y])
Rand_.append(rand_lim)
return Rand_
ex_ = ex(cv, vc, iters, change, mi, ma)
new_ = new(cv, vc, iters, change, mi, ma)
ex_ = np.array([np.array(xi) for xi in ex_])
new_ = np.array([np.array(xi) for xi in new_])
# ex_ = np.array([np.array(xi, dtype=float) for xi in ex_], dtype=object)
# new_ = np.array([np.array(xi, dtype=float) for xi in new_], dtype=object)
np.testing.assert_array_equal(ex_, new_)
Why this error happened?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论