使用Numba在Python中求解最小二乘

问题描述

我在使用Numba求解python中的最小二乘时遇到麻烦。 这是我的代码

import numpy as np
from numba import jit

@jit(nopython=True)
def test(A,B):
    alpha = np.linalg.lstsq(A,B,rcond=None)
    return alpha

A = np.random.rand(10,3)
B = np.random.rand(10,1)
alpha = test(A,B)

print(alpha)

没有“ @jit(nopython = True)”这一行,它可以工作。使用“ @jit”时,出现以下错误

Invalid use of Function(<function lstsq at 0x000001767E21E318>) with argument(s) of type(s): (array(float64,2d,C),array(float64,rcond=none)
 * parameterized
In deFinition 0:
    TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of ExternalFunction(numba_ez_gelsd) with argument(s) of type(s): (Literal[int](100),int64,ArrayCTypes(dtype=float64,ndim=2),ArrayCT
ypes(dtype=float64,ndim=1),none,ArrayCTypes(dtype=int32,ndim=1))
KNown signatures:
 * (int8,float64*,float64,int32*) -> int32
In deFinition 0:
    All templates rejected with literals.
In deFinition 1:
    All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: ExternalFunction(numba_ez_gelsd)
[2] During: typing of call at C:\Users\owner\anaconda3\lib\site-packages\numba\targets\linalg.py (1645)

有什么建议吗?

解决方法

删除 rcond = None 关键字,它将正常工作。