Python 代码重构期间scipy SLSQP的参数数无效
我正在尝试使用Python 代码重构期间scipy SLSQP的参数数无效,python,numpy,scipy-optimize,scipy-optimize-minimize,Python,Numpy,Scipy Optimize,Scipy Optimize Minimize,我正在尝试使用scipy.optimize.minimize来优化目标函数。 起初,我不断地得到错误 TypeError:numpy boolean subtract,即-operator,已被弃用,请改用按位异或、^运算符或逻辑异或函数。 在寻找解决方案之后,我发现在我的scipy\optimize\slsqp.py文件中需要进行代码重构。我将-运算符更改为np.subtract(),如下所示 jac[i]=(func(*(x0+dx,)+args))-f0)/epsilon#这是错误行 ja
scipy.optimize.minimize来优化目标函数。
起初,我不断地得到错误
TypeError:numpy boolean subtract,即-operator,已被弃用,请改用按位异或、^运算符或逻辑异或函数。
在寻找解决方案之后,我发现在我的scipy\optimize\slsqp.py
文件中需要进行代码重构。我将-
运算符更改为np.subtract(),如下所示
jac[i]=(func(*(x0+dx,)+args))-f0)/epsilon#这是错误行
jac[i]=np.subtract((func(*(x0+dx,)+args)),f0))/epsilon,我把它改成了这个
但现在我不断地犯错误,
ValueError:同一行上的参数数无效
我不明白,因为我尝试了np.subtract和示例参数,它似乎工作得很好
任何帮助都将不胜感激。使用布尔数据类型数组:
In [131]: x = np.ones(4, bool)
您的第一个错误:
In [132]: x-x
Traceback (most recent call last):
File "<ipython-input-132-966d70d4047a>", line 1, in <module>
x-x
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
使用正确的参数(2),仍然存在布尔错误:
In [134]: np.subtract(x,x)
Traceback (most recent call last):
File "<ipython-input-134-76169c24bdd5>", line 1, in <module>
np.subtract(x,x)
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
[134]中的:np.减法(x,x)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
np.减法(x,x)
TypeError:不支持numpy boolean subtract,即“-`运算符,请改用按位异或、`^`运算符或逻辑异或函数。
如果您在所指示的行中得到TypeError
,这似乎意味着您的目标函数func
正在返回类型为bool
的值。您是否检查过func
是否实际返回浮点值,正如SciPy中的极小值所期望的那样?
In [134]: np.subtract(x,x)
Traceback (most recent call last):
File "<ipython-input-134-76169c24bdd5>", line 1, in <module>
np.subtract(x,x)
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.