[英]How to specify the parameter an objective function is minimized with respect to, using scipy.optimize.minimize()?
Suppose I have an objective function f(a,b,c) .假设我有一个目标 function f(a,b,c) 。 I want to find the value of b that minimizes it, holding a and c constant, and to experiment with different combinations of a and c , I prefer not to write f(a,b,c) as g(b) .
我想找到最小化它的b值,保持a和c不变,并尝试a和c的不同组合,我不喜欢将f(a,b,c)写为g(b) 。
from scipy.optimize import minimize
def f(a,b,c):
return((a+1)**2 + b + c/2)
res = minimize(f, x0=1, args=(a,c,),)
print(res.x)
Then how do I specify that b is the parameter that f(a,b,c) should be minimized with respect to?那么我如何指定b是f(a,b,c)应该最小化的参数? Does that parameter have to be expressed as x ?
该参数是否必须表示为x ? Or should I make b the first argument of f ?
或者我应该将b作为f的第一个参数?
As the documentation states, the signature of the function should be fun(x, *args)
where x
is the parameter that is minimized for.正如文档所述,function 的签名应该是
fun(x, *args)
其中x
是最小化的参数。 So you can just use a small wrapper around your original function:因此,您可以在原始 function 周围使用一个小包装器:
res = minimize(lambda b, a, c: f(a, b, c), x0=1, args=(a, c))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.