在运行 scipy.minimize 函数时,“我遇到了 TypeError: 'numpy.float64' object is not callable” 错误。具体来说,在执行以下代码时出现了错误:
.../scipy/optimize/optimize.py", line 292, in function_wrapper
return function(*(wrapper_args + args))
我已经查看了此前类似的话题,通常由于.minimize的第一个输入参数不是函数而导致此问题发生。然而,我很难理解,因为“a”本身就是一个函数。你认为是什么问题呢?
### "data" is a pandas data frame of float values
### "w" is a numpy float array i.e. [0.11365704 0.00886848 0.65302202 0.05680696 0.1676455 ]
def a(data, w):
### Return a negative float value from position [2] of an numpy array of float values calculated via the "b" function i.e -0.3632965490830499
return -b(data, w)[2]
constraint = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
### i.e ((0, 1), (0, 1), (0, 1), (0, 1), (0, 1))
bound = tuple((0, 1) for x in range (len(symbols)))
opts = scipy.minimize(a(data, w), len(symbols) * [1. / len(symbols),], method = 'SLSQP', bounds = bound, constraints = constraint)
a(data, w)
是一个函数调用,而不是一个函数。只有a
本身才是一个函数,即没有括号(...)
。 - tel