使用hstack()的python valueError(ValueError:除了连接轴之外的所有输入数组维度必须完全匹配)

时间:2017-06-23 10:40:58

标签: python numpy

我收到以下错误:

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)))

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

任何想法导致这个?

1 个答案:

答案 0 :(得分:1)

如果您尝试堆叠的数组的形状为(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.]])