Numba 无法识别该字符串。如何更正以下代码?谢谢!

@nb.jit(nb.float64(nb.float64[:], nb.char[:]), nopython=True, cache=True)
def func(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string,
    :return: a np.array of same shape as x
    """
    return result

最佳答案

以下适用于 Numba 0.44:

import numpy as np
import numba as nb

from numba import types

@nb.jit(nb.float64[:](nb.float64[:], types.unicode_type), nopython=True, cache=True)
def func(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string,
    :return: a np.array of same shape as x
    """
    print(y)
    return x

但是,如果您尝试在未指定 func 值的情况下运行 y ,则会出现错误,因为在您的签名中您说需要第二个参数。我试图弄清楚如何处理可选参数(查看 types.Omitted ),但无法弄清楚。我可能会考虑不指定签名并让 numba 进行正确的类型推断:
@nb.jit(nopython=True, cache=True)
def func2(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string,
    :return: a np.array of same shape as x
    """
    print(y)
    return x

关于python - 使用Numba时如何指定 'string'数据类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56463147/

10-10 17:31