建模 - 支持向量回归(SVR)与线性回归

时间:2016-01-18 16:43:47

标签: python scikit-learn regression svm linear-regression

我对建模技术有点新意,我试图比较SVR和线性回归。我使用f(x)= 5x + 10线性函数来生成训练和测试数据集。到目前为止,我已经编写了以下代码片段:

import csv 
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.linear_model import LinearRegression

with open('test.csv', 'r') as f1:
  train_dataframe = pd.read_csv(f1)

X_train = train_dataframe.iloc[:30,(0)]
y_train = train_dataframe.iloc[:30,(1)]

with open('test.csv','r') as f2:
     test_dataframe = pd.read_csv(f2)

X_test = test_dataframe.iloc[30:,(0)]
y_test = test_dataframe.iloc[30:,(1)]

svr = svm.SVR(kernel="rbf", gamma=0.1)
log = LinearRegression()
svr.fit(X_train.reshape(-1,1),y_train)
log.fit(X_train.reshape(-1,1), y_train)

predSVR = svr.predict(X_test.reshape(-1,1))
predLog = log.predict(X_test.reshape(-1,1))

plt.plot(X_test, y_test, label='true data')
plt.plot(X_test, predSVR, 'co', label='SVR')
plt.plot(X_test, predLog, 'mo', label='LogReg')
plt.legend()
plt.show()

正如您在图片中看到的,线性回归工作正常,但SVM预测精度较差。

enter image description here

如果您有任何建议可以解决此问题,请与我们联系。

由于

3 个答案:

答案 0 :(得分:6)

原因是内核rbf的SVR不应用特征缩放。在将数据拟合到模型之前,需要应用特征缩放。

功能缩放的示例代码

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X = sc_X.fit_transform(X)
sc_y = StandardScaler()
y = sc_y.fit_transform(y)

答案 1 :(得分:4)

请参阅以下代码:

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.cross_validation import train_test_split

X = np.linspace(0,100,101)
y = np.array([(100*np.random.rand(1)+num) for num in (5*x+10)])

X_train, X_test, y_train, y_test = train_test_split(X, y)

svr = SVR(kernel='linear')
lm = LinearRegression()
svr.fit(X_train.reshape(-1,1),y_train.flatten())
lm.fit(X_train.reshape(-1,1), y_train.flatten())

pred_SVR = svr.predict(X_test.reshape(-1,1))
pred_lm = lm.predict(X_test.reshape(-1,1))

plt.plot(X,y, label='True data')
plt.plot(X_test[::2], pred_SVR[::2], 'co', label='SVR')
plt.plot(X_test[1::2], pred_lm[1::2], 'mo', label='Linear Reg')
plt.legend(loc='upper left');

enter image description here

你无处可去的原因是rbf内核

答案 2 :(得分:0)

如果我们用这样的约束来调整SVR rbf模型:

svr_rbf = SVR(内核='rbf',C = 1e3,gamma = 0.1)

我们将看到不同的结果,如下图所示。绿星是新的SVR_rbf模型预测。希望对您有所帮助。

enter image description here