对象在 least_squares 的函数中不可调用,如何使其可调用?
object is not callable in function of least_squares, how to make it callable?
尝试运行一个least_squares
函数,我经常遇到同样的错误。
无论 return 的类型如何,从 tulpe
到 ndarray
,该对象都不可调用
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)
变量y
、X
、W
、q
和m
是输入变量。 PSI
是 msecost
.
中实际使用的另一个函数的输出变量
Whosebug 给出了这个:
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
作为可调用函数并通过使用 [=22] 最小化 returns f
和 J
=] 作为变量。我绝对没有其他限制。我做错了吗?
需要传递函数的引用,所以
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 结果设置为 函数 ——这就是异常抱怨 ndarray 对象不是 "callable" 的原因
尝试运行一个least_squares
函数,我经常遇到同样的错误。
无论 return 的类型如何,从 tulpe
到 ndarray
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)
变量y
、X
、W
、q
和m
是输入变量。 PSI
是 msecost
.
Whosebug 给出了这个:
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
作为可调用函数并通过使用 [=22] 最小化 returns f
和 J
=] 作为变量。我绝对没有其他限制。我做错了吗?
需要传递函数的引用,所以
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 结果设置为 函数 ——这就是异常抱怨 ndarray 对象不是 "callable" 的原因