Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么Numba无法编译此函数?_Python_Python 3.x_Numpy_Numba - Fatal编程技术网

Python 为什么Numba无法编译此函数?

Python 为什么Numba无法编译此函数?,python,python-3.x,numpy,numba,Python,Python 3.x,Numpy,Numba,我正在尝试使用Numba编译以下函数: @njit(fastmath=True, nogil=True) def generate_items(array, start): array_positions = np.empty(SIZE, dtype=np.int64) count = 0 while count < SIZE - start: new_array = mutate(np.empty(0, dtype=np.uint8))

我正在尝试使用Numba编译以下函数:

@njit(fastmath=True, nogil=True)
def generate_items(array, start):
    array_positions = np.empty(SIZE, dtype=np.int64)
    count = 0
    while count < SIZE - start:
        new_array = mutate(np.empty(0, dtype=np.uint8))

        if len(new_array) > 0:
            array_positions[count] = len(array)  # <<=== FAILS HERE
            array = np.append(array, np.append(new_array, 255))
            count += 1

    return array, array_positions
这似乎没有意义,因为我只是将一个
int
(len上的结果)赋值给一个
dtype
np.int64
的数组

请注意,
array
的类型为
np.uint8
——但我没有分配数组本身,因此此消息对我来说毫无意义

我试图重构为:

tmp = len(array)  # <<=== FAILS HERE
array_positions[count] = tmp  

tmp=len(array)#第一个问题是您使用了不受支持的函数
mutate
()。 接下来,正如错误所说,您尝试在中添加具有不同数据类型的数组

np.append(array, np.append(new_array, 255))
其中
数组
的类型为
int64
,而
new_数组
保存的值类型为
uint8
。如果您使用了
@jit
,它会向您显示一个下降到
对象
模式的警告,但是由于您使用
@njit
装饰器强制执行了
非Python
模式,因此它会抛出一个错误。
干杯。

第一个问题是您使用了不受支持的函数
mutate
()。 接下来,正如错误所说,您尝试在中添加具有不同数据类型的数组

np.append(array, np.append(new_array, 255))
其中
数组
的类型为
int64
,而
new_数组
保存的值类型为
uint8
。如果您使用了
@jit
,它会向您显示一个下降到
对象
模式的警告,但是由于您使用
@njit
装饰器强制执行了
非Python
模式,因此它会抛出一个错误。
干杯。

这是否回答了您的问题?不,那是另一个问题。。在这个例子中我没有使用concatenate。
np.append
使用
np.concatenate
。我不确定是否使用
numba
,但在循环中使用
np.append
(或任何串联/堆栈系列)是一个坏主意-速度慢,而且通常很难正确。我们建议将数组收集到一个列表中,并在最后进行一次连接。花几分钟时间查看
np.append
的python代码。您将看到,即使在
numpy
中,它也不是一个很好的函数。另一件事,在
numba
中使用
concatenate
时,给它一个元组参数,而不是列表。这是否回答了您的问题?不,那是另一个问题。。在这个例子中我没有使用concatenate。
np.append
使用
np.concatenate
。我不确定是否使用
numba
,但在循环中使用
np.append
(或任何串联/堆栈系列)是一个坏主意-速度慢,而且通常很难正确。我们建议将数组收集到一个列表中,并在最后进行一次连接。花几分钟时间查看
np.append
的python代码。您将看到,即使在
numpy
中,它也不是一个很好的函数。另外,在
numba
中使用
concatenate
时,给它一个元组参数,而不是列表。