2014-08-31 92 views
5

當訓練SVM迴歸時,通常建議在訓練之前縮放輸入要素。Scikit-learn SVM迴歸分解

但是如何縮放目標?通常這不被認爲是必要的,我不明白爲什麼它是必要的。

然而,在用於從SVM迴歸scikit學習例如: http://scikit-learn.org/stable/auto_examples/svm/plot_svm_regression.html

通過只引入線y = Y/1000訓練前,預測會分解爲恆定值。在訓練之前縮放目標變量可以解決問題,但我不明白爲什麼這是必要的。

是什麼原因導致此問題?

import numpy as np 
from sklearn.svm import SVR 
import matplotlib.pyplot as plt 

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

# Added line: this will make the prediction break down 
y=y/1000 

# Fit regression model 
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) 
svr_lin = SVR(kernel='linear', C=1e3) 
svr_poly = SVR(kernel='poly', C=1e3, degree=2) 
y_rbf = svr_rbf.fit(X, y).predict(X) 
y_lin = svr_lin.fit(X, y).predict(X) 
y_poly = svr_poly.fit(X, y).predict(X) 

# look at the results 
plt.scatter(X, y, c='k', label='data') 
plt.hold('on') 
plt.plot(X, y_rbf, c='g', label='RBF model') 
plt.plot(X, y_lin, c='r', label='Linear model') 
plt.plot(X, y_poly, c='b', label='Polynomial model') 
plt.xlabel('data') 
plt.ylabel('target') 
plt.title('Support Vector Regression') 
plt.legend() 
plt.show() 

回答

7

支持向量迴歸使用的損失函數只有在預測值和目標之間的差異超過某個閾值時纔是肯定的。在閾值以下,預測被認爲「足夠好」,損失爲零。當您縮小目標時,SVM學習者可以退回平坦模型,因爲它不會再造成任何損失。

閾值參數在sklearn.svm.SVR中被稱爲epsilon;將其設置爲較小值的較小目標。這個數學背後的解釋是here

+0

就是這樣,謝謝你的回答。因此,對於通用模型,選項可以擴展目標,或者在網格搜索中包含epsilon。你有建議什麼更有意義嗎? – user1774143 2014-09-01 06:46:10

+1

@ user1774143正確的「epsilon」網格搜索。縮放'y'是等價的,但容易出錯。 – 2014-09-01 08:37:52