Scipy优化 - 不正确的参数
简单的问题/问题
def my_func(x,args):
a = x[0]
b = x[1]
return a*args[0] + a*args[1] + b*args[2]
import scipy.optimize as optimize
params = [1,2,3]
guess = [3,4]
bounds = [(1,5),(2,7)]
result = optimize.minimize(my_func, x0=guess, args=(params), method='SLSQP', bounds = bounds)
print(result)
print('done')
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
print(result)
第一个优化运行良好。但是,第二个优化似乎与输入参数的数量有问题吗?不确定为什么:
fun: 9.0
jac: array([3., 3.])
message: 'Optimization terminated successfully'
nfev: 6
nit: 2
njev: 2
status: 0
success: True
x: array([1., 2.])
done
Traceback (most recent call last):
File "c:\Users\bob\Desktop\optimiser.py", line 15, in <module>
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3328, in brute
Jout = np.array(list(mapper(wrapped_func, grid)))
File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3400, in __call__
return self.f(np.asarray(x).flatten(), *self.args)
TypeError: my_func() takes 2 positional arguments but 4 were given
我不明白为什么它认为4个论点是对my_func的。有什么想法吗?
干杯
simple question/issue
def my_func(x,args):
a = x[0]
b = x[1]
return a*args[0] + a*args[1] + b*args[2]
import scipy.optimize as optimize
params = [1,2,3]
guess = [3,4]
bounds = [(1,5),(2,7)]
result = optimize.minimize(my_func, x0=guess, args=(params), method='SLSQP', bounds = bounds)
print(result)
print('done')
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
print(result)
The first optimisation runs fine. However, the second optimisation seems to have an issue with the number of input arguments? Not sure why:
fun: 9.0
jac: array([3., 3.])
message: 'Optimization terminated successfully'
nfev: 6
nit: 2
njev: 2
status: 0
success: True
x: array([1., 2.])
done
Traceback (most recent call last):
File "c:\Users\bob\Desktop\optimiser.py", line 15, in <module>
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3328, in brute
Jout = np.array(list(mapper(wrapped_func, grid)))
File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3400, in __call__
return self.f(np.asarray(x).flatten(), *self.args)
TypeError: my_func() takes 2 positional arguments but 4 were given
I don't understand why it thinks 4 arguments are being given to my_func. Any ideas?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
emotizize.brute
将其传递给可呼叫函数时扩展args
参数。Optimize.minimize
执行同样的事情,但是该功能首先将args
变成元组,如果还没有:请参阅在这里以获取详细信息。 a>不执行此操作。因此,您得到的是列表的元组
([1,2,3],)
nathemotimize.minimize.minimize
和[1,2,2, 3]
呼叫Optimize.Brute
时。前者扩展到一个论点,后者为三个论点。您可以通过播放来解决这个问题。将您的函数定义为采用可变数量的参数:
并将其他参数指定为元组:
Because
optimize.brute
expands theargs
argument when it passes these to the callable function.optimize.minimize
does the same thing, but that function first turnsargs
into a tuple if it is not already:See here for details.
optimize.brute
does not do this. Thus, what you get is a tuple of a list([1, 2, 3],)
when callingoptimize.minimize
, and[1, 2, 3]
when callingoptimize.brute
. The former expands to a single argument, the latter to three arguments.You can fix this by playing nice. Define your function as taking a variadic number of arguments:
And specify the additional arguments as a tuple: