gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:54:39 25 4
gpt4 key购买 nike

我对建模技术有点陌生,我正在尝试比较 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com