对象在Minimum_squares函数中不可调用,如何使其可调用?

时间:2019-08-14 23:59:05

标签: python python-3.x optimization least-squares

尝试运行least_squares函数时,我经常遇到相同的错误。

tulpendarray的返回类型无论如何都无法调用该对象


res_PSI = least_squares(fun=msecost(PSI, y, X, W, q, m), x0=PSI, jac='2-point', bounds=(-np.inf, np.inf), method='lm', ftol=1e-10,
                        xtol=1e-10, gtol=1e-10, max_nfev=4000)

变量yXWqm是输入变量。 PSImsecost中实际使用的另一个函数的输出变量。

stackoverflow正在提供此信息:

  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2060, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2054, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1405, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1412, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Azerty/PycharmProjects/OptionsTest/venv37/HARST/HAR.py", line 27, in <module>
    x = HARST.mrstar(y=dependent_variable,X=regressors,W=dummies,q=transition,T=T,nX=nX,nW=nW,M=M,rob=rob,flag=flag,sig=sig)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\HARST\HARST.py", line 216, in mrstar
    alpha, llambda, beta, gamma, c, fX, yhat, ehat, G = parestlm(y, X, W, q, T, nX, nW, m, gamma_0, c_0)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\HARST\HARST.py", line 335, in parestlm
    xtol=1e-10, gtol=1e-10, max_nfev=4000)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\lib\site-packages\scipy\optimize\_lsq\least_squares.py", line 807, in least_squares
    f0 = fun_wrapped(x0)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\lib\site-packages\scipy\optimize\_lsq\least_squares.py", line 802, in fun_wrapped
    return np.atleast_1d(fun(x, *args, **kwargs))
TypeError: 'numpy.ndarray' object is not callable

实际的msecost函数是这样的:

def msecost(PSI, y, X, W, q, m):
    T, nX = X.shape
    nW = W.shape[1]

    gamma= getpar(PSI)[0][0]
    c = getpar(PSI)[0][1]

    Z = np.concatenate([X, W],axis=1)
    fX = np.zeros((T, m))
    dfX = np.zeros((T, m))
    for i in range(m):
        fX[:, i] = siglog(gamma * (q - c))
        dfX[:, i] = dsiglog(fX[:, i])
        Z = np.concatenate([Z, np.tile(fX[:, i], (nX, 1)).transpose() * X],axis=1)  # repmat
    theta = np.linalg.pinv(Z.transpose() @ Z) @ Z.transpose() @ y
    alpha = theta[0:nX]
    if not (W.all):
        beta = []
    else:
        beta = theta[nX:nX + nW]
    llambda = theta[nX + nW:].reshape(nX, m, order='F').copy()  # reshape
    if not (W.all):
        yhat = X @ alpha + np.sum(fX @ llambda.transpose() * X, axis=1)
    else:
        yhat = X @ alpha + W @ beta + np.sum(fX @ llambda.transpose() * X, axis=1)
    ehat = y - yhat
    f = np.sum(ehat ** 2) / T

    ggamma, gc = gradG(PSI, X, q, llambda, dfX, m)

    J = np.sum(-2 * np.tile(ehat, (2 * m,1 )).transpose() * np.concatenate([ggamma, gc],axis=1) / T)

    return np.array([f, J])

我从least_squares开始,期望将msecost作为可调用函数,并使用f作为变量来最小化返回JPSI。我绝对没有其他限制。我做错了吗?

1 个答案:

答案 0 :(得分:0)

您需要传递函数的引用,所以

res_PSI = least_squares(fun=msecost, x0=PSI, ...)

也就是说,适合的人希望使用各种参数值来调用msecost函数。使用时

res_PSI = least_squares(fun=msecost(PSI, y, X, W, q, m), x0=PSI, ...) # Nope!

将调用msecost()的ndarray结果设置为 function ,这就是为什么异常会抱怨ndarray对象不“可调用”的原因