我正在使用numba的jit代码加速,代码如下:
@jit(nopython=True)
def testrun2():
for i in range(5):
nx = np.empty(shape=(0,1))
for j in range(5):
nx=np.append(nx,[eta2[j]*(hour_avgV2[j]/hour_avgV1[i])])
print(nx.mean())
if __name__ == '__main__':
t1 = time.time()
testrun2()
t2 = time.time()
print(t2-t1)
报错如下:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot unify array(float64, 2d, C) and array(float64, 1d, C) for 'nx.2', defined at /tmp/ipykernel_8059/3388145730.py (5)
File "../../../../tmp/ipykernel_8059/3388145730.py", line 5:
<source missing, REPL/exec in use?>
During: typing of assignment at /tmp/ipykernel_8059/3388145730.py (5)
File "../../../../tmp/ipykernel_8059/3388145730.py", line 5:
<source missing, REPL/exec in use?>
如果我把@jit(nopython=True)注销掉,这代码可以正常运行。或者如果我把nx = np.empty(shape=(0,1))这一行放入第二层for循环,代码也可以正常运行,但是这不符合我的运算逻辑。
请问有什么方法能够解决这个问题吗?