【问题标题】:numpy concatenate error " only integer scalar arrays can be converted to a scalar index"numpy 连接错误“只有整数标量数组可以转换为标量索引”
【发布时间】:2020-11-10 01:37:54
【问题描述】:

我有 numpy 数据

x = [[1. 2.2 3.4] [3. 4. 5. ]]

y = [[2.6660993 3.6791213 3.7325573]]

只想把这些串联起来,结果应该是这样的。

[[1. 2.2 3.4] [3. 4. 5. ] [2.6660993 3.6791213 3.7325573] ]

但是,np.concatenate(x,y) 显示错误。

only integer scalar arrays can be converted to a scalar index

【问题讨论】:

  • concatenate 的第二个参数是axis,它必须是一个整数!

标签: python numpy


【解决方案1】:

函数 concatenate 接收一个 ndarrays 元组:

import numpy as np
x = np.array( [[1,2,3],[4,5,6]] )
y = np.array([[7,8,9]])
z = np.concatenate((x,y))
print(z)

【讨论】:

    【解决方案2】:

    你想要

    np.concatenate((x, y[None,:]))
    

    请注意,要连接的东西是作为一个序列传递的,并且除了轴(默认为 0)之外,它们在所有维度上都必须相同。

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 2021-10-31
      • 2018-05-29
      • 2018-04-04
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      相关资源
      最近更新 更多