gpt4 book ai didi

Python valueError 使用 hstack() (ValueError : all the input array dimensions except for the concatenation axis must match exactly)

转载 作者:行者123 更新时间:2023-12-05 06:39:19 37 4
gpt4 key购买 nike

我收到以下错误:

Traceback (most recent call last):
File "/home/odroid/trackAndFollow/getPositions.py", line 34, in
<module>
tfVeloToCamera = np.hstack((np.vstack((rmVeloToCamera,
zero_array)),np.transpose(translationVector_veloToCamera)))
File "/usr/lib/python2.7/dist-packages/numpy/core/shape_base.py", line
280, in hstack
return _nx.concatenate(arrs, 1)
ValueError: all the input array dimensions except for the
concatenation axis must match exactly

代码:

rotationVector_veloToCamera = 
np.array([[[-1.77611954,0.30024612,0.76069987]]])
translationVector_veloToCamera = np.array([[
0.0146381,0.02553223,0.16231193]])

rmVeloToCamera,jac = cv2.Rodrigues(rotationVector_veloToCamera)
tfVeloToCamera = np.hstack((np.vstack((rmVeloToCamera,
zero_array)),np.transpose(translationVector_veloToCamera)))

我在某处读到原因是形状或 dtype。hstack 中 2 个变量的形状是 (4,3) (3,1)并且都有 dtype=float64

知道是什么导致了这个问题吗?

最佳答案

如果您尝试堆叠的数组的形状是 (4, 3)(3, 1),那么它们没有任何匹配数组维度,如 4 != 33 != 1。如果您要对第一个进行转置,给定维度 (3, 4)(3, 1),它们应该堆叠。

np.hstack((np.zeros((4, 3)), np.zeros((3,1))))
Traceback (most recent call last):
...
ValueError: all the input array dimensions except for the concatenation axis must match exactly

np.hstack((np.zeros((4, 3)).T, np.zeros((3,1))))
Out[56]:
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])

顺便说一句,您可能希望转而使用 np.concatenate 并指定一个轴,而不是 np.vstacknp. hstack.

例如:

np.concatenate((np.zeros((4, 3)), np.zeros((3,1)).T), axis=0)
Out[20]:
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])

关于Python valueError 使用 hstack() (ValueError : all the input array dimensions except for the concatenation axis must match exactly),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719424/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com