scipy.optimize.minimum最小值“trustconstr”给出了不同初始值的不同最优值

2024-04-23 10:57:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用scipy库来解决一个优化问题。我的目标函数是SVR回归。不同的初始值给出不同的最优值。为什么会这样?你知道吗


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])


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]

x0 = np.array([26,36,11,7,580,377,84,43,4.3])
res = minimize(objective, x0, method='trust-constr',
               options={'verbose': 1}, bounds=bounds)

optimal_values = res.x

如果我把x0改成不同的值,我的最优值就不同了。为什么会这样??你知道吗

this is the code for svr regression:

X = dataset.iloc[:,:-1 ].values
y = dataset.iloc[:,9:10].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_Y = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

y_train = sc_Y.fit_transform(y_train)
y_test = sc_Y.transform(y_test)


from sklearn.svm import SVR
regressorSVR = SVR(kernel = 'rbf')

regressorSVR.fit(X_train, y_train)

Tags: thefromtestimporttransformtrainscipydataset
1条回答
网友
1楼 · 发布于 2024-04-23 10:57:07

我知道答案了。我的目标函数是非线性的。所以它是一个非凸优化问题。scipy中的所有解算器都提供了局部收敛。如果你的优化问题是非凸的,那么最终可能是局部收敛的。有一个全局解的概念,但不在scipy中,非凸问题的局部收敛与全局收敛简化为P与NP问题。你知道吗

相关问题 更多 >