如何在Numba中用多维数组中的数字替换nans



在普通python中,用numpy数组中的数字替换nans是微不足道的。然而,在Numba 中执行相同操作时,以下操作失败

@jit(nopython=True)
def dostuff():
x = np.array([[1,np.nan,3]]);
np.nan_to_num(x,copy=False);
dostuff()

如何在Numba可编译函数中的numpy数组中用零替换nan?对于一维,可以进行x[np.isnan(x)]=0,但对于更高的维度,这也会失败。

对于一维,可以做x[np.isnan(x(]=0,但对于更高的尺寸,这也失败了。

有线索:(

import numpy as np
from numba import jit
@jit(nopython=True)
def dostuff(x):
shape = x.shape
x = x.ravel()
x[np.isnan(x)] = 0
x = x.reshape(shape)
return x
a = np.array([[1,np.nan,3], [np.nan, 2, 6]])
dostuff(a)
print(a)

输出:

[[1. 0. 3.]
[0. 2. 6.]]

最新更新