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

标签 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

如果您有任何解决此问题的建议,请告诉我。

谢谢

最佳答案

原因是带有内核 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)

关于python - 建模支持向量回归 (SVR) 与线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34859831/

相关文章:

python - 如何在 Django 模板之外获取格式化的 DateTimeField 值?

python - 通过 Spark 1.6 Dataframe 上的其他字段计算每个组的不同元素

python - 使用 Python 将数据框中的选择性行合并为 1

python - 为什么我的 SGD 与我的线性回归模型相去甚远?

scikit-learn - `sample_weight` 对 `DecisionTreeClassifier` 在 sklearn 中的工作方式有何影响?

r - 摘要 quantreg backsolve 中的错误

python - 通过将现有列表乘以 1 来创建新列表对象是否等同于进行深度复制?

python - sklearn 中的 CountVectorizer 仅包含出现次数高于某个最小次数的单词

python - Python生成统计表并导出到Excel

tensorflow - 为什么深度神经网络不能逼近简单的 ln(x) 函数?