我有两个数组,
A = np.array([[1,2,3],[4,5,6]])
b = np.array([100,101])
我想连接它们,以便b
在右侧添加一列,这样我们就有了一个新数组A | b
,类似于:
1 2 3 100
4 5 6 101
我正在尝试以这种方式连接:
new = np.concatenate((A, b), axis=1)
但我收到下一个错误:
ValueError: all the input arrays must have the same number of dimensions, but the array at index 0 has 2 dimension(s), and the array at index 1 has 1 dimension(s)
如何连接这两个数组?
您可以使用column_stack
:
>>> np.column_stack((A, b))
array([[ 1, 2, 3, 100],
[ 4, 5, 6, 101]])
这需要注意b
不是 2D。
为了concatenate
工作,我们手动制作b
形状(2, 1)
:
>>> np.concatenate((A, b[:, np.newaxis]), axis=1)
array([[ 1, 2, 3, 100],
[ 4, 5, 6, 101]])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句