我正在使用 scipy.optimize.minimize 为我的目标函数找到最佳参数。
我的代码:
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import Bounds
bounds = Bounds([26,26,8,6,400,100,0,25,2],[36,38,28,28,1800,800,100,50,7])
energy_history = []
x_values = []
def objective(x):
x_trail = x.reshape(1,-1)
x_trail = sc_X.transform(x_trail)
y_trail = regressorSVR.predict(x_trail)
y_trail = y_trail.reshape(1,-1)
y_trail = sc_Y.inverse_transform(y_trail)
return y_trail[0]
def callback(x,y):
fobj = objective(x)
energy_history.append(fobj)
x_values.append(x)
x0 = np.array([26,28,15,7,400,377,40,43,4.3])
res = minimize(objective, x0, method='trust-constr',
options={'verbose': 1}, bounds=bounds,callback=callback)
optimal_values = res.x
energy = res.fun
给出初始值后,我得到的最小值(res.fun)是-7.1。我正在创建一个列表(energy_history)来查看它是如何达到这个值的。我在该列表中看到一些小于 -7.1 的值,但为什么 -7.1 会作为最小值返回。
目标函数多次达到 -21 的值,但为什么仍然返回 -7 作为最小值?