【问题标题】:IndexError when evaluating ndarray with indexes in another ndarray使用另一个 ndarray 中的索引评估 ndarray 时出现 IndexError
【发布时间】:2016-02-19 03:39:07
【问题描述】:

我正在尝试从具有另一个 ndarray 中的索引的 ndarray 中获取值,但我不断收到此错误

IndexError 数组索引过多。

我试图从中获取值的数组 scores 具有 scores.shape = (10,10000) 指向索引的数组 indicesindices.shape = (10000,2)

我正在尝试以这种方式获取值:

values = scores[tuple(indices)]

但这是我得到错误的地方。

我试图以这种方式访问​​多个单独的分数值,例如scores[0,6], scores[1,9] 在另一个数组中,所以我得到类似的东西

[scores[0,6],scores[1,9],...] 

一气呵成,避免循环。那些[[0,6] , [1,9], ...] 存储在索引数组中。我提到了前面的内容,以防万一。

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    尝试以下操作:scores[indices[:,0],indices[:,1]]。或者,scores[tuple(indices.T)]

    当您执行scores[tuple(indices)] 时,tuple(indices) 正在创建一个 2 元素数组的元组。当您尝试获取 10,000 维数组的 2 个元素时,Numpy 会解释这一点!对于您需要的索引类型,Numpy 需要每个维度的值数组。换句话说,不是( [x1,y1], [x2,y2] ),而是( [x1,x2], [y1, y2] )

    【讨论】:

    • 你写下的最后一行让我很受启发!谢谢@cge ;)
    猜你喜欢
    • 2021-03-14
    • 2018-10-10
    • 1970-01-01
    • 2021-10-15
    • 2018-06-05
    • 2018-06-05
    • 2020-03-26
    • 1970-01-01
    • 2018-04-12
    相关资源
    最近更新 更多