如何在多元多项式回归上可视化回归线?

时间:2020-09-08 14:40:16

标签: python machine-learning scikit-learn

所以我有了这段代码,我的SGD模型得到了不错的r分数。剩下要做的就是可视化回归线,但我不知道如何做。这是我的代码:

from sklearn.datasets import make_friedman1
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures

X_F1, y_F1 = make_friedman1(n_samples = 100,
                           n_features = 7, random_state=0)
poly = PolynomialFeatures(degree=2)
X_train_scale = poly.fit_transform(X_F1)
poly.fit(X_train_scale,y_F1)
X_train, X_test, y_train, y_test = train_test_split(X_train_scale,y_F1, random_state=0)
PolyReg = SGDRegressor(alpha=0.001,max_iter=100000).fit(X_train,y_train)
print ("Intercept for the Polynomial model ",PolyReg.intercept_)
print ("Coefficents for the Polynomial model ",PolyReg.coef_)
print ("R-squared Score (training) = ",PolyReg.score(X_train,y_train))
print ("R-squared Score (test) = ",PolyReg.score(X_test,y_test))

1 个答案:

答案 0 :(得分:0)

有些事情吗?

x = X_test
y = `PolyReg.coef_ * x + PolyReg.intercept_
plt.plot(x, y, '-')
plt.show()

根据您要查看的内容替换xy的值。 您也可以通过以下方式在同一图上添加数据点:

plt.plot(x, y_test, ".")