Python 用参数最小化函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43017792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:26:12  来源:igfitidea点击:

Minimize function with parameters

pythonnumpyscipymathematical-optimization

提问by Nate Stemen

Currently I have the following code that defines the function f.

目前我有以下定义函数的代码f

a = #something
b = #something
c = #something
def f(x):
    """Evaluates some function that depends on parameters a, b, and c"""
    someNumber = #some calculation
    return someNumber

Ideally I would do def f(x, a, b, c), BUT I am minimizing fwith respect to xand SciPy's optimization toolbox doesn't allow for one to minimize functions with parameters in the arguments. That said I would like to run my minimization code for multiple values of a, band c. Is there a way I can do this?

理想情况下,我会做def f(x, a, b, c),但我尽量减少f对于x和SciPy的的优化工具箱不允许一个与在参数参数最小化的功能。也就是说,我想为a, b和 的多个值运行我的最小化代码c。有没有办法做到这一点?

回答by Liteye

You can specify additional arguments in args

您可以在 args

from scipy.optimize import minimize 
minimize(f, x0, args=(a, b, c))

回答by hpaulj

This is a straightforward question and answer about using minimize. In case other users need something more concrete, here's a simple example.

这是一个关于使用minimize. 如果其他用户需要更具体的东西,这里有一个简单的例子。

A generalized quadratic equation:

一个广义二次方程:

In [282]: def fun(x, a,b,c):
     ...:     return a*x**2 + b*x + c

In [283]: optimize.minimize(fun, 10, args=(1,0,0))
Out[283]: 
      fun: 1.7161984122524196e-15
 hess_inv: array([[ 0.50000001]])
      jac: array([ -6.79528891e-08])
  message: 'Optimization terminated successfully.'
     nfev: 15
      nit: 4
     njev: 5
   status: 0
  success: True
        x: array([ -4.14270251e-08])

In [284]: optimize.minimize(fun, 10, args=(1,1,1))
Out[284]: 
      fun: 0.7500000000000221
 hess_inv: array([[ 0.49999999]])
      jac: array([  3.12924385e-07])
  message: 'Optimization terminated successfully.'
     nfev: 12
      nit: 2
     njev: 4
   status: 0
  success: True
        x: array([-0.49999985])

The function could take arrays as input as well, but still needs to return a single (scalar) value:

该函数也可以将数组作为输入,但仍需要返回单个(标量)值:

In [289]: optimize.minimize(fun, [10,10,10], args=(np.array([1,2,3]), 1, 1))
Out[289]: 
      fun: 2.541666666667115
 hess_inv: array([[ 0.50021475, -0.00126004,  0.00061239],
       [-0.00126004,  0.25822101, -0.00259327],
       [ 0.00061239, -0.00259327,  0.16946887]])
      jac: array([ -8.94069672e-08,   4.47034836e-07,  -2.20537186e-06])
  message: 'Optimization terminated successfully.'
     nfev: 55
      nit: 9
     njev: 11
   status: 0
  success: True
        x: array([-0.50000006, -0.2499999 , -0.16666704])

In [286]: def fun(x, a,b,c):
 ...:     return (a*x**2 + b*x + c).sum()

It's a good idea to make sure the function runs with the proposed x0and args, e.g.

确保函数与建议x0和参数一起运行是一个好主意,例如

In [291]: fun(np.array([10,10,10]), np.array([1,2,3]), 1, 1)
Out[291]: 633

If you can't call the objective function, or are confused as to how its arguments work, minimizeisn't a magic bullet. This minimization is only as good as your understanding of the objective function.

如果您不能调用目标函数,或者对其参数的工作方式感到困惑,minimize这不是灵丹妙药。这种最小化仅与您对目标函数的理解一样好。