【问题标题】:Can somebody explain to me why numpy indexing fails when using a 1 row matrix?有人可以向我解释为什么使用 1 行矩阵时 numpy 索引会失败吗?
【发布时间】:2022-08-20 20:26:49
【问题描述】:

为了简单起见:

points_3D_db.shape = (2816, 4233, 3)
index = array([33, 2860], dtype=int16)

这些作品:

points_3D_db[33, 2860] = array([ 1.54911746, -2.87904632,  7.43229437])
points_3D_db[(33, 2860)] = array([ 1.54911746, -2.87904632,  7.43229437])
points_3D_db[index[0], index[1]] = array([ 1.54911746, -2.87904632,  7.43229437])

这不起作用:

points_3D_db[index] = *** IndexError: index 2860 is out of bounds for axis 0 with size 2816

现在为什么 numpy 会抛出这个错误?

    标签: python numpy


    【解决方案1】:

    您需要使用元组来索引:

    points_3D_db[tuple(index)]) = array([ 1.54911746, -2.87904632,  7.43229437])
    

    例子:

    points_3D_db = np.zeros((2816, 4233, 3))
    index = np.array([33, 2860], dtype=np.int16)
    
    points_3D_db[tuple(index)] = [1,2,3]
    
    print(points_3D_db[index[0], index[1]])
    # [1. 2. 3.]
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多