Python 岭回归与多项式核次=1的SVM回归(SVR)的差异

Python 岭回归与多项式核次=1的SVM回归(SVR)的差异,python,machine-learning,scikit-learn,regression,svm,Python,Machine Learning,Scikit Learn,Regression,Svm,我正在尝试为一个应用程序构建一个模型, 我使用了sklearn的岭回归和SVR,虽然我试图保持参数不变,但它们看起来有所不同 我在两个模型中都使用了正则化参数为=1。(它们都具有L2正则化) 有一个额外的参数为多边形内核,我设置为零 数据是标准化的 from sklearn.linear_model import Ridge linear_ridge = Ridge(alpha=1.0) # L2 regularization linear_ridge.fit(np.array(X_train

我正在尝试为一个应用程序构建一个模型, 我使用了sklearn的岭回归和SVR,虽然我试图保持参数不变,但它们看起来有所不同

我在两个模型中都使用了正则化参数为=1。(它们都具有L2正则化) 有一个额外的参数为多边形内核,我设置为零

数据是标准化的

from sklearn.linear_model import Ridge

linear_ridge = Ridge(alpha=1.0) # L2 regularization
linear_ridge.fit(np.array(X_train) , np.array(y_train))

from sklearn import svm

model_SVR_poly = svm.SVR(kernel = 'poly' , coef0=0.0 , degree = 1, C = 1.0 , epsilon = 0.1 ) #L2 regularization
model_SVR_poly.fit(np.array(X_train) , np.array(y_train))


Linear_ridge_pred = linear_ridge.predict(test_data[start_data:]) *Y_std[0] + Y_mean[0]
svr_poly_pred =  model_SVR_poly.predict(test_data[start_data:]) *Y_std[0] + Y_mean[0]
如果epsilon的值减小到0.0,则下冲量将大于脊线,如果增大,则过冲量将更大

在测试阶段,脊线似乎下冲,而SVR似乎过冲

在我的案例中,这两种实现的区别是什么


对于我来说,正如您所指出的,
Ridge()
SVR()
的实现可能存在一些差异

一方面,你可能会看到损失函数(epsilon不敏感损失和平方epsilon不敏感损失)与(Ridge损失)之间存在差异。sklearn文档中也强调了这一点,但它将内核岭回归和SVR与非线性内核进行了比较

除此之外,将SVR与次数为1的多项式核一起使用这一事实增加了另一个区别:正如您所看到的(SVR构建在LibSVM库之上)还有一个需要考虑的参数(
gamma
)(为了方便起见,您可以将它设为1,它等于
'scale'

这里是我可以通过调整(使用未调整的参数)得到的拟合差异。我还试图考虑<代码>线性VSR()>代码>,WRT <代码> Svr()>代码>之间有一些进一步的不同,如您所见,例如,或。

print(__doc__)

import numpy as np
from sklearn.linear_model import Ridge
from sklearn.svm import LinearSVR, SVR
import matplotlib.pyplot as plt
np.random.seed(42)

# #############################################################################
# Generate sample data
X = np.sort(5 * np.random.rand(40, 1), axis=0)
y = np.sin(X).ravel()

# #############################################################################
# Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(8))

# #############################################################################
# Fit regression model
svr_lin = SVR(kernel='linear', C=1, tol=1e-5)
svr_lins = LinearSVR(loss='squared_epsilon_insensitive', C=1, tol=1e-5, random_state=42)
svr_poly = SVR(kernel='poly', C=1, degree=1, gamma=1, tol=1e-5, coef0=0.0)
ridge = Ridge(alpha=1, random_state=42)
y_lin = svr_lin.fit(X, y).predict(X)
y_lins = svr_lins.fit(X, y).predict(X)
y_poly = svr_poly.fit(X, y).predict(X)
y_ridge = ridge.fit(X, y).predict(X)

coef_y_lin, intercept_y_lin = svr_lin.coef_, svr_lin.intercept_
coef_y_lins, intercept_y_lins = svr_lins.coef_, svr_lins.intercept_
coef_y_ridge, intercept_y_ridge = ridge.coef_, ridge.intercept_

# #############################################################################
# Look at the results
lw = 2
plt.figure(figsize=(10,5))
plt.scatter(X, y, color='darkorange', label='data')
plt.plot(X, y_lins, color='navy', lw=lw, label='Linear model (LinearSVR) %s, %s' % 
(coef_y_lins, intercept_y_lins))
plt.plot(X, y_lin, color='red', lw=lw, label='Linear model (SVR) %s, %s' % (coef_y_lin, intercept_y_lin))
plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial model of degree 1 (SVR)')
plt.plot(X, y_ridge, color='g', lw=lw, label='Ridge %s, %s' % (coef_y_ridge, intercept_y_ridge))
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.axis([0, 5, -1, 1.5])