array1.shape
给(180),array2.shape
给(180,1)
这两个有什么不同?由于这种差异,我无法使用以下命令堆叠它们
np.vstack((array2, array1))
我应该对array1形状进行哪些更改才能将它们堆叠起来?
发布于 2016-09-13 05:02:08
让我们定义一些数组:
>>> x = np.zeros((4, 1))
>>> y = np.zeros((4))
按原样,这些数组无法堆叠:
>>> np.vstack((x, y))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/numpy/core/shape_base.py", line 230, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
但是,通过简单的更改,它们将堆叠在一起:
>>> np.vstack((x, y[:, None]))
array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]])
或者:
>>> np.vstack((x[:, 0], y))
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
发布于 2016-09-13 05:22:15
In [81]: x1=np.ones((10,)); x2=np.ones((10,1))
一个数组是一维的,另一个是二维的。vertical
堆栈需要2个维度,垂直和水平。因此,np.vstack
通过np.atleast_2d
传递每个输入
In [82]: np.atleast_2d(x1).shape
Out[82]: (1, 10)
但是现在我们有一个(1,10)数组和一个(10,1)数组-它们不能在任一轴上连接。
但如果我们重塑x1
,使其成为(10,1)
,那么我们可以在任何一个方向上与x2
结合:
In [83]: np.concatenate((x1[:,None],x2), axis=0).shape
Out[83]: (20, 1)
In [84]: np.concatenate((x1[:,None],x2), axis=1).shape
Out[84]: (10, 2)
打印出两个数组:
In [86]: x1
Out[86]: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [87]: x2
Out[87]:
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
如何在不做任何调整的情况下将这两个形状连接起来?
https://stackoverflow.com/questions/39462433
复制相似问题