1D numpy concatenate:TypeError:只能将整数标量数组转换为标量索引

San*_*osh 16 python arrays numpy

我想将numpy数组存储到另一个numpy数组中

我在用 np.concatenate

这是我的代码

x=np.concatenate(x,s_x)
Run Code Online (Sandbox Code Playgroud)

这些是类型和形状 x and s_x

Type of s_x: <class 'numpy.ndarray'>, Shape of s_x: (173,)
Type of x: <class 'numpy.ndarray'> (0,), Shape of x: (0,)
Run Code Online (Sandbox Code Playgroud)

这是显示的错误

TypeError: only integer scalar arrays can be converted to a scalar index
Run Code Online (Sandbox Code Playgroud)

Nil*_*ner 49

您需要将数组作为可迭代(元组或列表)传递,因此正确的语法是

x=np.concatenate((x, s_x))
Run Code Online (Sandbox Code Playgroud)

  • 对于这样一个通用和有用的功能,它确实具有最无用的错误消息。谢谢! (12认同)
  • Python很棒,但是正是这种挑剔有时使它感到脖子疼痛。 (2认同)
  • 我很幸运能够向下滚动谷歌搜索结果并找到这个答案。 (2认同)
  • @UlissesBraga-Neto 问题不在于 Python,而在于 numpy。错误消息应该提供更多信息。 (2认同)