索引空数组,Numba vs.Numpy

2024-06-26 14:51:09 发布

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

我在试验NumbaNumpy进行数组索引的行为,遇到了一些我不太了解的问题;因此我希望有人能为我指出正确的方向,这可能是一个非常简单的问题。下面是两个函数,它们都使用np.arange公司命令。然后,我使用索引0 example[0] = 1将(NumbaNumpy的执行/中断方法进行各种实验)添加到数组中。你知道吗

带有jitNumba函数运行时没有错误,但Numpy示例给出了错误:

IndexError: index 0 is out of bounds for axis 0 with size 0

Numpy错误是有道理的,但是我不确定为什么启用了jitNumba允许操作没有错误。你知道吗

import numba as nb
import numpy as np

@nb.jit()
def funcnumba():
    '''
    Add item to position 0 using Numba
    '''
    example = np.arange(0)
    example[0] = 1
    return example

def funcnumpy():
    '''
    Add item to position 0 using Numpy. This produces an error which makes sense
    '''
    example = np.arange(0)
    example[0] = 1
    return example

print(funcnumba())

print(funcnumpy())

Tags: 函数importnumpyaddexampledefas错误
1条回答
网友
1楼 · 发布于 2024-06-26 14:51:09

参见Numba documentation on arrays

Currently there are no bounds checking for array indexing and slicing (...)

这意味着您将在本例中写入超出数组边界的内容。因为它只是一个元素,所以您可能会幸运地侥幸逃脱,但是您也可能会使程序崩溃,或者更糟糕的是,悄悄地覆盖其他一些值。有关它的讨论,请参见issue #730。你知道吗

相关问题 更多 >