如何用 Numba 中的多维数组中的数字替换 nans?
how to replace nans by numbers in a multidimensional array in Numba?
在普通的 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 数组中的 nans?对于一维,可以做到 x[np.isnan(x)]=0
但对于更高的维度,这也失败了。
For one-dimensional one can do x[np.isnan(x)]=0 but for higher
dimensions this fails as well.
有线索:)
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.]]
在普通的 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 数组中的 nans?对于一维,可以做到 x[np.isnan(x)]=0
但对于更高的维度,这也失败了。
For one-dimensional one can do x[np.isnan(x)]=0 but for higher dimensions this fails as well.
有线索:)
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.]]