【发布时间】:2023-04-10 18:33:01
【问题描述】:
我正在尝试使用 scipy.optimize.minimize. 优化目标函数
最初,我一直收到错误
TypeError: numpy boolean subtract, the - operator, is deprecated, use the bitwise_xor, the ^ operator, or the logical_xor function instead.
在寻找解决方案后,我发现我的scipy\optimize\slsqp.py 文件中需要进行代码重构。我把-操作符改成了np.subtract(),如下图,
jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon # This was the error line
jac[i] = np.subtract((func(*((x0+dx,)+args)), f0))/epsilon # which I changed to this
但现在我不断收到错误消息,
ValueError: invalid number of arguments 在同一行
我不明白,因为我已经尝试使用示例参数 np.subtract,它似乎工作正常。
任何帮助将不胜感激。
【问题讨论】:
-
如果您在指定的行中得到
TypeError,这似乎意味着您的目标函数func正在返回一个类型为bool的值。您是否检查过func确实返回浮点值,正如 SciPy 中的最小化器所期望的那样?
标签: python numpy scipy-optimize scipy-optimize-minimize