scipy.minimize - "TypeError: numpy.float64' object is not callable running" scipy.minimize-“TypeError:numpy.float64'对象不可调用运行”

7
在运行 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
2个回答

5

简短回答

它应该变成:

opts = scipy.minimize(a, len(symbols) * [1. / len(symbols),], args=(w,), method='SLSQP', bounds=bound, constraints=constraint)

详细信息

a(data, w)不是一个函数,而是一个函数调用。换句话说,a(data, w)的值和类型有效地是函数a的返回值。minimize需要实际的函数而不是调用(即在括号(...)和括号中间的所有内容之外),作为它的第一个参数。

来自scipy.optimize.minimize文档

scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)

...

fun:callable

要最小化的目标函数。必须采用形式f(x, *args)。优化参数x是一维数组,args是元组,包含任何其他固定参数,以完全指定函数。

...

args:tuple,可选

传递给目标函数的额外参数...

因此,假设w是固定的(至少相对于您想要的最小化来说),则可以通过args参数将其传递给minimize,如上所示。


第99行代码中,opts = opt.minimize(a, args=(data, w), len(symbols) * [1. / len(symbols),], method='SLSQP', bounds=bound, constraints=constraint)出现了语法错误:位置参数在关键字参数之后。 - Nipper
@Nipper 看起来你正在使用 Raubtaube 的答案,它有无效的语法(这就是为什么你会收到错误消息)。尝试一下我发布的那个。 - tel
抱歉,我没有仔细阅读。按照您的建议进行了操作,结果是line 99 opts = opt.minimize(a, len(symbols) * [1. / len(symbols),], args = (weights,), method = 'SLSQP', bounds = bound, constraints = constraint) ^ SyntaxError: invalid syntax。我需要交换args中w的位置,因为b函数的第一个输入是数据框(b函数无法对numpy.array执行.shift()等操作)。 - Nipper
opts = opt.minimize(a, len(symbols) * [1. / len(symbols),], args = (w, ), method = 'SLSQP', bounds = bound, constraints = constraint) ERROR line 16, in b r1d = np.log(data / data.shift(1)) AttributeError: 'numpy.ndarray' object has no attribute 'shift' - Nipper
让我们在聊天中继续这个讨论 - Nipper
显示剩余2条评论

1
你没有传递函数,而是传递了被计算出的结果给 minimize 函数。
opts = scipy.minimize(a,  len(symbols) * [1. / len(symbols),], method = 'SLSQP', bounds = bound, constraints = constraint, args = (data,w))

应该可以工作。

编辑:修复了愚蠢的语法错误。


第99行 opts = opt.minimize(a, args=(data, w), method='SLSQP', bounds=bound, constraints=constraint, options={'maxiter': 1000}, tol=1e-6) ^ SyntaxError: 关键字参数后面跟着位置参数。 这是输出。 - Nipper
语法不正确。您需要交换 args = (data,w)len(symbols) * [1. / len(symbols),]。此外,应该是 args=(w,)minimize 需要能够自由地操作 a 的第一个参数。 - tel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接