Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在多元多项式回归中可视化回归线?_Python_Machine Learning_Scikit Learn - Fatal编程技术网

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

Python 如何在多元多项式回归中可视化回归线?,python,machine-learning,scikit-learn,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.preproc

所以我有这个代码,我的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))

类似这样的东西

x = X_test
y = `PolyReg.coef_ * x + PolyReg.intercept_
plt.plot(x, y, '-')
plt.show()
根据希望看到的内容替换
x
y
值。 也可以通过以下方式在同一绘图上添加数据点:

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

非常感谢!但我注意到,当我沿着数据集绘制回归线时,所有的点都在这条线上。我是python的初学者,所以我不知道是什么原因导致了这一点。好吧,我想帮助你如何绘制。但对数据的理解是站在你这边的;-)我的意思是:用您想要绘制的任何内容替换
x
y
,例如,您可能想要绘制列车数据,而不是测试数据。也许学习过程本身存在缺陷;我相信你写的,系数是好的。在任何情况下,现在您已经能够进行绘图,了解发生了什么应该会有所帮助。@JayrhaldPadilla欢迎您这样做。请不要用与初始问题无关的后续问题扩展问题;既然答案解决了你所要求的问题,那就开玩笑地接受它吧——看。如果您有后续问题,请打开新帖子。