【问题标题】:Using np.where in cython as index在 cython 中使用 np.where 作为索引
【发布时间】:2020-02-26 20:22:37
【问题描述】:

我正在尝试在 cython 程序中使用 np.where 来选择值在半径内的位置。通常这适用于 numpy,但使用 cython 我得到 ​​p>

TypeError: 'tuple' 对象不能被解释为整数

def test(double[:,::1] array, double[:] point, double radius):
    array = np.asarray(array)
    idx = np.where(np.logical_and(np.greater_equal(array[:, 0], point[0] - radius), np.less_equal(array[:, 0], point[0] + radius)))
    a = array[idx]

我尝试这样做但出错了

只有整数标量数组可以转换为标量索引

a = array[idx[0].astype(int)]

【问题讨论】:

  • 我想你想要一个 = array[idx[0]]
  • 这给出了一个类型错误:只有整数标量数组可以转换为标量索引.....因为我相信输出是数组的元组。我也试过做 a = array[idx[0].astype(int)]

标签: python numpy indexing cython where-clause


【解决方案1】:

奇怪的是,即使我已经做了 np.asarray,它也能做到这一点

a = np.array(array)[idx[0]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2019-05-18
    • 2020-04-29
    • 2011-11-18
    • 2014-01-26
    相关资源
    最近更新 更多