首页 > 解决方案 > 如何连接具有不同尺寸(形状)的ndrarray?

问题描述

我有两个数据类型是 ndarray。我想合并 X_train 和 Y_train,但它们的形状不同。我尝试使用连接,堆栈,但它显示了同样的错误。

print(X_train.shape)
print(type(X_train[1]))
print(Y_train.shape)
print(type(Y_train[1]))

(17731, 30, 14, 1)
<class 'numpy.ndarray'>
(17731, 1)
<class 'numpy.ndarray'>

x_train_tt = np.stack([X_train , Y_train])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-74b8e20eca7c> in <module>

----> 3 x_train_tt = np.stack([X_train , Y_train])

<__array_function__ internals> in stack(*args, **kwargs)

~\anaconda3\envs\newenvironment2.3.6\lib\site-packages\numpy\core\shape_base.py in stack(arrays, axis, out)
    424     shapes = {arr.shape for arr in arrays}
    425     if len(shapes) != 1:
--> 426         raise ValueError('all input arrays must have the same shape')
    427 
    428     result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape

有什么解决办法吗?

我想拥有这样的身材

x_train_tt.shape (17731,30,15,1)

标签: numpystackconcatenationnumpy-ndarray

解决方案


推荐阅读