numba不接受dtype=obj的numpy数组

2024-03-29 12:56:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个空数组,我想在每个索引[I,j]处填充任意长度的列表。所以我初始化了一个空数组,这个数组应该包含这样的对象:

@jit(nopython=True, parrallel=True)
def numba_function():
    values          = np.empty((length, length), dtype=object)
    for i in range(10):
        for j in range(10):
            a_list_of_things = [1,2,3,4]
            values[i,j] = a_list_of_things

此操作失败的原因是:

^{pr2}$

如果我通过设置nopython=False关闭numba,代码可以正常工作。在values数组中设置dtype=list并不能改善情况。在

有什么妙招可以克服这个问题吗?在


Tags: of对象intrue列表forrange数组
1条回答
网友
1楼 · 发布于 2024-03-29 12:56:43

nopython模式下的Numba(从0.43.1版起)不支持对象数组。在

键入对象数组的正确方法是:

import numba as nb
import numpy as np

@nb.njit
def numba_function():
    values = np.empty((2, 2), np.object_)
    return values

但如前所述,这是行不通的:

^{pr2}$

这也在the numba documentation中提到:

2.7.1. Scalar types

Numba supports the following Numpy scalar types:

  • Integers: all integers of either signedness, and any width up to 64 bits
  • Booleans
  • Real numbers: single-precision (32-bit) and double-precision (64-bit) reals
  • Complex numbers: single-precision (2x32-bit) and double-precision (2x64-bit) complex numbers
  • Datetimes and timestamps: of any unit
  • Character sequences (but no operations are available on them)
  • Structured scalars: structured scalars made of any of the types above and arrays of the types above

The following scalar types and features are not supported:

  • Arbitrary Python objects
  • Half-precision and extended-precision real and complex numbers
  • Nested structured scalars the fields of structured scalars may not contain other structured scalars

[...]

2.7.2. Array types

Numpy arrays of any of the scalar types above are supported, regardless of the shape or layout.

(重点是我的)

由于dtype=object将允许任意Python对象,因此不支持它。而dtype=list只相当于dtype=objectdocumentation

Built-in Python types

Several python types are equivalent to a corresponding array scalar when used to generate a dtype object:

int           np.int_
bool          np.bool_
float         np.float_
complex       np.cfloat
bytes         np.bytes_
str           np.bytes_ (Python2) or np.unicode_ (Python3)
unicode       np.unicode_
buffer        np.void
(all others)  np.object_

总之,使用object数组会很慢,这适用于NumPy数组和numba函数。无论何时选择使用这样的object数组,您都会隐式地决定不需要高性能。在

所以如果你想要性能并且使用NumPy数组,那么你需要重写它,这样你就不会首先使用对象数组,如果它仍然很慢,那么你可以考虑对非对象数组抛出numba。在

相关问题 更多 >