英文:
Numba: how to get indexes for all rows that contain at least one nan values?
问题
以下是翻译好的部分:
"Suppose I have a numpy 2d array (m by n), I want to get indexes of all its rows contain at least one nan values.
It is relatively straightforward to do it in pure numpy as follows:
import numpy as np
X = np.array([[1, 2], [3, np.nan], [6, 9]])
has_nan_idx = np.isnan(X).any(axis=1)
has_nan_idx
>>> array([False, True, False])
How can I achieve the same using numba njit? For me, I got an error since numba does not support any with arguments.
Thanks."
英文:
Suppose I have a numpy 2d array (m by n), I want to get indexes of all its rows contain at least one nan values.
It is relatively straightforward to do it in pure numpy as follows:
import numpy as np
X = np.array([[1, 2], [3, np.nan], [6, 9]])
has_nan_idx = np.isnan(X).any(axis=1)
has_nan_idx
>>> array([False, True, False])
How can I achieve the same using numba njit? For me, I got an error since numba does not support any with arguments.
Thanks.
答案1
得分: 1
如果您使用 guvectorize
,您将自动获得ufunc的好处,例如具有axis
关键字的功能。
例如:
from numba import guvectorize
@guvectorize(["void(float64[:], boolean[:])"], "(n)->()")
def isnan_with_axis(x, out):
n = x.size
out[0] = False
for i in range(n):
if np.isnan(x[i]):
out[0] = True
break
isnan_with_axis(X, axis=1)
# array([False, True, False])
英文:
If you use guvectorize
you'll automatically get the ufunc benefits of having things like the axis keyword.
For example:
from numba import guvectorize
@guvectorize(["void(float64[:], boolean[:])"], "(n)->()")
def isnan_with_axis(x, out):
n = x.size
out[0] = False
for i in range(n):
if np.isnan(x[i]):
out[0] = True
break
isnan_with_axis(X, axis=1)
# array([False, True, False])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论