【问题标题】:Using numpy.fill with Numba 'nopython' mode将 numpy.fill 与 Numba 'nopython' 模式一起使用
【发布时间】:2020-05-09 04:18:19
【问题描述】:

我正在尝试使用 numpy 填充函数来填充 ndarray 中的值。我在使用 Numba 的函数下有这个,我得到一个属性错误。这是我的代码示例:

@jit(nopython=True)
def computething(param1):
    x = np.sum(param1)
    x1 = np.zeros(10)
    x1.fill(x)

*请注意,这只是一个示例代码。

我收到以下错误:

UntypedAttributeError: Unknown attribute 'fill' of type array(float64, 1d, C)

如何防止此错误?谢谢!

【问题讨论】:

  • 旁注:我认为你不应该使用numba 来实现该功能,因为你只使用numpy 功能,没有python
  • 确切的输入是什么?一个简单的数组,例如 [1,2,3] 工作正常
  • 是的,它在 nopython=True 选项打开的情况下不起作用——在它关闭的情况下它工作正常。该功能有一些额外的 Numba 兼容功能,需要将其打开以加快处理速度。但是,这仅在使用 numpy 填充功能时才会中断。确切的输入是一个数组,然后通过求和将其缩小为单个数字,然后使用 numpy fill 用该单个值填充新数组。

标签: python numpy numba


【解决方案1】:

可行的解决方案是:

@jit(nopython=True)
def computething(param1):
    x = np.sum(param1)
    x1 = np.zeros(10)
    x1[:] = x

但是,当设置 nopython=True 时,numpy 填充函数仍然会给出属性错误。使用 nopython=False 可以正常工作。

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2020-06-15
    • 1970-01-01
    • 2017-08-08
    • 2020-09-15
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多