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

问题:

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)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年2月25日 下午10:51
下一篇 2023年2月25日 下午10:52

相关推荐

此站出售,如需请站内私信或者邮箱!