ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1相关的知识,希望对你有一定的参考价值。

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

目录

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

问题:

解决:

完整错误:


问题:

import numpy as np
# create a 1d array
ar1 = np.array([1, 2])
# create a 2d array
ar2 = np.array([[0, 0, 0],
                [1, 1, 1]])

# hstack the arrays
ar_h = np.hstack((ar1, ar2))
# display the concatenated array
print(ar_h)

解决:

import numpy as np
# create a 1d array
ar1 = np.array([[1, 2],
               [1, 2]])
# create a 2d array 
ar2 = np.array([[0, 0, 0],
                [1, 1, 1]])

# hstack the arrays
ar_h = np.hstack((ar1, ar2))
# display the concatenated array
print(ar_h)


*********************************************************************

[[1 2 0 0 0]
 [1 2 1 1 1]]

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-f0ed0654d3e0> in <module>
      7 
      8 # hstack the arrays
----> 9 ar_h = np.hstack((ar1, ar2))
     10 # display the concatenated array
     11 print(ar_h)

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

D:\\anaconda\\lib\\site-packages\\numpy\\core\\shape_base.py in hstack(tup)
    341     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    342     if arrs and arrs[0].ndim == 1:
--> 343         return _nx.concatenate(arrs, 0)
    344     else:
    345         return _nx.concatenate(arrs, 1)

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

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
创作打卡挑战赛 赢取流量/现金/CSDN周边激励大奖

以上是关于ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:具有多个元素的数组的真值不明确。在绘制框架时在 plt.show() 中使用 a.any() 或 a.all()

NLP/TF-IDF: ValueError: 具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。 & [复制]

OneHotEncoder : ValueError: Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

If 语句,ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

为啥我会收到 ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()