如何修复pythonsklearn中的SVR图

2024-04-20 07:36:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一个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

然后得到以下输出:

^{pr2}$

然后我试着用这个来展示回归的结果:

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

我试着画出最适合模型的线,而不是从每一点开始画一条线。在


Tags: 数据模型plottransformtrainpltfitcolor
2条回答

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

enter image description here

不要使用plt.plot,因为所有数据都是随机排序的。使用plt.scatter或将数据从最小值排序到最大值

相关问题 更多 >