如果我有两条ndarray:
a.shape # returns (200,300, 3)
b.shape # returns (200, 300)
numpy.vstack((a,b)) # Gives error将打印出错误: ValueError:数组必须具有相同的维数
我试着做了vstack((a.reshape(-1,300), b),但输出非常奇怪。
发布于 2012-10-15 00:59:46
您不需要指定您实际想要的最终形状。如果是(200,300,4),你可以改用dstack:
>>> import numpy as np
>>> a = np.random.random((200,300,3))
>>> b = np.random.random((200,300))
>>> c = np.dstack((a,b))
>>> c.shape
(200, 300, 4)基本上,当你在堆叠时,长度必须在所有其他轴上一致。
基于评论更新:
如果您想要(800,300),您可以尝试如下所示:
>>> a = np.ones((2, 3, 3)) * np.array([1,2,3])
>>> b = np.ones((2, 3)) * 4
>>> c = np.dstack((a,b))
>>> c
array([[[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]],
[[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]]])
>>> c.T.reshape(c.shape[0]*c.shape[-1], -1)
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 3., 3., 3.],
[ 3., 3., 3.],
[ 4., 4., 4.],
[ 4., 4., 4.]])
>>> c.T.reshape(c.shape[0]*c.shape[-1], -1).shape
(8, 3)https://stackoverflow.com/questions/12884362
复制相似问题