Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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_Matplotlib_Scikit Learn_Regression - Fatal编程技术网

如何在python中绘制多项式回归图?

如何在python中绘制多项式回归图?,python,matplotlib,scikit-learn,regression,Python,Matplotlib,Scikit Learn,Regression,当我使用下面的代码时,一切都很好,我得到了一个漂亮的图形 # Model f = np.polyfit(df[feature], y, order) p = np.poly1d(f) print(p) # Visualisation def PlotPolly(model, independent_variable, dependent_variable, Name): x_new = np.linspace(df[feature].min(), df[feature].max(),

当我使用下面的代码时,一切都很好,我得到了一个漂亮的图形

# Model
f = np.polyfit(df[feature], y, order)
p = np.poly1d(f)
print(p) 

# Visualisation
def PlotPolly(model, independent_variable, dependent_variable, Name):
    x_new = np.linspace(df[feature].min(), df[feature].max(), 100)
    y_new = model(x_new)

    plt.plot(independent_variable, dependent_variable, '.', x_new, y_new, '-')
    plt.title('Polynomial Fit')
    ax = plt.gca()
    ax.set_facecolor((0.898, 0.898, 0.898))
    fig = plt.gcf()
    plt.xlabel(Name)
    plt.ylabel(target)

   plt.show()
   plt.close()

PlotPolly(p, X, y, feature) 
但是,当我使用sklearn定义模型时,我似乎无法绘制图形

# Model
from sklearn.preprocessing import PolynomialFeatures
f = PolynomialFeatures(degree = 4)
p = f.fit_transform(X)
m = LinearRegression()
m.fit(p, y)
这样定义的模型似乎可行。 但是下面的代码只正确地绘制了红点。蓝线到处都是。我需要在下面的代码中更改什么才能使其工作?如果需要此信息,我的变量以以下格式定义:
X=df[[功能]]
y=df[目标]

plt.scatter(X, y, color = 'red')
plt.plot(X, m.predict(f.fit_transform(X)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

是因为您的X数据未排序吗?如果您也使用散点来绘制模型输出会怎么样?我将尝试对其进行排序,不确定如何使用散点来绘制模型输出。散点(X,m.predict(f.fit_transform(X)),color='blue')。所以这条线和你的一样,只是用散点代替了点图。或者将“.”作为第三个参数传递给plot call.sorting help,scatter很容易混淆,因为它不是一条连续线(改为虚线)。谢谢您的帮助:)