在看李航老师的《统计学习方法》时,想把例7.1通过代码实现,有了这篇博客。
1、求解思路
使用scipy中的minimize函数求解最优化问题的形式如下:
其中
x
x
x是一个向量,
g
i
(
x
)
g_{i}(x)
gi(x)是非等式约束,
h
j
(
x
)
h_{j}(x)
hj(x)是等式约束。
例7.1中的优化问题如下:
刚好能够带到式子里面,下面是代码:
from scipy.optimize import minimize
import numpy as np
#目标函数
fun=lambda x: 0.5*(x[0]*x[0]+x[1]*x[1])
#约束条件
cons=(
{'type':'ineq','fun':lambda x: 3*x[0]+3*x[1]+x[2]-1},
{'type':'ineq','fun':lambda x: 4*x[0]+3*x[1]+x[2]-1},
{'type':'ineq','fun':lambda x: -x[0]-x[1]-x[2]-1}
)
#w1,w2,b初始值
x0=np.array([0]*3)
#结果
res=minimize(fun,x0,method='SLSQP',constraints=cons)
print(res)
程序运行结果:
fun: 0.25000000000000056
jac: array([0.50000001, 0.50000001, 0. ])
message: 'Optimization terminated successfully.'
nfev: 10
nit: 2
njev: 2
status: 0
success: True
x: array([ 0.5, 0.5, -2. ])
2、minimize函数讲解
scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)
函数官网链接:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize
我这里只列几个常用的参数:
method常用的有BFGS, L-BFGS-B, SLSQP,分别的含义如下:
- SLSQP:使用顺序最小二乘编程(SLSQP)最小化一个或多个变量的标量函数;
- BFGS:使用BFGS算法最小化一个或多个变量的标量函数;
- L-BFGS-B:使用L-BFGS-B算法最小化一个或多个变量的标量函数。
constraints定义了约束条件,每个约束定义为一个词典,键值对如下:
- fun:约束函数;
- type:约束类型,‘eq’是等式约束,‘ineq’是不等式约束;
函数的返回值是一个OptimizeResult对象,各属性如下:
-
x: ndarray
The solution of the optimization. -
success: bool
Whether or not the optimizer exited successfully. -
status: int
Termination status of the optimizer. Its value depends on the underlying solver. Refer to message for details. -
message: str
Description of the cause of the termination. -
fun, jac, hess: ndarray
Values of objective function, its Jacobian and its Hessian (if available). The Hessians may be approximations, see the documentation of the function in question. -
hess_inv: object
Inverse of the objective function’s Hessian; may be an approximation. Not available for all solvers. The type of this attribute may be either np.ndarray or scipy.sparse.linalg.LinearOperator. -
nfev, njev, nhev: int
Number of evaluations of the objective functions and of its Jacobian and Hessian. -
nit: int
Number of iterations performed by the optimizer. -
maxcv: float
The maximum constraint violation.