python - 如何在 Python sklearn 中修复 SVR 图

标签 python matplotlib machine-learning scikit-learn svm

我正在尝试将 SVR 模型拟合到我的数据集,并使用 Python 中的 Sklearn 查看绘图。

from sklearn.svm import SVR
#Load Data
X_train_Occ = pd.DataFrame(X_train['occupancy'])
Y_train_Occ = Y_train
#Rescale
sc_X = StandardScaler()
sc_Y = StandardScaler()
X_train_Occ_scaled = sc_X.fit_transform(X_train_Occ)
Y_train_Occ_scaled = sc_Y.fit_transform(Y_train_Occ.reshape(-1, 1))

regressor = SVR(kernel ='rbf')
regressor.fit(X_train_Occ_scaled, Y_train_Occ_scaled)

我将数据加载到 X 和 Y 数据帧中并对其进行缩放。 请参见下图:

enter image description here

然后我得到以下输出:

SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto', kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)

然后我尝试用以下方法显示回归结果:

plt.scatter(X_train_Occ_scaled, Y_train_Occ_scaled, color = 'red')
plt.plot(X_train_Occ_scaled, regressor.predict(X_train_Occ_scaled), color = 'blue')
plt.title('Occupancy vs Flow (SVR)')
plt.xlabel('Occupancy')
plt.ylabel('Flow')
plt.show()

给出以下情节:

enter image description here

模型是否过度拟合数据?还是代码有问题?

我正在关注这里的代码: http://scikit-learn.org/stable/auto_examples/svm/plot_svm_regression.html

我试图画出与模型最适合的线,而不是从每个点画出一条线。

最佳答案

如前所述,解决方案是首先按自变量对数据进行排序,然后将数据拟合到模型并预测结果。

enter image description here

关于python - 如何在 Python sklearn 中修复 SVR 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49777898/

相关文章:

python - 如何模拟包模块中定义的函数?

python - 使用宽松字典映射列中的值

Python正则表达式用于解析以给定的BNF格式定义的树结构

python matplotlib 颜色图也被后续绘图继承

Python 等效于 Matlab 文本扫描

python - 将 pandas 表(填充有字符串)保存为 png

python - matplotlib:从数据集中删除数据时散点图不更新

python-3.x - 训练结束后,nltk naivebayes 分类器如何学习更多特征集?

python - 如何获得一个值的 pandas Series 虚拟表示

R mlr 包 - 是否可以保存参数调整中的所有模型?